Screen Too Small

This course material requires a larger screen to properly display the terminal and editor interfaces.

Please use a tablet, laptop, or desktop computer for the best learning experience.

You can continue browsing, but the experience will be significantly limited.

7/9
Free Trial: 4 lessons remaining
Unlock Full Access

Navigating Neovim and Tmux

Estimated time: 5 minutes

So how do we get to navigate between both Neovim and Tmux? The keybindings are different. In this video, we're going to dive into how to make them seamlessly work together, how to easily run tests and to have sensible session management.

Let's get after it.

Currently, we use our Ctrl key with and vim motion keys (LDUR). This works great for moving between navigating Tmux panes but how does Neovim know how to jump from a Neovim pane to a Tmux pane? Currently, we'd have to use our leader prefix (Ctrl+s) and then the vim motion (LDUR) keys.

Nobody wants to deal with that. So how do we solve it?

Enter vim-tmux-navigator. (https://github.com/alexghergh/nvim-tmux-navigation).

On the tmux side of things, we can install the plugin as seen here.

set -g @plugin 'christoomey/vim-tmux-navigator'

Add vim-tmux-navigator to tmux.conf

Once this is here, you can reload your configuration and then install.

Reload: (Ctrl + s) + r

Install new plugin: (Ctrl + s) + I

So this solves our problem in Tmux, but what about Neovim? Right now, if you have split vim panes, you'd have to use (Ctrl + w) + vim motion key (LDUR). Well that won't do.

Now we're going to add some keymaps in Neovim. With these added to your keymaps in Neovim, you'll be able to navigate between neovim panes with a simple Ctrl + vim motion (LDUR) keystrokes. When you try this, you'll notice that it doesn't work navigating out of Neovim into Tmux. We're solving that next.

vim.keymap.set('n', '<c-k>', ':wincmd k<CR>')
vim.keymap.set('n', '<c-j>', ':wincmd j<CR>')
vim.keymap.set('n', '<c-h>', ':wincmd h<CR>')
vim.keymap.set('n', '<c-l>', ':wincmd l<CR>')

Tmux — I, Neovim, See You

Now if you've followed along our Neovim for Newbs, you'll know exactly what to do when installing a vim plugin using Lazy. Let's do that quickly here to solve this navigation problem.

First, we'll open up Neovim in our configuration located at ~/.config/nvim. We'll want to add a new file under the plugins directory. To do this, we'll open up Neotree and add the file.

Ctrl+n to open Neotree. Navigate to the plugins folder and press a for add. Type vim-tmux-navigator.lua and hit enter.

Voila - we've got our file.

Now we're going to add the necessary configs.

return {
  "christoomey/vim-tmux-navigator",
  vim.keymap.set("n", "<C-h>", ":TmuxNavigateLeft<CR>"),
  vim.keymap.set("n", "<C-j>", ":TmuxNavigateDown<CR>"),
  vim.keymap.set("n", "<C-k>", ":TmuxNavigateUp<CR>"),
  vim.keymap.set("n", "<C-l>", ":TmuxNavigateRight<CR>")
}

This adds the plugin and defines some keymaps that will translate to navigating tmux panes as well.

We can close and reopen Neovim, open a Tmux pane using (Ctrl+s) + " and try navigating around with Ctrl + vim motion keys (LDUR).

And it works!

Next we're going to learn about how to accelerate your TDD workflow in any project leveraging everything we've learned.

You've completed this lesson!

You completed this lesson less than a minute ago.