Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to edit shell commands from vim, and execute them from vim. Currently I use !!sh (pipes current line to command sh), but this removes the line itself.

Is it possible to execute the command in a shell, and paste the result below that line?

share|improve this question
up vote 4 down vote accepted

As Clausi pointed out, yyp!!sh yanks (copies) the current line to the register, pastes that line below the original one, and replaces the bottom one with the output of the command.

To save keystrokes you can map this action by adding the next line in the ~/.vimrc file:

map <F5> yyp!!sh<CR><Esc>

This way everytime F5 is pressed in command mode, the line will be executed and the result will be shown below the line.

share|improve this answer

What about copying/pasting the line before executing (something like yyp!!sh)?

share|improve this answer

Type shell command in the buffer, for instance last | head,
and in command line mode :read !^R.
Of course instead of typing literally ^R hit CTRL-R key combination.
Look it up in :help c_ctrl-r. . stands for "the last inserted text".
If the command is one word for example vnstat,
place cursor on that word, and type :read !<cword>.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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