You could
make them a command
:command! DoStuff thing1 | thing2
then execute it with
:DoStuff
or a macro
qq:thing1<CR>:thing2<CR>q
(<CR>
means "press ENTER") that you
access with
@q
or a function
:funct! DoStuff()
: thing1
: thing2
:endfunct
that you
or just run
:thing1 | thing2
in a single command line and then
hit :thi
(ie the first few letters
of the command) and then use the
up-arrow to recall the most recent
lines of command history that started
with that command.
Using the "bar", ie |
, command separator will not work with all commands, but will work with most. If thing1
or thing2
is one of the commands that can't be sequenced with a bar, you'll have to either
- do it with a macro or a function
or you can do
:exec 'thing1' | exec 'thing2'
on the command line or in a :command
, but you'll have to be careful if thing1
or thing2
themselves contain single quotes.
The choice of which of these options is the most appropriate is usually based on the complexity of what you want to do.
Functions make it easy to parameterize your commands, ie to use variables in them, and are useful for combining a long series of commands, but are somewhat long-winded for simple cases. See :help user-functions
for details.
Macros are convenient but you can only have 26 of them and they are usually pretty poorly named (ie with some letter of the alphabet). Also note that if you store something in the same register via y
, c
, etc, it will overwrite your macro. See :help recording
for details.
Commands let you give your routines a meaningful name, can be parameterized in a different way than functions, and can do other fancy things like tab completion. Commands have to be written on a single line, which is one reason why it's common for vim scripts to implement commands that do nothing but call functions. See :help user-commands
for more info.
If this series of commands ends up being something that you use on a regular basis, you can add your macro/command/function definition to your vimrc
file, which will cause it to be ready to go whenever you use vim. Under linux or OS/X your vimrc file is the file ~/.vimrc
. Getting macros set up in your vimrc is sort of tricky though, and if you're going to go to that length you might as well make it a function or a command anyway.