loading...
Cover image for Vim to the rescue: Subduing the Shell

Vim to the rescue: Subduing the Shell

l04db4l4nc3r profile image Angad Sharma ・1 min read

Vim to the rescue (9 Part Series)

1) Vim to the rescue: Prologue 2) Vim to the rescue: Tree to Markdown 3 ... 7 3) Vim to the rescue: Repetition Made Easy 4) Vim to the rescue: Human Readable Processes 5) Vim to the rescue: Multiple Cursors 6) Vim to the rescue: Sessions 7) Vim to the rescue: Attached Terminal 8) Vim to the rescue: Subduing the Shell 9) Vim to the rescue: PDF Preview

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>
  • 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
:.!$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
:5!$SHELL

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>

Vim to the rescue (9 Part Series)

1) Vim to the rescue: Prologue 2) Vim to the rescue: Tree to Markdown 3 ... 7 3) Vim to the rescue: Repetition Made Easy 4) Vim to the rescue: Human Readable Processes 5) Vim to the rescue: Multiple Cursors 6) Vim to the rescue: Sessions 7) Vim to the rescue: Attached Terminal 8) Vim to the rescue: Subduing the Shell 9) Vim to the rescue: PDF Preview

Posted on by:

l04db4l4nc3r profile

Angad Sharma

@l04db4l4nc3r

Backend Developer and DevOps SysAdmin

Discussion

markdown guide