Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Neovim's terminal emulator

#74

Run time:

Neovim lets you run a terminal emulator inside of a buffer. In this video, we’ll cover some of the basics of how terminal buffers work, and how you can use them alongside regular buffers in your workflow.

Shownotes

The :terminal command

In Neovim, you can open a terminal buffer using the command:

:terminal

By default, that will open a terminal buffer running your default shell. Alternatively, you can specify the program that you want to run when in the terminal buffer by providing an argument. For example, to run top or launch a rails server:

:terminal top
:terminal rails server

You can’t modify the text in a terminal buffer directly. Instead, the text is updated asynchronously by the program running inside the terminal buffer. You can interact with the program running inside the terminal buffer by switching to Terminal Mode.

A new mode: Terminal Mode

Vim users are accustomed to spending most of their time in Normal mode, with brief forays into Insert mode, Command-line mode, Visual mode, and other modes. In Neovim, you get a new mode to play with: Terminal mode. In this mode, you can interact with programs that run inside the built-in terminal emulator.

Terminal mode is only available in terminal buffers, where you can activate the mode by pressing i, a, I, or A. In a regular text buffer, these same commands would switch to Insert mode, but that mode is not available in a terminal buffer.

In Terminal mode: all keystrokes are forwarded to the underlying program. There’s one single exception to that rule: pressing <C-\><C-n> switches you back to Normal mode.

In the same way that nnoremap lets you create mappings for Normal mode, inoremap lets you create mappings for Insert mode, and so on, you can also create mappings for Terminal mode using tnoremap. That’s what makes Terminal mode truly a mode We’ll explore this capability in greater detail in another episode of Vimcasts.

Using Normal mode in a terminal buffer

In a terminal buffer, most Vim commands work just fine. You can move the Normal mode cursor around using motions such as h, j, k, l, w, b, etc. You can scroll the screen using <C-f>, <C-b>, <C-d>, and <C-u>. If the terminal buffer contains a filepath, you can place your cursor on it and use gf to open that file in a regular buffer. You can use y and p to copy and paste text in a terminal buffer (we’ll explore this in more detail in another episode).

Any command that would normally edit text in a buffer won’t work in a terminal buffer. For example, if you try running dd, or cw, or x, Neovim will show the error message: E21: Cannot make changes, 'modifiable' is off. I find that it helps to think of terminal buffers as being read-only, from Neovim’s perspective.

Killing processes in terminal buffers

There are various ways that you can kill the process running in a terminal buffer. The best way is to run :bdelete! [N] (the ! is required), where N is the number of the buffer you want to delete. You can find out the buffer number by inspecting the output from the :ls command.

Alternatively, you can activate the terminal buffer and switch to Terminal mode, then send a kill signal (or interupt signal) directly to the program. In many cases, <C-c> will do the trick, but some programs handle this differently.

Finally, when you quit Neovim, any remaining terminal buffers will be wiped out, causing their associated programs to exit.

What about the :terminal in Vim 8?

This video covers the :terminal feature in Neovim. Since I recorded this video, work has started to add a :terminal command to Vim 8. This has some similarities with the Neovim implementation and some important differences. Be warned: I don’t expect the instructions given here to work in Vim 8.

The last time I tried using the :terminal in Vim 8, it was a long way from being ready. I’m watching the development of that feature with interest.

Further reading

Comments

Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..