Friday, 19 March 2010

 

VIM: Creating a Custom Command with a Temporary Map

Maps allow us to create our custom Vim commands

The VIm initialization file .vimrc is typically used to store maps.
eg:-
imap ,,, <esc>bdwa<<esc>pa><cr></<esc>pa><esc>kA
" s commands these are wrap html around visually selected text
vnoremap sb "zdi<b><C-R>z</b><ESC>
vnoremap sq "zdi"<C-R>z"<ESC>
vnoremap sp "zdi<p><C-R>z</p><ESC>
vnoremap s( "zdi(<C-R>z)<ESC>

However it is very easy to create a temporary map eg to use the key '#' as a map

eg a map to copy the word under the cursor into the paste buffer
:map # "*yiw

A map has an advantage over a recording in this case as it is only necessary to type one character, a recording has an advantage over a map in that you don't have to think it out, so why not combine the two?

Map the key # such that it executes the recording q
:map # @q

Labels: ,


Wednesday, 1 July 2009

 

VIM: Creating Your Own Commands with a Map or a Recording

Customizing your own commands using a map or recording.

A Map is generally used for stored commands.

A Recording is used for one off jobs.

Recordings are more intuitive ie you simply record a sequence of commands.

Maps require a little more knowledge of map special characters <CR><ESC> etc

While it is possible to "save" a recording for future use it's a little tricky

:let @w="<C-R>q" ie read recording q (same as register q) and store in register w, which if put in your .vimrc will be preserved for future use

With experience you should find yourself preferring to create disposable recordings and relying less and less on maps; one well known Vimmer (Peppe) has a practically empty .vimrc.

Labels: , , ,


This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]