250

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?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Bub Bradlee
  • 2,905
  • 2
  • 19
  • 15
  • 2
    Wow I was trying to learn about vim from your question (which I did) and found out I could use `;` instead of `&&` to separate Unix shell commands too! –  Apr 02 '13 at 12:07
  • 10
    @ebyrob I feel it's important to note that `&&` is 'boolean and' in shell commands so if you have `command1 && command2`, `command2` will only execute if `command1` 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. – Will Apr 16 '13 at 20:24
  • ...and command1 || command2, command2 will only execute if command1 fails. That's because in computer logic the || and && are bitwise, and even though "&&" implies that both command1 AND command2 are to be evaulated, command2 does NOT need to be evaluated if command1 fails because no matter what cmd2 returns, it will always fail since the first one returned false (0), and 0&&1 would still be zero.Similarly, if command1=true, theres no reason for command 2 in an OR evaluation because at that point it's already true (1), because 1||0 is 1, and only if it's 0, would need 0||1(which is still 1) – osirisgothra Oct 16 '14 at 17:38
  • @osirisgothra that is known as short circuiting/ short circuit evaluation. – speakingcode Jun 04 '19 at 15:22

7 Answers7

305

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.

michaelmichael
  • 13,755
  • 7
  • 54
  • 60
  • 12
    Just watch out for the handful of commands that don't work with `|`! – too much php Jul 14 '10 at 22:53
  • 36
    When you find yourself wanting to use multiple commands in a `map` statement (and believe me, you will), check out `:help map_bar`. – Bill Odom Jul 14 '10 at 23:36
  • 9
    That's true. I asked that very question on superuser a few months ago. My `.vimrc` doesn't support an escaped bar (`\|`) for mappings. I learned I have to actually type out ``. – michaelmichael Jul 15 '10 at 15:23
  • Thanks for the working solution! `:help bar` shows motion.txt though and nothing about multiple commands. – geekQ Sep 13 '12 at 17:52
  • Is there a way to run all the commands even if some of the previous commands have failed? Asking because I've noticed that if I run :command1|command2 and command1 fails, the command2 won't be executed. – ka3ak Oct 04 '16 at 17:37
  • @ka3ak Found the answer here: http://vim.wikia.com/wiki/Multiple_commands_at_once Just use :silent! cmd1|cmd2|... – ka3ak Oct 05 '16 at 10:51
  • Special consideration needs to be taken for `normal` as `this command cannot be followed by another command, since any '|' is considered part of the command.` – dev Nov 25 '19 at 17:34
  • 1
    @geekQ should be `:help ` – Sri Kadimisetty Jan 20 '20 at 23:41
116

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

Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87
48

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()
Jonatan
  • 2,734
  • 2
  • 22
  • 33
  • 2
    Clean solution. Would definitely be handy for more than two or just long commands. – elimirks Nov 07 '13 at 20:18
  • 1
    Very useful also for the ability to hand errors with try/catch. – Immanuel Weihnachten Sep 23 '16 at 09:11
  • 4
    It's more conventional to use `: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. – minopret Oct 18 '18 at 08:21
  • @minopret thank you so much! I was debugging and turned out only `call` works; `exec` produces unexpected results. Thank you! – kohane15 Jul 01 '22 at 07:50
35

The command seperator in vim is |.

Daenyth
  • 35,856
  • 13
  • 85
  • 124
21

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.

Victor S.
  • 2,510
  • 3
  • 22
  • 35
Jerinaw
  • 5,260
  • 7
  • 41
  • 54
19

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

kenorb
  • 155,785
  • 88
  • 678
  • 743
eruciform
  • 7,680
  • 1
  • 35
  • 47
3

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

Song
  • 31
  • 2