Wednesday, April 27, 2022

[SOLVED] Pipe a vim command after a shell command

Issue

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?


Solution

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>


Answered By - romainl
Answer Checked By - Terry (WPSolving Volunteer)