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?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
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.
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
qa 2dw q
and played back with
Macros can be saved into an arbitrary register via
:help y or via vimscript:
"ay :let @a = 'd2w'<CR>
and used the same way:
Macros can also be saved in a variable:
:let foo = 'd2w'<CR>
and used in a slightly different way:
Macros can also be typed verbatim in the command-line, to be executed with
Macros can also be saved for later in the form of a mapping:
:nnoremap <key> d2w
and used by pressing
In all the examples above,
d2w is your macro. All the rest is more or less orthogonal to that macro.
<leader> key is not a special key at all.
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.
the leader key which seems to provide much the same functionality
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".