I've been trying to find something that will let me run multiple commands on the same line in Vim, akin to using semicolons to separate commands in *nix systems or &
in Windows. Is there a way to do this?
A bar |
will allow you to do this. From :help :bar
'|'
can be used to separate commands, so you can give multiple commands in one line. If you want to use'|'
in an argument, precede it with'\'
.
Example:
:echo "hello" | echo "goodbye"
Output:
hello
goodbye
NB: You may find that your ~/.vimrc
doesn't support mapping |
, or \|
. In these cases, try using <bar>
instead.
|
! –
Syringa map
statement (and believe me, you will), check out :help map_bar
. –
Ciceronian .vimrc
doesn't support an escaped bar (\|
) for mappings. I learned I have to actually type out <bar>
. –
Durkee :help bar
shows motion.txt though and nothing about multiple commands. –
Polk normal
as this command cannot be followed by another command, since any '|' is considered part of the command.
–
Yoshikoyoshio :help <bar>
–
Jennette -bar
, for |
to work as command separator - see :h command-bar
. –
Brisesoleil Put <CR>
(Carriage Return/Enter) between and after commands. For example:
map <F5> :w<CR>:!make && ./run<CR>
Don't use |
because:
Some commands have problems if you use
|
after them|
does not work consistently in configuration files, see:help map_bar
:h map_bar
I got that it was ok to use <bar>
. But you're right, it is complicated. Reading :h map_bar
is the way to go here. –
Flashcube You could define a function that executes your commands.
function Func()
:command
:command2
endfunction
And place this in, for example, your vimrc. Run the function with
exec Func()
:call Func()
because :execute Func()
means something more. It means to perform the return value of the function as a command. The function described here will not usually have a command that starts with :return
, so its return value will be the number zero. Performing that as a command will move the cursor to the first line of the current buffer, which is not always what you had in mind. –
Vanillic call
works; exec
produces unexpected results. Thank you! –
Farrison Thought this might help someone trying to do substitutions in a chain and one fails
from a comment
%s/word/newword/ge | %s/word2/newword2/ge
You can use the e
flag to ignore the error when the string is not found.
I've always used ^J
to separate multiple commands by pressing Ctrl+v, Ctrl+j.
^J
as command separator in a string because it is inserting the NULL character terminating the string. however you can use <CR> = "\n". –
Literal exe "echo 'foo' \n echo 'bar'"
–
Literal exe "echo 'foo'" . nr2char(0x0a) . "echo 'bar'"
–
Literal map aa :echo 'foo' <C-V><C-J> echo 'bar'<CR>
–
Literal You can create a new file, and write your commands on it. Then :so %
, which means source current file.
© 2022 - 2024 — McMap. All rights reserved.
;
instead of&&
to separate Unix shell commands too! – Goer&&
is 'boolean and' in shell commands so if you havecommand1 && command2
,command2
will only execute ifcommand1
executed successfully. with;
you're just manually specifying the end of that line and starting a new one. It's the same as writing each command on a different line in a shell script. – Wire