Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 36 ยท Workflows

Vim and the Terminal

Work between Vim and the shell: run commands with :!, read output with :r !, filter lines through programs with !, use :terminal, :make, suspend with Ctrl-z and pair Vim with tmux.

Vim is at its best when it cooperates with the tools around it. The Unix shell already has sort, jq, awk, fmt, compilers and test runners; Vim can send text to them and take results back. This lesson covers every bridge, from one-off commands to a full terminal inside the editor.

Running a command: :!

:!command runs a shell command and shows its output, then asks you to press Enter.

VIM
:!ls -l
:!python3 %
:!gcc % -o app && ./app

In the command line, % stands for the current file name, # for the alternate file, and modifiers refine them: %:p is the full path, %:h the directory, %:t the tail (file name), %:r the name without extension, %:e the extension. :! alone repeats the last command; :!! does too.

Characters like %, # and ! are special, so escape them with a backslash when you need them literally in a shell command.

Reading output into the buffer: :r !

VIM
:r !date
:r !ls src
:0r !cat header.txt

:r !cmd inserts the command's output below the cursor line. A line number first, as in :0r, chooses where (0 is above the first line). :r file inserts a file without a shell.

Sending text out: :w !

:w !cmd pipes the buffer (or a range) to a command's standard input without changing the file. :w !sudo tee % > /dev/null is the famous way to save a file you opened without permission. :'<,'>w !pbcopy copies the visual selection to a clipboard tool.

Filters: replace text with a program's output

A filter sends a range through a command and replaces it with the result. Use ! as an operator or on the command line:

  • !ip then type sort -u and Enter: filters the current paragraph (ip) through sort -u.
  • !! filters the current line; !5j filters six lines (current + 5 below).
  • :%!sort sorts the whole buffer; :'<,'>!sort -n sorts the selection numerically.
  • :%!jq . pretty-prints JSON; :%!python3 -m json.tool does too.
  • :%!column -t aligns columns; :%!fmt -w 72 re-wraps paragraphs.

When the command fails, Vim shows the error text in place of your lines; press u to undo.

JS Stand-in: a filter over a line range

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Building: :make and friends

:make runs the program in makeprg (default make) and fills the quickfix list from its output through errorformat. Set them per language:

VIM
autocmd FileType python setlocal makeprg=python3\ %
autocmd FileType c      setlocal makeprg=gcc\ -Wall\ -o\ %<\ %

Then :make, :copen, :cn take you through the errors. :compiler gcc loads a ready-made pair of makeprg and errorformat.

The built-in terminal

Vim 8.1+ and Neovim have :terminal (:term), a terminal emulator in a window:

  • :term starts your shell in a horizontal split; :vert term in a vertical one; :term python3 runs a specific program.
  • Neovim starts in Normal mode of the terminal buffer; press i to type into the shell. Leave terminal-input mode with Ctrl-\ Ctrl-n in Neovim (in Vim, Ctrl-w N).
  • The scrollback is a normal buffer, so you can use /, y, and gg on past output.

A mapping makes Esc easier (Neovim): vim.keymap.set("t", "<Esc><Esc>", "<C-\\><C-n>").

Suspending and shells

  • Ctrl-z suspends Vim and returns to the shell; fg brings it back with all your state. It is the quickest way to run a command and return.
  • :shell or :sh starts a subshell; exit returns.
  • vim - reads standard input: git log --oneline | vim - lets you search and edit a command's output. Add -R for read-only; view is Vim in read-only mode.
  • Set $EDITOR and $VISUAL to vim or nvim so git commit, crontab -e and other tools open it.
  • :cd, :lcd and :tcd change the working directory, which :! commands use; :pwd shows it.
  • Editing remote files: vim scp://user@host//path/file uses netrw; for heavy remote work run Vim on the server.

Vim with tmux

A terminal multiplexer such as tmux or Zellij keeps shells, servers and Vim in one window with detachable sessions. A typical layout: Vim on the left, a test runner on the right, one more pane for Git. Many people leave Vim's own splits for code and use tmux for everything else. Both have vi-style navigation if you ask for it (see Vi keys everywhere). Plugins can make Ctrl-h/j/k/l move seamlessly between Vim windows and tmux panes.

JS Stand-in: expand % modifiers in a command

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does :%!sort do?

    :range!cmd filters the range through the command and replaces it with the output. Undo with u.
  2. 2. Which command inserts the output of date below the cursor?

    :r !cmd reads command output into the buffer. :!cmd only displays it.
  3. 3. What does % mean inside a :! command?

    % expands to the current file name; modifiers like %:p and %:r refine it.
  4. 4. How do you return to your shell and later come back to the same Vim session?

    Ctrl-z suspends Vim as a background job; fg resumes it.

Summary

  • :!cmd runs a command, :r !cmd reads its output in, :w !cmd sends text out.
  • Filters (!ip, :%!sort) replace a range with a program's output.
  • :make plus makeprg and errorformat fills the quickfix list.
  • :terminal gives a terminal in a window; Ctrl-z, $EDITOR and tmux complete the picture.
  • %, %:p, %:h, %:t, %:r, %:e name the current file.

Next: Vim and Git