Terminal & External Commands

Vim was born in the Unix world, and it shows. Rather than trying to do everything itself, Vim lets you reach out to the vast ecosystem of command-line tools — sort, awk, jq, grep, curl — and pipe your text through them. This philosophy means you can format JSON, calculate columns of numbers, or sort a list without ever leaving your editor.

Neovim takes this further with a built-in terminal emulator, so you can run a full shell session right inside a split window. Combined with plugins like toggleterm.nvim, the boundary between your editor and your terminal blurs beautifully.

What you'll learn in this chapter:

  • Use Neovim's built-in terminal emulator and navigate between terminal and normal modes

  • Filter buffer text through external commands like sort, jq, and awk

  • Read external command output directly into your buffer with :r !

  • Configure toggleterm.nvim for convenient floating and split terminal workflows

  • Integrate shell tools like ripgrep, fd, fzf, and zoxide into your editing workflow

Neovim Built-in Terminal

Neovim has a built-in terminal emulator:

:terminal          " Open terminal in current window
:split | terminal  " Open in horizontal split
:vsplit | terminal " Open in vertical split

Note: Terminals open in buffer windows just like any other buffer. You can manage them using standard window navigation (see Buffers, Windows & Tabs).

Terminal Mode

Key
Action

Ctrl+\ Ctrl+n

Exit to Normal mode

i or a

Enter Terminal mode (start typing)

<C-w>h/j/k/l

Navigate to other windows (from Normal mode)

Pasting Registers in Terminal Mode

You can paste from registers while in Terminal mode:

This lets you press Ctrl+r followed by a register letter (e.g., + for clipboard) to paste its content, just like in Insert mode.

Terminal Autocommands

toggleterm.nvim

toggleterm.nvimarrow-up-right makes terminals much more convenient:

Custom Terminal Commands

Running External Commands

Reading External Command Output

You can read the output of any shell command directly into your buffer:

The Filter Command !

The filter command complements Vim's built-in text processing commands like :g (see The Global Command) by letting you leverage external tools.

You can filter text through external commands:

The Powerful AWK

AWKarrow-up-right is a powerful text-processing language. You can use it from within Vim:

Example: Calculate ages from birth years:

Given a table like:

In AWK, $4 is the 4th field (birth year). We replace it with the current year minus the birth year.

Exchange last two fields:

$NF is the last field, $(NF-1) is the second-to-last.

AWK Ternary Operator — classify values conditionally:

This replaces the second-to-last field with "retired" or "working" depending on whether the value is greater than 65.

Sum the last field:

Print only specific fields (extract columns):

Tip: Use column -t to align output into neat columns: :%!awk '{print $1, $2}' | column -t

Other Useful External Commands

Using ripgrep (rg) and fd

Modern alternatives to grep and find:

Use them from Vim:

Neovim: Telescope uses ripgrep and fd automatically for live_grep and find_files.

Shell Integration

Opening Files from the Shell

Clipboard Aliases

ZSH Auto-Suggestions

zsh-autosuggestionsarrow-up-right suggests commands as you type. Bind Ctrl+Space to accept:

Open Clipboard Content in Vim (ZSH Widget)

You can create a ZSH widget that opens clipboard content directly in Neovim:

Press Ctrl+x Ctrl+v in ZSH to open clipboard content in Neovim for editing.

Quick File Opening with fzf

zoxidearrow-up-right (modern replacement for z, autojump, and fasd) learns your most-used directories:

Running Tests from Neovim

vim-test / neotest

neotestarrow-up-right runs tests with results shown inline:

Summary

Vim's philosophy of composing small, sharp tools shines through its external command integration. By piping buffer text through shell utilities and using Neovim's built-in terminal, you gain access to the entire Unix ecosystem without leaving your editor. Whether you are formatting JSON with jq, sorting data with awk, or running a full interactive shell in a floating window, these techniques keep you in flow.

Key takeaways:

  • The filter command :%! pipes buffer contents through any external command, combining Vim's text manipulation with Unix tool power.

  • Neovim's built-in terminal runs inside buffer windows; use <C-\><C-n> to escape to Normal mode and standard window commands to navigate.

  • toggleterm.nvim adds floating, horizontal, and vertical terminal modes with a single toggle keymap, plus support for dedicated tool terminals like Lazygit.

  • Tools like ripgrep, fd, fzf, and zoxide integrate naturally with Vim's workflows and can be used both from the command line and within the editor.

Exercises

  1. Format JSON with an external command -- Paste or create some unformatted JSON in a buffer, then use an external command to pretty-print it.

  2. Sort and deduplicate lines -- Create a buffer with several duplicate and unsorted lines, then sort them and remove duplicates using an external command.

  3. Read command output into a buffer -- Insert the current date, a directory listing, or the output of a custom script into your buffer at the cursor position.

  4. Use AWK to transform tabular data -- Create a simple table of data in your buffer and use AWK to extract specific columns, calculate values, or reformat the output.

  5. Toggle a floating terminal and run a project command -- Configure and use toggleterm.nvim to open a floating terminal, run a build or test command, then dismiss it.

Last updated

Was this helpful?