Tip 17Managing Windows That Contain Terminal Buffers
images/neovim-only.png

Neovim’s splits and tab pages can display terminal buffers in just the same way that they display regular text buffers. That opens up a new possibility: you can use Neovim as a simple window manager not just for text files, but for any programs that you can run in a terminal.

Open up a simple text file in Neovim:

=> $ cd code/terminal/
=> $ nvim readme.md

Now open a new terminal buffer running your shell:

=> :terminal

Notice that the :terminal command takes over the current window, and the buffer containing readme.md is hidden. This is similar to how the :edit {file} command works.

Opening Terminal Buffers in a New Window

If you want to open a terminal buffer in a new window, you could do so by running these two commands:

=> :split
=> :terminal

Alternatively you could use this one-liner, which has exactly the same effect:

=> :split | terminal

In Ex commands, the | character behaves as a command separator (:help :bar). You could use the same technique to open a terminal buffer in a vertical split, or in a new tab page. The following table summarizes how these work.

CommandEffect

:terminal {cmd}

Terminal buffer is created in the current window

:split | terminal {cmd}

Terminal buffer is created in a horizontal split

:vsplit | terminal {cmd}

Terminal buffer is created in a vertical split

:tabedit | terminal {cmd}

Terminal buffer is created in a new tab page

If you omit the | character, the meaning of these commands is totally different. For example, :split terminal creates a text buffer called terminal, and if you :write it, you’ll end up with a file on disk called terminal!

Easy Window Switching

Open your vimrc file in a fresh tab page, then open a new terminal buffer in a vertical split:

=> :tabedit $MYVIMRC
=> :vsplit | terminal

Your workspace is now divided into two windows, with a terminal buffer on the left and your vimrc file on the right. In Normal mode, you can use <C-w>h and <C-w>l to switch between the left and right windows. You can also use <C-w>j and <C-w>k to switch down and up, if your workspace contains horizontal splits.

Press <C-w>h to activate the split window containing the terminal buffer, then switch to Terminal Mode by pressing i. In this context, how do you switch to the other window containing your vimrc file?

First, you need to switch back to Normal mode, which means pressing <C-><C-n>, then you can use <C-w>w to switch windows. That’s four keystrokes in total. Surely we can do better than that? Try putting this in your vimrc file and reloading it:

 nnoremap <M-​h​> <​c​-​w​>​h
 nnoremap <M-​j​> <​c​-​w​>​j
 nnoremap <M-​k​> <​c​-​w​>​k
 nnoremap <M-​l​> <​c​-​w​>​l
 if​ has(​'nvim'​)
  tnoremap <M-​h​> <​c​-><​c​-​n​><​c​-​w​>​h
  tnoremap <M-​j​> <​c​-><​c​-​n​><​c​-​w​>​j
  tnoremap <M-​k​> <​c​-><​c​-​n​><​c​-​w​>​k
  tnoremap <M-​l​> <​c​-><​c​-​n​><​c​-​w​>​l
 endif

The tnoremap command lets us create a mapping that applies only in Terminal mode. With these mappings defined, it doesn’t matter if you’re in Normal mode or Terminal mode, you can switch to another window by pressing <M-h>, <M-j>, <M-k>, or <M-l>. For the sake of completeness, you might also want to define equivalent mappings for Insert mode and Visual mode.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.239.148