Ashwin Nanjappa
Ashwin Nanjappa

Reputation: 78498

How to write from Vim buffer to console?

If I run a shell command from inside Vim, say :!ls, it's output will still be visible in the shell after I quit Vim.

I want to write selected lines from my current buffer in Vim to the shell in a similar manner. That is, those lines should be visible at the console after I quit Vim. How to do this?

To write lines 4-10 for example, this command did not do that: :4,10w !tee

Upvotes: 1

Views: 193

Answers (2)

pacholik
pacholik

Reputation: 8972

What about simple catting the file

:!cat %

(as oposed to :%!cat which would print only to buffer)


Print only selected lines:

:execute '!sed -n ' . line("'<") . ',' . line("'>") . 'p %'

You can define a command for that

command! -range PrintSelected :execute '!sed -n ' . line("'<") . ',' . line("'>") . 'p %'

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172540

To build upon @pacholik's answer (and answer your extended question from the comments), in order to only list the visual selection (or any other range of lines), you can use sed:

:execute printf('!sed -n %d,%dp %%', line("'<"), line("'>"))

Upvotes: 0

Related Questions