VIM: insert empty ERB tags

2019-01-28 14:21发布

问题:

How can I insert empty ERB tags and put cursor inside it? It is similar to surrounding with surround plugin, but there is nothing to surround.

For example, from this:

bla|bla

I want get this:

bla<%= | %>bla

回答1:

I would use Tim Pope's surround plugin to accomplish this.

Add the following to you ~/.vim/after/ftplugin/erb.vim

let b:surround_{char2nr('=')} = "<%= \r %>"
let b:surround_{char2nr('-')} = "<% \r %>"

Now when you press <c-s>= it will insert <%= | %> just as you wanted.

You may also want to look at Tim Pope's ragtag plugin which has such mappings already included.



回答2:

To insert predefined block of text, you should have a look at Snipmate. It will insert code and position the cursor to a predefined position.

I am not sure ERB is supported but it is very easy to add your own snippets.

For example, something like :

snippet ta
     <%= ${1} %> 

in erb.snippets should insert the text when typing ta

However, you can find plenty of snippets for Snipmate online.

For example, https://github.com/scrooloose/snipmate-snippets contains lots of predefined snippets.

See previous answer on how to install them.