Mastodon hachyterm.io

I’ve been toying around with tmux today.

tmux is a terminal multiplexer. It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal.

My terminal of choice is kitty, a GPU-based terminal emulator with tons of options.
kitty even offers windows and tabs, so I didn’t need to use tmux for creating new terminal panes.

My normal workflow looked like this: Navigate to a project, open NeoVim in that project. Discard Vim to the background by typing CTRL+Z and run terminal commands. Then switch back to Vim with fg.

The process became sluggish when the terminal command took some time to finish. Then I had to open a new terminal window that didn’t have my Vim session.

Maybe tmux can help?

Installation (Arch Linux)

pacman -S tmux

Useful aliases:

alias t="tmux"
alias ta="t a -t"
alias tls="t ls"
alias tn="t new -t"

For example, tn my-project starts a new tmux session called “my project”. See cheatsheet.

Configuration

You can customize tmux with a configuration file: ~/.tmux.conf.

I recommend this guide for a more detailed tutorial on getting started with tmux: Jaime’s Guide to Tmux: The Most Awesome Tool You Didn’t know you needed.

Here are some basic options that make Tmux work well with Vim:

  1. Install tpm, the tmux plugin manager:
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
  1. Configure tmux

Add these lines to ~/.tmux.conf:

## Plugin management
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'christoomey/vim-tmux-navigator'
run -b '~/.tmux/plugins/tpm/tpm'

Here we initialize tpm, add some sensible defaults and vim-tmux-navigator.
If you add other customizations, do that before the tpm plugin setup.

  1. Configure (Neo)Vim:

Add vim-tmux-navigator to your plugins.

For example, with Vundle:

Plugin 'christoomey/vim-tmux-navigator'

To install the plugin with Vundle, run :PluginInstall.

Create new Vim key mappings for switching between Vim and Tmux panes (inside ~/.vimrc or similar).

" TMUX
let g:tmux_navigator_no_mappings = 1
nnoremap <silent> <c-k> :TmuxNavigateUp<cr>
nnoremap <silent> <c-j> :TmuxNavigateDown<cr>
nnoremap <silent> <c-h> :TmuxNavigateLeft<cr>
nnoremap <silent> <c-l> :TmuxNavigateRight<cr>
" Disable tmux navigator when zooming the Vim pane
let g:tmux_navigator_disable_when_zoomed = 1

Make sure that you don’t have conflicting key bindings in your configuration file.
I had old key mappings for navigating between Vim buffers that conflicted with the new setup.

  1. Start tmux

Start tmux from the terminal with tmux or t. Open a new pane in the session with <Ctrl+b>% or <Ctrl+b>".

(<Ctrl+b> is the default tmux “prefix”, a keyboard combination that prefaces tmux commands. You can change the prefix, if you like.)

You can open Vim in one pane and even open several buffers inside Vim.

You can navigate between Vim buffers and between tmux panes with <Ctrl+h/j/k/l>.

If you want to maximize a tmux pane, type <Ctrl+b>z.

Further Reading