There are 3 scenarios I see here:
Adding a vim command
If you want to add a new vim command, so that typing :W would write the buffer, do git commit and quit vim, you'll need to follow JonnyRaa's answer. Namely, define a function, and then define a command that executes that function:
function! WriteCommitAndQuit() w silent !git commit -am "auto" q endfunction command! W call WriteCommitAndQuit()
Notes:
- These lines can be put into ~/.vimrc, or executed directly after hitting
: to go into command line mode. - I used
silent to avoid getting the "Press ENTER or type command to continue" prompt. - You might want to use
write and quit instead of w and q, for nicer-looking code. (Conversely, silent can be shortened to sil if you're in a hurry...)
Adding a keyboard mapping
If you want to just be able to hit a single key to run these commands, you'll need to add a mapping, and the trick for putting a shell command in the middle is to use <CR> to simulate hitting ENTER:
map <F10> :w<CR>:silent !git commit -am "auto"<CR>:q<CR>
Notes:
- Once again, this can go in ~/.vimrc or execute by hitting
: and typing it directly. - Instead of
silent, you can just add an extra <CR> after the shell command.
Executing directly
Sometimes there's a sequence of commands you want to run repeatedly, but they're not worth bothering to persist into vimrc, since they are ad-hoc and won't be useful in the future. In this case, I just execute the line and then rely on the up arrow to bring the line again from history and execute it. In this case, the trick is to add an escaped LF character in the middle of the command, using Ctrl-V, Ctrl-J:
:w | silent !git commit -am "auto" ^@ q
Notes:
- The
^@ in the command is what gets shown when I hit Ctrl-V, Ctrl-J. It won't work if you hit Shift-6, Shift-2. However, it does seem to work if I hit Ctrl-Shift-2. - You can also replace the first pipe character with LF, but you don't have to.
- In case you're curious, Ctrl-J is LF because LF is ASCII 10, and J is the 10th letter of the ABC. Similarly, Ctrl-I is Tab, Ctrl-M is CR, and Ctrl-] is Escape, since Escape is ASCII 27, and if you look at an ASCII table, you'll realize that
] is the 27th letter of the ABC.
vimcommands. One of the commands happens to be a!command that executes a shell command, but the two commands can be anything.pdflatexandgitare notvim commands, they areshell commands. You have to use!to execute them inside vim. In my question, theqwould be translated to!qwhich isq (in bash)and produce error.vim commandsis not code, and should not be quoted in backticks. Consider italicising for emphasis. BTW, would y'all mind linking to the proposed dupe so others have context to build an informed decision on the matter?:!, according to the docs.