DEV Community

Igor Irianto
Igor Irianto

Posted on • Updated on • Originally published at irian.to

Execute command line commands from inside vim

Follow @learnvim for more Vim tips and tricks!

One vim command I have been experimenting with recently is bang (!) command to execute external commands from within vim (:h :!).

Why is this so awesome? Let me show you some examples so you can do even more awesome things!

Curl from inside vim

Yes, you read that right. If you have curl installed, you can curl from inside vim.

:r !curl -s 'https://jsonplaceholder.typicode.com/todos/1'
Enter fullscreen mode Exit fullscreen mode

The r is r[ead]. It is used to display the result. This command is so useful I mapped it as a shortcut (<Leader>cg): nnoremap <Leader>cg :r !curl -s<Space>

Of course, this works with curl POST/PUT/DELETE.

Paste file content

I find this is useful when dealing with fake data and json files. You can do this with :r !cat path/to/your.file

Get list of files

Let's say you need to quickly get a list of all javascript files in a project, you can do something like: :r !ls **.*.js

Get word count

If you want to check how many words in your current file, you can do :!wc %. You can also do the same with g C-g

Send command to other tmux session/window/pane

This is my personal favorite right after curl. This one is for tmux users. If you are working with multiple panes, you can send command to another pane.

:!tmux send-keys -t mySession:myWindow.2 "echo 'HELLO TMUX MAGIC'" Enter
Enter fullscreen mode Exit fullscreen mode

Let me break it down:

  • tmux send-keys sends tmux instructions
  • -t to target
  • mySession:myWindow.2 is tmux's convention to target a specific pane. The syntax is: session_name:window_name.window_number.
  • "echo 'HELLO TMUX MAGIC'" Enter is the instruction

With this, I can send any instruction to any session, any window, any pane. I can tell my "Rails" session to execute rspec on "Test" window's pane #2 while I am sitting inside a different session. How cool is that?

Repeat the previous bang command

After running fairly lengthy command, you can quickly recall previous command with double bang: :!!

Conclusion

I have only scratched the surface of ! potential; being able to execute external command unlocks many possibilities.

What command do you personally find it helpful to execute from inside vim?

Top comments (10)

Collapse
 
jakehamilton profile image
Jake

Should note that :term is also a very useful command! You can move around in the terminal buffer like you'd expect in vim which makes it really convenient to have a full terminal that can easily move text between files and the shell.

Collapse
 
iggredible profile image
Igor Irianto

You're right! The terminal command :)
I like how we can still switch to normal mode while in :term, giving us more power!

 
iggredible profile image
Igor Irianto

Haha thanks! I am genuinely flattered :) I always love a good feedback from someone like you too!

Anyway, it just got me thinking what if we can use quickfix window to complement some command lines like !ls so we are less dependent on NERDTree... sorry for muttering, not really sure exactly what I'm trying to do either haha. Maybe I'll write another post someday if I think of this clearly!

Collapse
 
iggredible profile image
Igor Irianto

Haha, thank you Sang! I am always happy knowing that my post affects other readers 👍
I noticed you've reacted most of my vim posts too! 😁

Collapse
 
danielkun profile image
Daniel Albuschat

With :.! you can insert the result of the command into the current buffer (the file you are currently editing).

Collapse
 
iggredible profile image
Igor Irianto • Edited

I just tried it - this is awesome! Does :.!{cmd} work exactly the same as :r !{cmd} then? If not, what's the difference?

Collapse
 
danielkun profile image
Daniel Albuschat • Edited

Actually I didn't know about :r !{cmd}, so I'm not sure. From a quick test I noticed that :r inserts a newline above the content, while :.! does not. Other than that, dunno ¯\(ツ)/¯.

Thread Thread
 
iggredible profile image
Igor Irianto

Oh, that's an interesting behavior :) Even a newline makes a difference 😎

Collapse
 
vlasales profile image
Vlastimil Pospichal
:r !grep word file
:r !sqlite3 data.sqlite "select value from Snippet where key='word'"
:%!sort
:!php %
Collapse
 
iggredible profile image
Igor Irianto

Oh nice! I didn't think about sqlite and grep. Those two would be super handy.
Regarding sort, I am already used to vim's built-in sort in vim (:h sort) though :)