tpope / vim-repeat

repeat.vim: enable repeating supported plugin maps with "."
http://www.vim.org/scripts/script.php?script_id=2136
2.58k stars 81 forks source link

please add a concrete example #55

Open jabbalaci opened 8 years ago

jabbalaci commented 8 years ago

By reading the README it was not clear to me how to use this plugin. Using this screencast (http://vimcasts.org/episodes/creating-repeatable-mappings-with-repeat-vim/) I managed to figure out how it works. Here is a concrete example, I think it would be a good idea to add it (or something similar) to the README:

before

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

It surrounds the current word (manual solution).

after

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

Now it's repeatable with the "." command.

robertf57 commented 8 years ago

I agree with jabbalaci. It's not at all clear how to use this plugin. I thought you could execute a plugin command, move to another position in your file, hit ".", and the command would repeat itself. Clearly that's not the case as you still need to create a custom mapping for this to work.

Goli4thus commented 1 year ago

I also agree with the above. Just tried to integration vim-repeat with my own plugin (still unreleased) and stuggled quite a bit. Neither by looking at the official help page or README, nor by looking at the developer comments in vim-repeat source code, nor by looking at other plugins like vim-surround or nerdcommenter did it really make click for me. Only after stumbling over this discussion and seeing the example by @jabbalaci did it make more sense. With some trial and error after that I finally got it working for my own case.

But yeah, having a couple simple examples would make adoption more doable.

Here's my gist of what I've learned today:

Let's say originally we had a mapping like this one:

execute 'nnoremap <leader>k ' .
            \ ':call MyPlugin#DoStuff(1, 2)<CR>'

Adding vim-repeat:

execute 'nnoremap <silent> <Plug>MyPluginMap ' .
            \ ':call MyPlugin#DoStuff(1, 2)<CR>'
            \ ':call repeat#set("\<Plug>MyPluginMap", -1)<CR>'
execute 'nnoremap <leader>k ' .
            \ '<Plug>MyPluginMap'

This example makes use of the second variable being -1, which ignores any count prefixes to the mapping call. In a sense it's about introducing <Plug>MyPluginMap, which is like a placeholder and is being used in the repeat#set() call. Furthermore the actual mapping then is made referencing this placeholder.