3

I have

  1. q mapped to :qenter
  2. Q mapped to <esc>:qa!enter

which means

  1. quit, unless something needs to be saved
  2. just quit

My meanings for these keys clashes with vim's idea that stdin needs to be saved, e.g.

  1. $ git log | vim -
  2. q
  3. vim says No write since last change ...
  4. Look at keyboard, find shift
  5. Q

And I can then quit.

How can I tell vim to ignore file saving when using "-" to read for input from stdin?

  • 2
    Warning about vim -: anything sent to stderr is interpreted by vim as a command. To avoid this, you'd want to redirect stderr to stdout: git log 2>&1 | vim - – Matt Boehm Mar 25 '15 at 14:46
  • 3
    q is very handy and used to record macros (see :h q). You may want to think about a different set of keys for you mapping. Maybe use a <leader>. See :h mapleader. – Peter Rincker Mar 25 '15 at 16:53
  • I prefer keyboard macros to recordings, and I also use q mapped to exit in bash, sys.exit() in python, and so weiter. – jalanb Mar 25 '15 at 23:21
6

I would start vim with a command that tells it the current file is a scratch buffer (which vim will not prompt you to save):

git log | vim -c "setlocal buftype=nofile bufhidden=hide noswapfile" -

Because I do this a lot, I save the scratch buffer settings to a command in my .vimrc

":ScratchBuffer makes current buffer disposable
command! ScratchBuffer setlocal buftype=nofile bufhidden=hide noswapfile

I also add a bash function in my .bashrc

v() {
    vim -c ScratchBuffer -
}

This means I can type git log | v to achieve the desired effect

  • 1
    Vim also has the StdinReadPre and StdinReadPost autocmds, which could be used to do this for you automatically. You could also use the VimEnter autocmd, but then you can't distinguish between reading from stdin and just typing vim with no arguments (my answer here actually does something very similar). – Martin Tournoij Mar 25 '15 at 19:27
  • WFM, YMMV – jalanb Mar 26 '15 at 0:10
  • Thanks @Carpetsmoker gitlab.com/phunehehe/my-env/blob/… – phunehehe Mar 1 '17 at 7:05
2

You can run vim in read-only mode (as you're not opening a proper file):

git log | vim -R -

Then standard q should work, without asking you to save your changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.