Want to see the full-length video right now for free?

Sign In with GitHub for Free Access


emacs cheatsheet:

M-f       Move forward one word
M-b       Move backward one word
M-l       Convert next word to lowercase
M-u       Convert next word to uppercase
C-x C-s   Save the file
C-x C-c   Quit

I'm running a stock bash shell here with very little customisation. Here's a neat feature from bash: if I type out a command line and press control-x followed by control-e, it opens up a text editor containing that command line. In this case, the default editor is emacs. I can modify the command line here, then when I save the file and exit, bash executes the command as it was saved in the text editor.

Some command-line tools that expect text input can be configured to open a text editor. For example, if I run git commit:

git commit
error: unable to start editor ''

I get an error message: "Unable to start editor".

We can specify our preferred text editor by setting the VISUAL variable. Let's set this to use Neovim:

export VISUAL='nvim'

Now if I run git commit, it launches Neovim. And if I use the control-x-control-e mapping, that too launches Neovim.

So far that's pretty straightforward. Now let's complicate matters by launching a terminal emulator inside of Neovim:

nvim +terminal

Our VISUAL variable is still set to nvim.

echo $VISUAL

In this context, if I type out a command line and use the control-x-control-e mapping, it launches a fresh nvim instance inside of our existing nvim instance. I find this confusing, so I try to avoid getting into this scenario.

Wouldn't it be cool if we could set things up so that bash would open a buffer in the existing nvim instance?

[quit the existing nvim instance]

Let me introduce a tool that makes this possible: neovim-remote, by Marco Hinz. This can be installed using pip:

pip3 install --user neovim-remote

Although the package is called "neovim-remote", the executable is shortened to nvr. Let's check it's installed by looking up the help:

nvr -h | less

Ok, it looks like we're all set.

Let's launch a fresh instance of Neovim with a terminal emulator.

nvim +terminal

In this context, we can use the nvr executable to open files in the current instance of Neovim:


By default, that replaces the current Vim window.

[use <c-^> to switch back to terminal buffer]

We can use the dash-oh flag to open in a split window:

nvr -o

Or dash-big-oh to open in a vertical split window:

nvr -O

If we set our $VISUAL variable to nvr, then we can make bash use the current nvim instance whenever it tries to invoke a text editor. We'll make nvr open the buffer in a split window. And we'll use the --remote-wait option, which tells nvr to block until the buffer gets deleted.

export VISUAL='nvr -cc split --remote-wait'

Let's try using the control-x-control-e mapping to edit a command line. Now we've got a new split window containing the command line in a temporary file. Notice that in our terminal buffer, the nvr command has blocked: the command line won't be evaluated until our temporary buffer is deleted.

It's not enough to write and quit our temporary buffer:


If we inspect the buffer list, you'll see that it's still listed:


It's only when I delete the buffer that our terminal becomes unblocked and executes the command line.

:bwipe 2

It would be more convenient if the temporary buffer were deleted when we closed the window containing it. We can acheive this by setting the bufhidden option to wipe:

:help bufhidden

Let's modify our $VISUAL variable so that this option is set for the temporary buffer:

export VISUAL="nvr -cc split --remote-wait +'set bufhidden=wipe'"

This time, when we use control-x-control-e to edit a command line, the buffer is automatically wiped as soon as we close it, which causes the command line to be executed. That means we can use :wq or zee-zee to save and execute our command line. That's pretty neat!

Let's open up the bashrc file and set our preferred text editor there. If the $NVIM_LISTEN_ADDRESS variable is set, we can assume that bash is running inside of a neovim terminal emulator, and we'll use nvr as our preferred text editor. Otherwise, we'll just launch a new nvim instance.

Now let's source our bashrc file. In this context, control-x-control-e launches a fresh nvim instance. But if we run bash inside of a Neovim terminal emulator, the same mapping opens the command line in the current nvim instance. The same is true also for git commit, and most other commands that open a text editor.