2

Would like to send a line to an external programme and inspect output in the minibuffer or a new buffer.

Don't want the output to replace original text. How to do this?

In gnu emacs there's M-| (shell-command-on-region) which will use the either minibuffer or a dedicated "command output" buffer , determined by output length .

6

Assuming you want to send the current line, you can do it this way:

yy                 " Yank the current line
:new               " Split open a new buffer/window
Vp                 " Paste, replacing the empty line
:%!<command>       " Process buffer with your command

I don't know if there is a shorter way.


Update:

Let's combine this, and Matt Boehm's suggestion, into a mapping for you:

nnoremap <Leader><Bar> "zyy:new<CR>:set buftype=nofile bufhidden=wipeout noswapfile<CR>V"zp:redraw<CR>:%!

Put that in your ~/.vimrc and now you can just hit \| when you want to use it.

I took the liberty of changing Matt's bufhidden=hide to bufhidden=wipeout which will will make the scratch buffer very temporary. Now it will automatically destroy itself if you close the newly split window (for example with Ctrl-W C). But beware: it will also discard any unsaved changes. Choose whichever behaviour you prefer.

We use the 'z' register in this mapping to avoid clobbering the default yank register, which might have something useful in it.

The :redraw helps to show what is going on but is entirely optional. Without it, you won't see the split window until you have executed (or cancelled) the external command.

| improve this answer | |
  • 1
    This is a very sensible solution, I like it. – tommcdo Feb 25 '15 at 3:23
  • 1
    Note that if this new buffer is meant to be temporary, you may want to make it a scratch buffer by doing setlocal buftype=nofile bufhidden=hide noswapfile. Among other things, this lets you quit vim without saving/deleting that buffer. If you do this regularly, I'd suggest making a keyboard shortcut or getting one of the "scratch" plugins. – Matt Boehm Feb 25 '15 at 17:24

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.