I have a binding in my .vimrc that reads the contents of the system clipboard to a line immediately below my cursor

nnoremap <silent> <leader>f <esc> :read ! test -f /usr/bin/xsel && /usr/bin/xsel -ob \|\| /usr/bin/pbpaste<cr>

I tried to change it to (1)

nnoremap <silent> <leader>f <esc> :read ! /usr/bin/xsel -ob \|\| /usr/bin/pbpaste<cr>

or (2)

nnoremap <silent> <leader>f <esc> :read ! /usr/bin/xsel -ob ; /usr/bin/pbpaste<cr>

for the sake of simplicity, even though it's more brittle.

When I change it to either of those, however, I get the contents of stderr in my buffer as well, as if vim is reading from both stdout and stderr when executing the command. (this is after echo clipboard_contents | pbcopy).

/bin/sh: /usr/bin/xsel: No such file or directory

Why is vim doing that? Is there a way to tell it to silently drop stderr in this case or redirect it to /dev/null?


From :h :r!:

                                                        :r! :read!
:[range]r[ead] [++opt] !{cmd}
                        Execute {cmd} and insert its standard output below
                        the cursor or the specified line.  A temporary file is
                        used to store the output of the command which is then
                        read into the buffer.  'shellredir' is used to save
                        the output of the command, which can be set to include
                        stderr or not.

And in :h 'shellredir':

'shellredir' 'srr'      string  (default ">", ">&" or ">%s 2>&1")
                        {not in Vi}
        String to be used to put the output of a filter command in a temporary
        file.  ...
        The default is ">".  For Unix, if the 'shell' option is "csh", "tcsh"
        or "zsh" during initializations, the default becomes ">&".  If the
        'shell' option is "sh", "ksh" or "bash" the default becomes
        ">%s 2>&1".  This means that stderr is also included.

So, just do:

set shellredir=>
| improve this answer | |
  • Wait is read ! one atomic thing or decomposable as read and !? I looked at help read and read no further :/ – Gregory Nisbet Mar 13 '17 at 2:09
  • @GregoryNisbet decomposable, since options affecting ! would also affect :r!.But that doesn't mean the combination may not have its own special properties. – muru Mar 13 '17 at 2:11
  • Depends on what you want to do, but it might be easier to redirect the command itself, or wrap it in sh: :r! sh -c '....' 2>/dev/null. Otherwise you'd have to write a function. – muru Mar 13 '17 at 2:21

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.