Tip 18Using Normal Mode Commands in a Terminal Buffer
images/neovim-only.png

Most Normal mode commands work in terminal buffers just as they would in regular text buffers. In this tip we’ll run through some examples of the commands that are useful in this context.

Preparation

The source code that accompanies this book includes a terminal directory. Switch to that directory, then start Neovim with a terminal buffer:

=> $ cd code/terminal
=> $ nvim +terminal

This last command is equivalent to running nvim to launch Neovim, then running the :terminal Ex command after Neovim has launched (see :h -+c). The examples throughout this tip should be run within this internal shell.

Copying and Pasting

Just like in regular buffers, you can copy text from a terminal buffer into any of Vim’s registers. You can also paste the contents of a register into a terminal buffer, although the result depends on which program is running in the terminal buffer.

In the code/terminal directory, you’ll find a nvim-setup-instructions.md file. Press i to switch to Terminal mode, then print the contents of that file using cat:

=> » cat nvim-setup-instructions.md
<= Run the following commands:
 
 * `mkdir -p ~/.config/nvim`
 * `touch ~/.config/nvim/init.vim`
 
 These commands are safe if `init.vim` already exists.

Now switch back to Normal mode and move the cursor to the line with the mkdir instruction. Pressing yi` copies the text inside the backticks into Vim’s unnamed register, then you can paste that text at the location of the terminal cursor using p. To execute that command, you have to switch to Terminal mode then press <CR>. Repeat the same steps to copy and paste the touch instruction.

The copy and paste commands work more or less as you would expect. You can prefix the yank and put commands with a named register (for example, "a), or you could use a special register such as "* to reference the system clipboard.

There’s one significant difference in behavior to watch out for, though. In a text buffer, the p command inserts text next to the location of Vim’s cursor; however, in a terminal buffer, the p command always inserts text at the location of the terminal cursor. This makes sense when you consider that terminal buffers are not modifiable.

Scrolling

In a terminal buffer, you can scroll up and down using familiar Normal mode commands.

In the code/terminal directory, you’ll find a lorem-ipsum.txt file. If you print the contents of that file in your shell, it should fill the screen with more text than you can view all at once:

=> » clear
=> » cat lorem-ipsum.txt
<= Lorem ipsum dolor sit amet...
 ...

Quickly jump back to the top of your scrollback using the gg command. Jump to the bottom again using the G command. If there’s a program running in the shell that continually outputs text, the bottom of the buffer becomes a moving target. Pressing the G key causes the buffer to automatically scroll so that the last line is always visible.

In Normal mode, the j and k commands move Vim’s cursor down and up one line at a time. You might be tempted to use this to scroll the screen, but instead I recommend using the <C-e> and <C-y> commands. These scroll the buffer down and up one line at a time. The table summarizes these and several other useful Normal mode commands for scrolling.

CommandEffect

gg

Jump to top of scrollback

G

Jump to bottom of scrollback

<C-y>

Scroll up one line

<C-e>

Scroll down one line

<C-u>

Scroll up half a page

<C-d>

Scroll down half a page

<C-b>

Scroll up a page

<C-f>

Scroll down a page

Use Vim’s standard search commands to find patterns in the shell’s scrollback. For example, suppose you need to find occurrences of the word “duo” in the shell’s scrollback. You can do this by running: /duo<CR>, which moves the cursor to the next match. (Try it! The “lorem ipsum” sample contains a few matches for that pattern.) Then, use n to repeat the search or N to reverse it.

Jumping to a Filepath

When the cursor is positioned on a filepath, you can use the gf command to open that file in a buffer. This works in a terminal buffer just as you would expect. Let’s try it out. Use the find command to output a list of filepaths:

=> » pwd
<= /Users/drew/modvim/code/terminal
=> » find $PWD
<= /Users/drew/modvim/code/terminal
 /Users/drew/modvim/code/terminal/lorem-ipsum.txt
 /Users/drew/modvim/code/terminal/nvim-setup-instructions.md
 /Users/drew/modvim/code/terminal/readme.md
 ...

Switch back to Normal mode and position your cursor on one of the absolute filepaths, then use the gf command. That opens the specified file in a buffer, which takes over the current window. The terminal buffer is now hidden, but you can quickly switch back to it using the <C-^> command (:h ctrl-^).

This is really handy when running a failing build or test suite. If the output includes the filename (and line number) where the failure originated, just use gf to investigate.

Using Normal Mode Operations to Edit a Command Line in the Shell

You might be tempted to use Normal mode commands to edit the current command line. This doesn’t work. Remember, the text in a terminal buffer cannot be modified by Normal mode commands. Instead, switch to Terminal mode and then invoke <C-x><C-e>, which opens the current command line in your preferred editor. See Tip 22, Using an Existing nvim Instance as the Preferred Editor for more details.

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

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