How to write buffer content to stdout?

Viewed 21700

Is there any chance to write the content of the current vim buffer to stdout?

I'd like to use vim to edit content that was passed via stdin - without the need of a temporary file to retrieve the modified content (on Linux/Unix).

Is it possible that a plugin/script - that act on quit or save put the buffer content to stdout?

11 Answers

You can use the ex-mode (p)rint command to print any set of lines you want to stdout.

print all lines:

:1,$p

Also prints all lines (% is a shorthand for the range 1,$)

:%p

print lines 4-10:

:4,10p

print next line containing FOO

/FOO/ p

print all lines containing FOO

g/FOO/ p

This post gave me an idea: copying the buffer/or selection to clipboard:

  1. Install xclip (any linux distribuition package manager should have it.)

  2. Do this vim command:

:w !xclip

If you want to copy only some lines, select tem first with (normal mode) V, then issue the command above.

What helped resolve matters for me is adding

setenv SHELL /bin/bash
exec bash -l

to .bashrc, otherwise vim can launch the wrong shell. Also, a script called save, which encrypts stdin and saves the result to $1, so the command in vim becomes :w !save afilename.

Related