Home > vim > [vim] a concrete repeat.vim example

[vim] a concrete repeat.vim example

In vim you have a custom mapping that consists of a series of commands. You would like to repeat it with the “.” command but it only repeats the last part of the series of commands, not the whole.

Tim Pope has a plugin for this called repeat.vim . However, he doesn’t give a concrete example in the README so it’s very hard to figure out how it works.

Let’s see the following mapping that surrounds a word with apostrophes:

nnoremap <Leader>' viw<esc>a'<esc>hbi'<esc>lel

Usage: put the cursor on a word and press <Leader>' to surround it.

If you try to repeat it with “.”, it will only repeat the insertion of the second apostrophe.

How to make the whole mapping repeatable:

    nnoremap <silent> <Plug>SurroundWordWithApostrophe  viw<esc>a'<esc>hbi'<esc>lel
        \ :call repeat#set("\<Plug>SurroundWordWithApostrophe", v:count)<cr>                                                                      
    nmap <Leader>'  <Plug>SurroundWordWithApostrophe


Categories: vim Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: