DEV Community

Cover image for Vim to the rescue: Subduing the Shell
Angad Sharma
Angad Sharma

Posted on

Vim to the rescue: Subduing the Shell

Introduction

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:

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>
Enter fullscreen mode Exit fullscreen mode
  • Additionally you can take your cursor to the position where the command output ends:
:r! <shell_command>
Enter fullscreen mode Exit fullscreen mode
  • 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"
Enter fullscreen mode Exit fullscreen mode

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
:.!$SHELL
Enter fullscreen mode Exit fullscreen mode

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
:5!$SHELL
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
joaomcteixeira profile image
João M.C. Teixeira

It is about VIM, so I like it!