How to Insert Erb-Tags with Vim

How do I insert ERB-tags with Vim?

I've been using the surround.vim plugin for a long time, but not the rails.vim plugin. Turns out that the rails.vim plugin is adding functionality to surround.vim, or is piggy-backing on it. Either way, I got it working this-a way:

  1. Install the rails.vim plugin if it isn't already, and open an ERB document. Vim should recognize it as a eruby filetype.
  2. If it doesn't use :set filetype=eruby Return to make Vim see things your way.
  3. Visually select some text you want to wrap with the ERB tags using v or V.
  4. Press S= or S-. (That's capital S)
  5. The selected text should be wrapped in a <%= %> or <%- %> block.

I just confirmed this works in column-selection mode too.

This is documented in the rails.vim help: from the command-mode search for :h rails-surround. And if you don't have the help available for rails.vim, it's because the instructions weren't followed for installing it:

:helptags ~/.vim/doc

The rails.vim plugin requires the surround.vim plugin also, so that has to be previously installed. I use that plugin's functionality at least once a minute when I'm editing. It rocks.

VIM: insert empty ERB tags

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.

Adding erb wrap to surround.vim

Create the file ~/.vim/after/ftplugin/erb.vim and add the following lines:

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

The other method is to use autocmd's to activate the surround mappings. I tend prefer using ~/.vim/after/ directory instead of cluttering up my ~/.vimrc with filetype specific settings.

Indenting of ERB tags in Vim

I believe that there are 2 possible ways of solving your problem.

Firstly, temporarily remove the Janus distribution and replace it with a minimum .vimrc. Then install the ERuby plugin and see if it works. If it works now then the problem has to be a conflict with one of the other plugins, and you should try reenabling the different Janus plugins one by one to see where the fault lies. However, if it still doesn't work then you know that the plugin itself doesn't work on your system and you could send a detailed bug report to the author (including the operating system you are using and your vim version).

Alternatively, it is very easy to reconfigure Vim to use a different indentation method.
You could use a different built-in indentation (:h indentexpr), for example for XML:

autocmd FileType eruby setl indentexpr=XmlIndentGet(v:lnum,1)

Or you could configure Vim to use an external indentation program (:h equalprg).

How to add %= % & variants in vim insert mode while using vim-rails and vim-surround?

You are looking for RagTag or Rails plugins (in addition to surround) which provides the surroundings you need. You would use Rails.vim if you are using rails and RagTag otherwise. You can have both installed if needed.

Once you have surround and RagTag/Rails installed you can just do the following:

  • <c-s>= insert mode for <%= %>
  • <c-s># insert mode for <%# %>
  • Visually select code and do S= to surround with <%= and %>

Note: You may have to use <c-g>s for insert mode surrounding if you are using a the terminal. You could also disable your terminal flow control by running stty -ixon which would allow you to use the <c-s>/<c-q> keys (I personally disable flow control in my ~/.bashrc).

If you want to add "surroundings" to other filetypes please read :h surround-customizing.

Please read both the surround documentation and whichever plugin you decide to install for more details.

Vim html.erb snippets?? snipMate Need a vim tip

Snippets are stored in directory called snippets somewhere in your ~/.vim folder.

If you look there, there is usually one file per filetype, there is a c.snippets, a ruby.snippets, so it seems what you have to do is to create an erb.snippets there with what you want.

Eventually you could copy the content of ruby.snippets and html.snippets into your new erb.snippets.

Alternatively you can search on github, some people have posted their own erb.snippets configuration. For example, there is a nice collection there :
https://github.com/scrooloose/snipmate-snippets

The best thing would to try first to open a snippet file and look at the syntax, it is pretty easy to create your own snippet depending on what you use the most.

how to exclude dash sign (-) from erb tags % -% when using rails.vim and surround vim plugins

Hop over to rails.vim:

mvim ~/.vim/autoload/rails.vim

and remove hyphen in the following line:

call self.setvar('surround_45', "<% \r -%>")

Now yss- will wrap code in line with <% %>.

You will probably want to do the same with the end-shortcuts — see this diff for the details (it was commited two days ago).

How to avoid constant switching to and from English keyboard layout to type Vim commands when writing in non-Latin language (e.g., Greek)?

This problem can be solved with the help of the keymap option.
It allows to define an alternate keyboard mapping to use in modes
requiring text input.

To switch between the default and alternate keymaps while in Insert,
Replace, or Command-line mode (but not Normal mode), use
Ctrl+^ (Ctrl+6).
Changing the keymap affects text input only; keyboard behavior in
Normal mode stays the same regardless of the current keymap setting.
One can leave Insert mode writing in Greek and immediately use
Normal-mode keybindings without switching to a different keyboard
layout. If one then returns to Insert mode or, for example, starts
a search by typing /, Vim switches the keymap back to Greek
automatically.

The current keymap used in those text-entering modes is remembered
between switchings to other modes. The only exception from this
behaviour is made for Command-line mode which always starts with the
default keymap, since most of the time it is required to type an Ex
command (in ASCII). With the keymap option set, user is supposed
to work in Vim keeping system keyboard layout set to English while
switching Vim keymap with Ctrl+^ (instead of
the system-wide layout switch).

To enable, say, the UTF-8 Greek keymap permanently, add the following
line to your .vimrc file.

:set keymap=greek_utf-8

There are many predefined keymaps for a large set of languages, you
can browse them all in Vim itself using :e $VIMRUNTIME/keymap. Note
that usually there are several keymaps provided for one language which
differ only by character encoding, so that anybody could choose one
that suits their configuration.

I also recommend setting the options below to specify whether the
keymap should be enabled by default in Insert mode and when entering
a search pattern:

:set iminsert=0 imsearch=-1

See :help iminsert and :help imsearch for the detailed explanation.

There is also a special language mode that, if I am not mistaken,
was introduced in Vim earlier than keymap. It allows to achieve
the behaviour similar to the one provided by keymap through manually
specifying letter pairs that correspond to the keys on keyboard in
a long string to be saved in the langmap option. Personally—my
native language is not English, too—I prefer (and recommend) using
the keymap way instead.

In conclusion, I should emphasize that all of the above is equally
applicable to any other language Vim has (or can be configured to
have) a keymap for.

See also my answer to a similar question ‘Vim “annoyance”
with keyboard layouts’ that has been asked since I originally
gave this answer.



Related Topics



Leave a reply



Submit