Improving the vim terminal development environment

Since I started working at Google, I've been doing almost all my development using vim through the terminal. Since my last post on the subject (over a year ago - bad blogger), I've discovered some significant improvements to what I recommended there, and I thought I'd share what I've learned.

The biggest improvement is to switch to iTerm2 as my terminal - thanks to Ross for the recommendation. Not only does it remove the need for the TerminalColors hack, it also supports mouse events natively - so the MouseTerm hack is no longer needed. Plus, it includes support for control codes that change the cursor shape, so it is now simple to get vim to show whether it's in normal or insert mode. Just add these lines to .vimrc:

let &t_SI = "\<Esc>]50;CursorShape=1\x7"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"

The latest version of iTerm2 has another trick up its sleeve. It integrates seamlessly with a custom version of tmux, the terminal multiplexer that's an alternative to GNU Screen. Using any mulitplexer allows you to connect to a remote server with a single connection, and open as many tabs/windows to that connection as you like - and, what's more, you can detach your session when you log out and re-attach later, bringing your terminals back exactly how you left them. Useful enough, but what iTerm2 adds is native support for tmux tabs and windows - so that they open as tabs/windows on the Mac desktop, and you can switch between them using normal Mac shortcuts rather than tmux-specific ones. Very useful: it involves compiling a custom version of tmux and some associated libraries on the server, but it's well worth it.

One missing element was the ability to cut-and-paste directly from vim into apps running on the Mac. This was tricky to get right.

The basic principle is for the X clipboard to be shared with the OS X pasteboard, and then to to get vim to yank/delete to that clipboard. To get this working, I needed to run an X server on my Mac and enable clipboard sharing - it needs to be running for this, even if you're not using any X applications. There is an X11 app supplied with OS X, but (on Snow Leopard at least) it didn't seem to support the correct settings. Although it might be possible to get it working with some use of defaults write, the easier solution is to use the open-source XQuartz project - it's the same as the supplied, but updated more frequently. The version that Google had, luckily, already installed on my machine (2.7.0) includes a Pasteboard tab on the Preferences dialog which controls syncing between Mac Pasteboard and X clipboard - you need to tick "Enable syncing", "Update Pasteboard when CLIPBOARD changes", and "Update CLIPBOARD when Pasteboard changes".

The next step is to ensure that the SSH connection to the development machine uses X forwarding, which simply means using ssh -X when connecting. If the remote machine/VM is headless, it may not have any of the X stuff installed: installing the xauth package is probably enough to bring in the necessary dependencies, but I'm definitely not an expert here.

Now, in vim, you can yank to the X clipboard by using the + register, using "+y - I now have this mapped to Ctrl-C, which is close enough to the Mac's default of Cmd-C that I find it easy to remember. In bleeding-edge versions of vim later than 7.3.74, there is actually an additional option that you can use so that all yanks go directly to the X clipboard: set clipboard=unnamedplus. But I found that this ignores the difference between line-wise and character-wise selection, treating everything as character-wise, so cutting and pasting full lines inside vim becomes unnecessarily annoying. Better to learn the extra shortcut for those times when I explicitly need to copy out of vim.

One final update to my last post: I talked there about switching between tabs, but I've now stopped using tabs altogether. Seriously, folks, if you're using vim, learn about buffers: they are great. Especially with the latest version of Command-T which allows you to use the same fuzzy-matching search to switch between open buffers using <leader>b. Another advantage of buffers over tabs is that it allows you to have multiple windows open simultaneously in the same session: Ctrl-w v splits the main window horizontally, and Ctrl-w w switches between open windows (I've actually remapped Tab in normal mode to this). With a large monitor coupled with Google's extremely strict 80-character limit, you can easily have two or three files next to each other. A real productivity boost.

Comments !