I'm trying to make a key mapping in vim that (a) saves current file (b) performs a git action, using shell (c) quits current vim editor.
I've tried the following methods but still can't figure it out.
Method 1 - in Vim command line
:w | !git commit -am "auto" | q
Method 2 - in .vimrc
map :W :w \| !git commit -am "auto"
map :L :Wq
Problem
The problem is that the pipe | can only be used to append shell commands. How do I do 'a Vim command' + 'a shell command' + 'a Vim command'? How to pipe a vim command after a shell command?
You need to execute those three commands separately. This is what <CR> is for:
nnoremap <key> :w<CR>:!git commit -am "auto"<CR>:qa<CR>
There are 3 scenarios I see here:
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:
: to go into command line mode.silent to avoid getting the "Press ENTER or type command to continue" prompt.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...)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:
: and typing it directly.silent, you can just add an extra <CR> after the shell command.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:
^@ 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.] is the 27th letter of the ABC.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With