Vim and Tmux on your Mac

Setting up your computer for Vim and Tmux often comes with a few issues. Here's how to manage plugins for Vim and use Tmux to boost your productivity, as well as settle a few common issues.

We'll install:

  1. Vundle - Plugin management for Vim
  2. Solarized color scheme
  3. Tmux - Terminal multiplexer

We'll also figure out some issues Mac and Tmux have with showing 256 color themes.

Install Vundle

You can install Vundle by cloning it:

$ git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle

Configure Vundle

Once installed, you can configure it.

And follow the installation directions by editing your ~/.vimrc file:

set nocompatible
filetype off    " Required

set rtp+=~/.vim/bundle/vundle/
call vundle#rc()

Bundle 'gmarik/vundle'    " Required

filetype plugin indent on " Required

Once this is setup, you can open a new instance of vim and run :BundleInstall:

$ vim # Any file will do
:BundleInstall

You'll get a confirmation "Done" message on the bottom of your screen if all goes well.

Add Solarized Color Theme

Next, we'll add the Solarized color theme. Vundle makes this really easy. Simply add the Github repository name in ~/.vimrc:

Bundle 'gmarik/vundle'  " We saw this before

Bundle 'altercation/vim-colors-solarized'  " New line!!

" Some settings to enable the theme:
set number        " Show line numbers
syntax enable     " Use syntax highlighting
set background=dark
colorscheme solarized

Now that we've added the solarized theme, we need to have Vundle install it.

$ vim
:BundleInstall

Now open up your Terminal (iTerm2 or Terminal app). If you're on a Mac, you may see something...ugly. I had a terrible background and the colors were completley off. I didn't have 256 color enabled.

Here's what I saw: Exhibit A

To fix that, I finally came across this StackOverflow question, which had the answer waiting.

Change your ~.vimrc settings from above to the following:

" Some settings to enable the theme:
set number
syntax enable
set background=dark
let g:solarized_termcolors = 256  " New line!!
colorscheme solarized

Once you start up a new instance of Vim, you should see your new, colored vim!

Exhibit B

Onto Tmux!

Let's up our game and get Tmux into the mix. We can use Tmux to open up multiple "panes" within our shell.

First, install it. On your Mac, you can use Homebrew:

$ brew install tmux

Ubuntu or Debian users can user apt-get:

$ sudo apt-get install tmux

Great. Now, colors are also an issue when running Vim within Tmux. Let's fix that. Create or edit the file ~/.tmux.conf:

$ vim ~/.tmux.conf
> set -g default-terminal "screen-256color"

Now we're ready to use Tmux. Start up a new Tmux session:

$ tmux

Next, split the screen vertically so we have 2 panes with this keyboard shortcut:

Ctrl-b %

You can switch between panes with this shortcut:

Ctrl-b o

You can then open up separate files in each! (Or do ... anything really).

tmux

More Tmux

Tmux has Windows, and within the Windows it has Panes. Each Window consists of a set of 1-n Panes.

Tmux also has Sessions. A collection of Windows/Panes live within a Session. You can detach from a Session, leaving it running in the background. You can later re-attach to it, and continue working. This is how people pair program.

Split Screen into 2 Panes:
Ctrl-b %
Split current Pane horizontally into 2 Panes:
Ctrl-b "
Switch between Panes:
Ctrl-b o
Create new Window:
Ctrl-b c
Switch between Windows:
Ctrl-b n  # next
Ctrl-b p  # previous
Detach from Session:
Ctrl-b d
Re-attach to a Session:
tmux attach -t [session-name]
Create a Session:
tmux new -s [session-name]
Switch between Sessions:
tmux switch -t [session-name]
Switch between Sessions within Tmux:
Ctrl-b (      # previous session
Ctrl-b )      # next session
Ctrl-b L      # ‘last’ (previously used) session
Ctrl-b s      # choose a session from a list
List Sessions:
tmux list-sessions
List all commands:
Ctrl-b ?    

Scripted!

You can run this bash script on your Debian or Ubuntu server to run the above Vim+Tmux configurations. This might conflict with anything you currently have in your ~/.vimrc or ~/.tmux.conf files (If they already exist). Back them up first.

Further Reading: