How many times have you ran a command on your terminal and copy pasted its output to vim? Maybe you want to include some terminal output data, or maybe you want to create an issue on github by editing it in vim first.

In this blog we will be learning how to get the output of shell commands in your current vim buffer.

For reference, here is what we are going to achieve:

{% youtube euwcDjaBOxI %}

Getting the output of a script

  • You can take the output of any shell script and paste it after your cursor position by using the following command:
" . means current line
:.! <shell_command>
  • Additionally you can take your cursor to the position where the command output ends:
:r! <shell_command>
  • The best part about this is that you can use the text inside your vim buffer and pass it through the command line. For example if your current buffer contains something like this:
My cow says:

cowsay "Hello, World"

You can go over to the cowsay command and press the following keys to get the output right in your buffer:

" passes the current line through a shell

You will get an output like this:

Alt Text

  • Not only this, you can take the text from any line and paste its output:
" takes the text in the 5th line as input

Mapping the madness

I have mapped <Shift> + q to the .!$SHELL command. So pressing the keybinding instantaneously returns the output of the current line when ran through a shell.

noremap Q !!$SHELL<CR>