Multiple commands on same line

VimCommand

Vim Problem Overview


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?

Vim Solutions


Solution 1 - Vim

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.

Solution 2 - Vim

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

Solution 3 - Vim

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()

Solution 4 - Vim

The command seperator in vim is |.

Solution 5 - Vim

I've always used ^J to separate multiple commands by pressing Ctrl+v, Ctrl+j.

Solution 6 - Vim

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.

Solution 7 - Vim

You can create a new file, and write your commands on it. Then :so %, which means source current file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBub BradleeView Question on Stackoverflow
Solution 1 - VimmichaelmichaelView Answer on Stackoverflow
Solution 2 - VimBabken VardanyanView Answer on Stackoverflow
Solution 3 - VimJonatanView Answer on Stackoverflow
Solution 4 - VimDaenythView Answer on Stackoverflow
Solution 5 - VimeruciformView Answer on Stackoverflow
Solution 6 - VimJerinawView Answer on Stackoverflow
Solution 7 - VimSongView Answer on Stackoverflow