Robot Has No Heart

Xavier Shay blogs here

A robot that does not have a heart

Vim and tmux on OSX

I recently switched from MacVim to vim inside tmux, using iTerm in full screen mode (Command+Enter). It’s pretty rad. I tried screen first, but even after a lot of screwing around there was still a lot of brokeness, and I don’t like how it does split panes anyways. Follows are some notes about what is required for tmux.

Get the latest vim and tmux

Latest vim required for proper clipboard sharing, if you don’t want to install it you can use the pbcopy plugin mentioned below.

1
2
brew install --HEAD vim
brew install tmux

Set up pretty colors

my vim/tmux setup

I use the solarized color scheme. To make this work, ensure you are not overriding the TERM variable in your .{bash|zsh}rc, then create an alias for tmux:

1
2
# .zshrc
alias tmux="TERM=screen-256color-bce tmux"

I also have a tmux config:

1
2
# .tmux.conf
set -g default-terminal "screen-256color"

Clipboard sharing

Up until I wrote this blog post, I had been using the pbcopy plugin to share clipboard using a cute hack involving ssh’ing back into your machine to run pbcopy/pbpaste. In researching some more details on this though I found an excellent write up of the problem and a far better solution by Chris Johnsen that enables proper sharing without ssh’ing, and therefore also the * register (use "*y to copy, "*p to paste – note this does not work with the vim that ships with OSX).

Mouse integration

The mouse is good for two things: scrolling, and selecting text from your scrollback.

For the first, put the following config:

1
2
# ~/.tmux.conf
set -g mode-mouse on

For the second, hold the option key while you select.

Workflow

Find another reference for basic keys, this here are notes on top of that. Ctrl-B sucks as an escape sequence, rebind it to Ctrl-A to match screen. Most online references don’t mention it, but the default binding for horizontal split is prefix " (it’s in the man page). I tend to have a main pane for editing and a smaller pane for a REPL or log. If I need to investigate the smaller pane, I press Ctrl-A Ctrl-O, which switches the two panes to give me the log in the larger one.

I use the tslime.vim plugin to send text directly from vim to the supplementary pane. This is a killer feature. As well as the built in Ctrl-C shortcut, I also use a trick I learned from Gary Bernhardt and remap <leader>t on the fly to send whatever command I am currently testing to the other pane. Some examples:

1
2
3
4
; Load a file into a clojure repl
:map ;t :w\|:call Send_to_Tmux("\n\n\n(load-file \"./myfile.clj\")\n")<CR>
; Run rspec in zsh
:map ;t :w\|:call Send_to_Tmux("rspec spec/my_spec.rb\n")<CR>

If I need to interact with a shell I’ll usually Ctrl-Z vim, do what I need to do, then fg back again. If it’s a context switch, I’ll start a new tmux window then exit it after I’m done with the distraction.

I don’t use sessions. I prefer setting up from scratch each time since it takes no time at all, and eases my brain into the problem. Clean desk and all that.

That’s it. Nothing too fancy, but I’ve been meaning to make the switch from MacVim for a while and with this set up I can’t ever see myself going back.

  1. Dave Bolton says:

    Is your first brew install supposed to be for the homebrew-alt vim? Right now it reads "screen.rb"

    I remap my Tmux key to Ctrl-z (So Ctrl-z z to suspend). Ctrl-b is insane.

    And any particular reason you need to use screen-256color-bce in your TERM setting? Haven't needed that here.

  2. Ralph von der Heyden says:

    Thanks for that great post, Xavier. I've tried the tmux/cli-vim combo several times now, and each time I found something holding me back, like the missing osx clipboard support. But after your post, I may just give it another try.

    I have one question, though: Did you find a way to integrate the tmux pastebuffer with the osx clipboard? Like when I select text in copy mode, I sometimes would like to paste it in native osx applications, not just other tmux sessions. Did you find a solution for that?

  3. Xavier Shay says:

    Dave, yeah homebrew-alt screen was a miscopy, supposed to be vim, fixed.
    I tried many combinations to get colors working, -bce is what I ended up with. Some documentation somewhere told me to do it. Just removed it though and it still seems to work, so...

    Ralph, I think my workaround is using the mouse to select things in copy mode. Not ideal if you're keyboard only, but I tend to be using my mouse to scroll in copy mode anyways. Let me know if you find something.

  4. Ralph von der Heyden says:

    Indeed, I found a solution for this. I am using this in my ~/.tmux.conf in order to copy the tmux buffer to the osx clipboard:

    1
    
    bind C-y save-buffer /tmp/tmux-buffer \; run-shell "reattach-to-user-namespace -l zsh -c 'cat /tmp/tmux-buffer|pbcopy'"
    

    Thanks again for the article!

  5. Rolf Bjaanes says:

    Vim still points to /usr/bin/vim after installing it through homebrew-alt.
    How do I fix that?

  6. Ralph von der Heyden says:

    @Rolf You can run "brew list vim" in order to find out where the binaries are installed, and adjust your $PATH accordingly.

    Or if you don't mind installing macvim, you can just do that from the core homebrew repository, and then put the following in your .{bash|zsh}rc:

    alias vim='mvim -v'

  7. Rolf Bjaanes says:

    @Ralph: Thanks for the quick reply.

    I have MacVim, but i'd like to try this non-MacVim setup.

    `brew list vim`
    says Error: No such keg: /usr/local/Cellar/

    trying the install from homebrew-alt again says Warning: Formula already installed: vim

    `brew list`
    show vim in the list

    -- very confusing :)

  8. Rolf Bjaanes says:

    I had to prepend my $PATH with /usr/local/bin.
    Apparantly this wasn't done and `brew doctor` told me about it.

    Everything is in it's place.

    Apologies for the noise and thanks for the help :)

  9. Julien Blanchard says:

    What are the benefits of using tmux windows and panes over iterm2 tabs and splits for local development?

  10. Xavier Shay says:

    Julien, I haven't used iterm2 tabs extensively, but I think the killer feature of tmux is being able to easily programmatically send text to a pane. I also find pane/window manipulation more natural in tmux, perhaps just because the default keyboard shortcuts are better (I'm using colemak, not sure where they are in qwerty).

  11. Kyriacos says:

    I've been using macvim in the terminal for quite some time and i made the switch for the past couple of days to macvim not in the terminal.

    The terminal version i found quite slow for some reason. Navigating in vim was slower and using minibufexplorer was a nightmare as it with nerdtree it kept opening wrong splits in wrong windows or kept closing minibuf most of the time with :bd.

    Command-t is pretty slow so is ack. Dont know why though.

    Im just wondering are you using anything which makes it overall faster in the terminal? Have you seen a difference in speed?

    I want to go back namely because of tmux but fir some reason its pretty slow on my machine.

    Thanks :).

  12. Marcin Kulik says:

    Dude, I was thinking about starting to learn colemak but the biggest worry was how does it go with Vim and its qwerty-centric bindings. Can you tell me more about your colemak+vim setup? Or write a blog post even ;)

  13. Dave Bolton says:

    I second the request for a blog post on colemak + vim -- I'd love to know how that works in practice.

  14. Hannes says:

    I modified Ralph's suggestion to not use a tempfile.

    1
    
    bind C-y run-shell "reattach-to-user-namespace -l zsh -c 'tmux show-buffer | pbcopy'"
    

    Thanks for the original solution.

  15. Jens says:

    I'm trying to get a tmux to accept some changes to the colouring I'm trying to make, but it doesn't work. I'm using iTerm with the solarized-colorscheme as well an I'm trying to set the bg colour of the status bar to base02 (wich is working) and the the fg color to base1 (which is not working) using the following in the .tmux.conf:

    1
    2
    
    set -g status-bg colour235  
    set -g status-fg colour244 
    
    Accourding to http://ethanschoonover.com/solarized colour244 should match base1. But tmux doesn't seem to accept this, it will display the text in the status bar in plain white!

    I already tried the here mentioned settings for 265-colours in the .tmux.conf an .bashrc, without any effect...

    Anyone any Idea?

  16. Seth Milliken says:

    To have homebrew automatically create the appropriate symlinks (in `/usr/local/bin`) to supersede the system vim:

    1
    
    brew install macvim --override-system-vim
    

  17. Xavier Shay says:

    Re Colemak + Vim, I learned vim after Colemak and so just learned the keys without remapping anything. Obviously this is suboptimal for like jkl; movement, but for the things you actually end up using a heap they're all in pretty good positions.

  18. chris says:

    Nice post. Out of curiosity, how is tmux better than screen? I've done similar set up as yours for a while, but used screen. I've only briefly used tmux because screen (with the vertical split patch) has been satisfactory for me, so I'd be interested in some of the advantages you found with tmux.

  19. Xavier Shay says:

    Chris, see second sentence. I tried for a while but couldn't get screen to do what I wanted, as far as split panes, mouse integration, clipboard, and colors go (could get most but not all).

  20. Hank Beaver says:

    I appreciate your post and don't want to debate screen versus tmux. It got me almost where I needed to be. I use OSX 10.7, tmux, iTerm, vim. I need to be able to copy from vim buffer and tmux scrollback into OSX clipboard. The only step that I culled from Vim wiki was:

    set clipboard=unnamed

    Once I did that the vim yank and clipboard magically worked.

    Thanks,
    H

  21. https://www.google.com/accounts/o8/id says:

    Homebrew VIM is now at homebrew-dupes, so the new URL is

    1
    
    brew install --HEAD https://raw.github.com/Homebrew/homebrew-dupes/master/vim.rb 
    

  22. Howard says:

    ctrl-a then z to zoom into a tmux pane. do the same to zoom back out. that way you don't have to do that crazy ctrl-a ctrl-o to swap panes.

Post a comment


(lesstile enabled - surround code blocks with ---)

A pretty flower Another pretty flower