Vim Terminal mode

Published December 14, 2022

Reading time: 2 minutes.


I’m a big Vim fan. I use it for most of my writing and all of my software development. It’s customizable and extensible, and most of all, it’s light-weight.

Vim 8 has a terminal mode which is great if you do content around CLI tools. It makes it so easy to snag the output of programs so you can paste that output into your work. I’ve actually used it to run several commands and then turn that into a tutorial.

To activate Terminal mode, use the :terminal or :term command. Vim will open a new window where you can run any CLI command you want.

You can use CTRL+d or type exit like a normal Terminal, but that also closes it, meaning you can’t snag its content and use it as a reference.

Unfortunatley, exiting Terminal mode is awkward if you wish to keep the text from the session.

A quick glance at the :h Terminal-mode command shows that there are a couple of commands you can run:

Use CTRL-W N (or ’termwinkey’ N) to switch to Terminal-Normal mode. Now the contents of the terminal window is under control of Vim, the job output is suspended. CTRL-\ CTRL-N does the same.

So, you’ll have to press Ctrl+w followed by N (That’s an uppercase N) to get back to Normal mode where you can edit the text as if it were a regular file. The Ctrl+\ Ctrl+n keybaord combination also works, but if you’re like me and use tmux and Vim with some window navigation configuration options, that key combination won’t work.

To avoid this awkwardness, map it to the Escape key by adding the following line to your .vimrc file:

tnoremap <Esc> <C-w>N

The tnoremap sets keybindings only for Terminal mode, leaving your other bindings alone. The only drawback is that you won’t be able to use the Escape key in your Terminal. You could modify this to use your leader key instead, which is what I do:

tnoremap <Leader><Esc> <C-w>N

Now I can switch to Normal mode with my Leader key, which I’ve set to the comma, followed by the Escape key.

I hope you found that useful.


Thanks for reading

I don't have comments enabled on this site, but I'd love to talk with you about this article on BlueSky, Mastodon, Twitter, or LinkedIn. Follow me there and say hi.


Liked this? I have a newsletter.