2

Currently I tend to use macros and have not learned how to use the leader key which seems to provide much the same functionality.

Aside from it being easier to type , than @ what is the gestalt of using a leader key as opposed to a macro?

  • 2
    A macro is something you record and replay. The leader key is a sort of shift, it allows you to define new key mappings, even though most easily accessible keys on the keyboard are already taken. Basically, you're comparing apples with orange juice. – Sato Katsura Jul 7 '16 at 16:07
  • @SatoKatsura I understand what they do, I just don't understand on practical basis what the difference is between them. Why not just use macros for everything? Why ever use a leader key, when I can do the same thing with a macro? – Tyler Durden Jul 7 '16 at 16:08
  • 1
    From my vimrc: nmap <Leader>[ <Plug>yankstack_substitute_older_paste. How would you write this as a macro? – Sato Katsura Jul 7 '16 at 16:14
7

I can see some confusion if you have been reading the help docs because they use some similar names even though these two things are pretty different.

The @ command will "play back" the contents of the register. Usually, macros are recorded into the registers with the q command. You can also set the registers by yanking into them or through vimscript. You can view :h @ for more info on how vim macros work.

The leader is designed to make managing your keybindings easier. For example, in my vimrc I have a keybinding to make quitting faster. nnoremap <leader>q :q<cr>. If I set my leader to , the command to quit would be ,q. If I set the leader to space, the command will be q. When you have a lot of keybindings, it can be convenient to have one "prefix" that you can type to use them all, to minimize the collisions with the native keybindings. :h leader will give you more information about this.

  • 1
    ...or you could just create the macro :q<CR> and assign it to Q so you type @q to do the same thing. I am still not seeing the difference. – Tyler Durden Jul 7 '16 at 16:23
  • 2
    There are only 26 registers (ignoring all the special ones). I have many more than 26 keybindings. That's one problem. Another problem: what if I want to yank something into the q register to paste somewhere else? the minute I run "qyy, my quit macro is gone. Keybindings don't have this problem. – DoYouEvenCodeBro Jul 7 '16 at 16:25
  • 2
    Okay, from what you are saying macros are more for limited temporary functionality and key bindings for a wider range of long-lasting behavior. Is that the idea? – Tyler Durden Jul 7 '16 at 16:27
  • Yes, exactly! :) – DoYouEvenCodeBro Jul 7 '16 at 16:28
  • 1
    @AaronMcMillin I don't use any insert mode mappings, I prefer to do things from normal mode. it is a pain in command mode when I have to type spaces, so I have <space><space> mapped to <space> just to be able to quickly enter a space :d – DoYouEvenCodeBro Jul 7 '16 at 21:44
6
  1. A macro is a sequence of keystrokes. Whether they are recorded or not, whether they are stored in a register or not is completely irrelevant.

    Macros can be recorded in an arbitrary register with :help q:

    qa
    2dw
    q
    

    and played back with :help @:

    @a
    

    Macros can be saved into an arbitrary register via :help y or via vimscript:

    "ay
    :let @a = 'd2w'<CR>
    

    and used the same way:

    @a
    

    Macros can also be saved in a variable:

    :let foo = 'd2w'<CR>
    

    and used in a slightly different way:

    @=foo<CR>
    

    Macros can also be typed verbatim in the command-line, to be executed with :help :normal:

    :normal! d2w<CR>
    

    Macros can also be saved for later in the form of a mapping:

    :nnoremap <key> d2w
    

    and used by pressing <key>.

    In all the examples above, d2w is your macro. All the rest is more or less orthogonal to that macro.

  2. The <leader> key is not a special key at all.

  3. What you are trying to compare are two ways to play back macros, not… macros and something else.

    If you are more comfortable with registers, use registers along with any of the register-related methods above.

    If you are more comfortable with mappings, use mappings.

    If you are just starting with Vim, try both and decide for yourself.


Note: The best Vim video ever happens to revolve largely around macros.

3

the leader key which seems to provide much the same functionality

No.

A macro is the recording of a succession of keystrokes, that you can replay.

The leader key is a special key that you can use in your mapping with <leader> and change only once in your configuration file.

You may want to use a macro when having a repetitive task that you can repeat easily with a succession of keystrokes.

You may want to use the leader key when a mapping you are defining needs not to override an existing mapping or when you want to put the mapping in your "personal mapping".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.