DEV Community

Discussion on: Use external programs like git in Neovim commands

Collapse
 
pbnj profile image
Peter Benjamin (they/them)

There is also :h filter.

If you press !! in normal mode, you will notice the vim command-line turn into :.! and wait for additional input:

  • The . means "current line"
  • The ! means "execute external program"

Putting both of these things together means the current line in the buffer is piped to an external program and the output is piped back/written into the current line in the buffer.

You can also press !<motion> in normal mode, like !ip, which will be translated to :.,.+n! (where n is the number of lines in your paragraph; if you have 3 lines in your paragraph, it will be :.,.+3!).

This is a more robust version of :'<,'>!.

This is also useful for when you want to execute the current line in an external program.

To put it all together, given a shell script like this:

#!/bin/bash

# 1. get the current user
# 3. print date
# 2. list files

whoami

ls
date
Enter fullscreen mode Exit fullscreen mode
  • Place the cursor on line 3, press !ip, type sort, hit enter. This will sort the comments to be in correct order
  • Place cursor on line 7, press !!, type sh, hit enter. This will execute whoami and write the output back into the buffer.
  • Place cursor on line 9, press !ip, type sh, hit enter. This will execute both ls and date in external shell and write the output back into the buffer.
Collapse
 
danrot90 profile image
Daniel Rotter

So far I've only used the visual mode instead of motions for filters, but depending on the situation this might be the easier thing to do. Thanks for mentioning it!