tmux and mouse mode

UPDATE 17 March 2013: I am placing this post into the public domain so people are free to incorporate it into their existing files which may be in the public domain already.


As I commented in my last post about tmux, tmux mouse mode left some things to be desired. Namely, there are two problems.

First, it completely takes over the mouse. Meaning that you can no longer do a simple “mark text and copy” operation on the native OS. There are patches over this problem, like reattach-to-user-namespace, but that is only helpful if you are running tmux on your Mac. I practically never do that, because most of the usefulness of tmux for me is in leaving active sessions to remote servers. I work on remote servers over ssh connections, detach from tmux, and stick my laptop in its bag. I come back to the session later. That would not be possible if tmux were running on my Mac, so I am almost always running it remotely on a Linux server.

Second, there is the pane problem. If you turn mouse mode off, and try to copy text in a pane that has another pane to its side, the terminal will just copy across the boundary. iTerm has no idea that the pane boundary exists; it’s just another character printed in the terminal window.

These days, I have mouse mode turned on by default in .tmux.conf. It’s just too useful to not have it on. If my hand is already on the trackpad, switching between windows or panes, or doing pane resizes, is very handy with the pointer. It’s also usually much faster to do a mark/copy operation within tmux by using the pointer.

1
2
3
4
set -g mode-mouse on
set -g mouse-resize-pane on
set -g mouse-select-pane on
set -g mouse-select-window on

But I do often want to do an OS-native mark and copy. So, I have defined two hotkeys to make it easy to turn mouse mode on and off.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Toggle mouse on with ^B m
bind m \
  set -g mode-mouse on \;\
  set -g mouse-resize-pane on \;\
  set -g mouse-select-pane on \;\
  set -g mouse-select-window on \;\
  display 'Mouse: ON'

# Toggle mouse off with ^B M
bind M \
  set -g mode-mouse off \;\
  set -g mouse-resize-pane off \;\
  set -g mouse-select-pane off \;\
  set -g mouse-select-window off \;\
  display 'Mouse: OFF'

UPDATE 20 July 2012: @aristidesfl points out that you don’t need to bind anything for mouse mode disable in iTerm2, simply hold option down and iTerm2 turns it off temporarily.

That makes it easy to switch between the two states (mouse mode on or off). But there is still one nagging situation: I want to do OS-native copy and paste, but it’s in a pane. Even with mouse mode off, things break because the terminal does not know what a tmux pane is. Once again, key bindings come to the rescue.

1
2
3
4
5
6
7
8
9
10
11
unbind +
bind + \
  new-window -d -n tmux-zoom 'clear && echo TMUX ZOOM && read' \;\
  swap-pane -s tmux-zoom.0 \;\
  select-window -t tmux-zoom
    
unbind -
bind - \
  last-window \;\
  swap-pane -s tmux-zoom.0 \;\
  kill-window -t tmux-zoom

These bindings (^B + and ^B -) are a “zoom” feature. Focused on a given pane, ^B + will promote the pane to be its own window. Then you can do OS-native mark and copy all you want, no panes to worry about. When you’re done, just use ^B - to turn the window back into a pane, right where it was before.

I’ve chosen to keep these two things separate, but with a little tinkering, you can create a binding that does both operations together: zoom a window, turn mouse mode off, and another binding to do the inverse.

As always you can find my current .tmux.conf file on GitHub.

iTerm2 keymaps for tmux

UPDATE 17 March 2013: I am placing this post into the public domain so people are free to incorporate it into their existing files which may be in the public domain already.

UPDATE 16 July 2012: I have updated this post to reflect my current keymaps, which I settled on after writing the original post.


As I’ve said before, I’m a huge fan of iTerm2. I am also a huge fan of tmux, the terminal multiplexor. If you’re not familiar with tmux, it’s conceptually the same thing as GNU screen, with some differences, and (in my opinion) some advantages. For example:

  • In screen, each instance is its own completely separate process. With tmux, the first time you run it, you start a server, but subsequent runs will instantiate clients that connect to that existing server. Why does that matter? Because it means you can lop a window off of one tmux session and glue it onto another. It also means you can view the same session from multiple clients simultaneously (say you left tmux attached at the office, you can attach to that same session at home without disrupting the session you left attached at work). You can even share a session with someone else. All of that’s possible because it’s all going on in the same server process.

  • tmux supports multiple panes within a window, where screen supports only windows.

  • If you don’t set a window title, tmux will auto-set it for you based on what you run in that window. So it might start as “bash” and change automatically to “vim” or “top” or “tail” depending on what you run inside. That is pretty handy to tell what’s going on in what window even if you don’t take the time to set a name for it.

It so happens that the authors of iTerm2 are tmux users as well, and they have integrated a tmux mode into iTerm2. It’s an early stage thing, so you have to forgive it, but at this point it still has some drawbacks. I am not going to talk about that integration much in this post, but I wanted to point out that it exists, that I know about it, and explain why I don’t use it.

  1. You can only connect iTerm2 to one iTerm2-aware tmux session at a time.

  2. They communicate via a special protocol. In practical terms, this means you “spend” a window for that channel. It sort of sits around lingering. To be fair, they added an option to auto-hide it on connect so it goes away by itself.

  3. Because this is still under development, you currently have to run the right version of iTerm2 with the right version of patched tmux. That can be a headache if your server side is used by multiple people. It also means an upgrade to iTerm2 sends you off to recompile tmux before you can use it in integrated mode again.

For these reasons, I have set aside the iTerm2 tmux integration and I’m just using regular tmux again. I’ll check it out again someday after it has matured (and preferably been built into tmux mainline so I don’t have to patch a copy myself).

But after using the integration, I found I was longing for easy hotkeys to navigate around my windows and panes in tmux. (Using the integrated mode, you use the iTerm-native hotkeys because it treats the tmux windows and panes as native iTerm tabs and panes).

I was frustrated by this because in tmux you use a prefix key for everything, so going to the next window means using Ctrl-B n and going to another pane might be Ctrl-B H. You can remap a lot of things, but it’s never as easy as native keys like Shift-Cmd-Right or similar that we are used to using on Macs and PCs.

At first, I played with the tmux mouse mode. It’s actually pretty nice. Assuming you send mouse events through to the server, tmux will recognize them (if you turn on the appropriate options), allowing you to select windows and panes using mouse clicks. You can even resize panes. Unfortunately, that comes at a cost: it breaks using normal mouse selection to copy from iTerm2 window into other programs on your Mac. Not good.

Then, I found “Send Hex Code.”

Angels rejoiced. Benjamins fell from the sky. My doctor told me to eat more ice cream.

Send Hex Code is just an unassuming option in a pulldown in the iTerm key mapping preferences. But when I saw it, I knew it was the solution to my problem. The thing is, you can represent any ASCII character as a hex value, and sending a sequence of ASCII characters is exactly what I needed to do.

I looked up some ASCII and ANSI hex values.

My first goal was to navigate between windows. The default maps for that are:

1
2
^B n        next window
^B p        previous window

Let’s rewrite those as hex bytes.

1
2
0x02 0x6E   next window
0x02 0x70   previous window

Next goal: navigate between panes in all four directions. This was stickier because it involves arrow keys, but! arrow keys are just escape sequences. So a few more hex bytes are all we need.

1
^B <arrow>      move to pane in <arrow> direction

The ANSI arrow sequence is <Esc>[<direction> where direction is one of [A, B, C, D] (mapped to [up, down, right, left]). Rewritten as hex:

1
2
3
4
0x02 0x1B 0x5B 0x41     move to pane above
0x02 0x1B 0x5B 0x42     move to pane below
0x02 0x1B 0x5B 0x43     move to pane at right
0x02 0x1B 0x5B 0x44     move to pane at left

Lastly, I wanted to be able to resize any given pane in any direction using key bindings. I added some bindings to ~/.tmux.conf (borrowing directional keys from my favorite editor, Vim).

1
2
3
4
bind-key J resize-pane -D
bind-key K resize-pane -U
bind-key H resize-pane -L
bind-key L resize-pane -R

Those bindings rewritten as hex values:

1
2
3
4
0x02 0x4B   resize upward
0x02 0x4A   resize downward
0x02 0x48   resize leftward
0x02 0x4C   resize rightward

Armed with all of my hex sequences, I headed to the iTerm2 preferences. Under the “Keys” section, you can add new items to the global shortcut list. (You can also do this in a profile’s key settings if you have a reason to not want bindings to be available to all profiles.) I added the below sequences, binding them to hotkeys.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Mac hotkey      Hex sequence            Purpose

Cmd-Left        0x02 0x1B 0x5B 0x44     Move to pane at left
Cmd-Down        0x02 0x1B 0x5B 0x42     Move to pane below
Cmd-Up          0x02 0x1B 0x5B 0x41     Move to pane above
Cmd-Right       0x02 0x1B 0x5B 0x43     Move to pane at right

Cmd-Opt-Left    0x02 0x48               Resize leftward
Cmd-Opt-Down    0x02 0x4A               Resize downward
Cmd-Opt-Up      0x02 0x4B               Resize upward
Cmd-Opt-Right   0x02 0x4C               Resize rightward

Ctrl-Cmd-Left   0x02 0x70               Move to window at left
Ctrl-Cmd-Right  0x02 0x6E               Move to window at right

But I am a Vim user. h,j,k,l have been mapped into my fingers for more than a decade, and they are on the main keyboard, so I don’t need to move my hand to the side. Why not map those as well?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Mac hotkey      Hex sequence            Purpose

Cmd-H           0x02 0x1B 0x5B 0x44     Move to pane at left
Cmd-J           0x02 0x1B 0x5B 0x42     Move to pane below
Cmd-K           0x02 0x1B 0x5B 0x41     Move to pane above
Cmd-L           0x02 0x1B 0x5B 0x43     Move to pane at right

Cmd-Opt-H       0x02 0x48               Resize leftward
Cmd-Opt-J       0x02 0x4A               Resize downward
Cmd-Opt-K       0x02 0x4B               Resize upward
Cmd-Opt-L       0x02 0x4C               Resize rightward

Ctrl-Cmd-H      0x02 0x70               Move to window at left
Ctrl-Cmd-L      0x02 0x6E               Move to window at right

The panel to enter each key binding into looks like this.

And this is what my final key map looks like.

Now I can move between tmux windows with Ctrl-Cmd-{direction}, move among panes with Cmd-{direction}, and resize with Cmd-Opt-{direction}. It’s really nice, and I can use Vim keys (H,J,K,L) or arrows.

(Yes, I am aware that this overrides certain keymaps. You will lose the “Hide” hotkey, Cmd-H, but I never use that so I didn’t care. iTerm also maps Cmd-{left,right} to navigate between tabs, but Cmd-{digit} and Shift-Cmd-{left,right} will still work.)

Lastly, a few finishing touches for my tmux.conf. The colors I use here assume Solarized Dark for the iTerm color scheme. They may look awful with another color scheme (or they may look fine).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Watch background windows for activity
setw -g monitor-activity on

# Make the active pane more visible
set -g pane-border-bg default
set -g pane-border-fg colour245
set -g pane-active-border-bg default
set -g pane-active-border-fg colour46

# Make the active window's name stand out
setw -g window-status-current-fg brightwhite
setw -g window-status-current-bg black

# Use color to indicate activity in a background window
# (Note this is inverted, fg means bg and vice versa.)
setw -g window-status-activity-fg white
setw -g window-status-activity-bg brightred

You can always find my current .tmux.conf file on GitHub.

When even the death penalty doesn’t deter copying

A bit of history to give some perspective on our modern issue of piracy (music and movies):

A few centuries ago, the penalty for unauthorized copying was breaking on the wheel. It is a term we’re not very familiar with these days, but it was a form of prolonged torturous death penalty where the convict first had every bone in his body broken, and then was weaved into the spokes of a wagon wheel and set up on public display. The cause of death was usually thirst, a couple of days later.

Back then the issue was duplicating popular fabric patterns. Yes, that was really a thing. And note that this didn’t happen to only a handful of offenders. Sixteen thousand people suffered this fate. Over fabric patterns.

The brutality of our forebears never fails to amaze me.

But it’s the end result of this practice that I find the most fascinating:

Capital punishment didn’t even make a dent in the pirating of the fabrics. Despite the fact that some villages had been so ravaged that everybody knew somebody personally who had been executed by public torture, the copying continued unabated at the same level.

I guess some things never change.

And When Even The Death Penalty Doesn’t Deter Copying — What Then? | TorrentFreak

This is why your newspaper is dying

Ryan Cash complains about online newspaper sites:

Another HUGE complaint I’d like to add to Brad’s list is when these websites draw a story out over 10 pages.

Welcome to the Internet — a place where physical pages don’t exist, and there’s no such thing as “no space left”. There is absolutely NO REASON for any story to span over more than one single page.

I wholeheartedly agree.

But the problem here isn’t cluelessness on the part of the editors. The problem is that newspapers aren’t in the business of providing news, and they never have been. They are in the business of gathering page views. They used to call it circulation, and they used to sell half-page ads, classifieds, and coupon inserts, but it’s the same game as it ever was (except less profitable).

Back in the 90s, I toured a local NBC affiliate’s offices with a friend who was in the news business. Two things I heard that day have informed my opinion of commercial news ever since.

My friend, a local news producer for many years:

How we decide what’s on tonight’s news: News is whatever I say it is.

Down in the bowels of the building was a room where two men watched a wall of monitors. A videotape robot busily swapped tapes behind them. This room aired the commercials.

This is the only room in the whole building that matters. What you think of as TV is just the stuff we cram in between commercials to keep you from changing channels.

This is Why Your Newspaper is Dying

The Internet is not an elephant

Now and then you hear an old-timer rant about how we young people never sit down and write a real letter anymore. (Young in this case is probably defined as under 75.) We’re too busy with our email, our Twitter and Facebook, our IMs and texts.

Clearly, they are technically correct. Nobody writes letters anymore. It’s archaic, a relic of a bygone era. It’s simple economics. In the old days, a letter was the best option to reach across a great distance and communicate with someone. Technology has changed the equation; many better options now exist.

Think of the oldest person you know. Consider life when they came of age. That’s when we set our expectations of how the world is supposed to work. I’ll use my wife’s grandmother as an example. She will turn 91 in a few weeks, meaning she was born in the spring of 1920, in rural Poland. Since then she has seen a lot, including the inside of a Nazi work camp.

What was life like when she came of age? Well, she lived in the country. She probably walked everywhere. Maybe she had a bike, maybe not. There was no Internet. There were no mobile phones. The television would not be invented until she was eight years old, but I suspect she did not personally see one until she emigrated to the United States after World War II. A trip to the next town over would probably have been a major undertaking, let alone a long trip (even one to a country next door, a fairly short trip by modern first-world standards).

I am not saying she rants about people not writing letters; I can’t say I have ever heard her say anything on the subject. But within her life experience, a letter was the best option for most of the time. Only after she was entering her 70s were better options coming into play: cheap long-distance rates and the Internet.

As I age, I find myself thinking about the longevity of the information artifacts we generate. In the beginning, I was mostly worried about digital photos. How could I ensure they survived? Unlike film negatives, digital storage technology changes quickly. Think fast: here’s a ZIP disk. Can you read it? No? What about this 5.25” floppy? 3.5”? What is the likelihood that the JPEG format will be readable by computers in common use when I am a grandparent?

These days, I am concerned about digital artifacts as a whole. My life is ever-increasingly digital, as it is with all of us. I have scanned and shredded most of my file cabinet. I get my bills via email. I communicate with everyone online, leaving no persistent trail (or so I hope). I haven’t used a film camera in years. My magnetic-tape videocamera is in a bag somewhere, but my Flip Mino and my iPhone shoot video all the time.

For the past ten years, I have been a member of Distributed Proofreaders, whose mission is to help Project Gutenberg digitize public domain works for safekeeping and distribution. We scan old books using OCR software, and then the hive mind goes to work. I and many others proofread scanned text. Others take the output from proofreaders and add formatting markup (italics, footnotes, etc.). After several rounds of work, a completed ebook is handed off to Project Gutenberg for publication. It is, for me, important work that I am happy to help with. Time will destroy all old books eventually; I believe they should be preserved for future generations.

I am currently working on a biography about William Cowper, a writer and poet who lived during the latter half of the 18th century. As with many such books, it is full of correspondence, which tells the story of his life. You get a feel for who he was, how he lived, how his relationships worked. I had never heard of William Cowper before I began scrutinizing these scans for errors, but I now find myself fascinated with his story.

I wonder: will anyone be able to write such books about us in a hundred years? How would they go about it? There will be so little left behind. Will your blog still be running for someone to analyze? Where will your emails be accessible? Your tweets? God help you, your Facebook photos from college?

They say the Internet never forgets, but I’m not so sure. I suspect that over a long enough period of time, the Internet will in fact forget. It will forget us all.

Paul Graham on: stuff

Paul Graham, on our obsession with material possessions:

Companies that sell stuff have spent huge sums training us to think stuff is still valuable. But it would be closer to the truth to treat stuff as worthless.

In fact, worse than worthless, because once you’ve accumulated a certain amount of stuff, it starts to own you rather than the other way around. I know of one couple who couldn’t retire to the town they preferred because they couldn’t afford a place there big enough for all their stuff. Their house isn’t theirs; it’s their stuff’s.

Not long after I bought my first home in 1997, I was talking to the next-door neighbor and mentioned that I was envious of his basement. My house didn’t have one, but I had grown up with basements all my life. I was always trying to figure out where to store things.

He replied that he had been in his house since it was built in the early 50s, and he pitied whoever had to clean the basement out when he died. That was the first time I ever thought of a basement as a double-edged sword.

I first realized the worthlessness of stuff when I lived in Italy for a year. All I took with me was one large backpack of stuff. The rest of my stuff I left in my landlady’s attic back in the US. And you know what? All I missed were some of the books. By the end of the year I couldn’t even remember what else I had stored in that attic.

And yet when I got back I didn’t discard so much as a box of it. Throw away a perfectly good rotary telephone? I might need that one day.

This reminds me of the storage unit I rented when I was selling my last house. I filled a 100 square foot room with extraneous stuff I didn’t have any day-to-day use for. It sat in there for more than six months.

Paul Graham on: Stuff

Instapaper’s fatal Kindle flaw

That’s very true.

Unfortunately, Instapaper’s support for the Kindle is missing one pretty big feature: the ability to assign a specific Instapaper folder to send to a Kindle.

The only choice today is to feed “Read Later” to a Kindle. That’s great, if you only use Instapaper for articles you want to read later. But how many people actually use it that way?

I feed all sorts of things into Instapaper.

  • Longer-form articles
  • Short blog posts
  • YouTube or Vimeo video pages
  • A political petition to be filled out
  • A cool gadget I saw on Amazon or wherever

The long-form articles are what I want to send to my Kindle.

The blog posts would work for it, but why bother sending them there? I can usually read those in under two minutes. And sending video pages or petitions to my Kindle is just a waste. I can’t do anything with them there.

My solution for a while now has been to split “Read Later” into two folders. The main “Read Later,” and a second that I named “Read Later - not for Kindle.” I have two bookmarklets in Safari, one to send to my Kindle, the other to send to a generic Instapaper folder. Not so bad, right?

Not if I’m using a browser.

Here’s the problem. I send things to Instapaper from a lot of places. The browser, sure, but also from certain web apps I use, and from a number of iPhone apps. Sometimes I email content to my Instapaper account. And none of those methods support folders. The default for feeding into Instapaper via its API or email is to stuff everything into “Read Later.”

That leaves me back where I started—now I have Kindle-inappropriate items that might get fed to my Kindle. I have to pay Amazon for that delivery (okay, it’s really cheap, but I’m still paying for it).

I have workarounds. Most iPhone apps, for instance, support opening links in Mobile Safari. So I do that, leaving the app I was using, wait for the page to load a second time in Safari, then use a bookmarklet to save to my non-Kindle Instapaper folder. Then I have to go back to the app I started from, sometimes having to wait for the page to load a third time, only to tap back to whatever I am viewing (a Twitter stream, Facebook, Pulse News, whatever).

I could just put everything in “Read Later,” then go into the Instapaper iPhone app or the web site and tidy up the folders before Friday morning when the weekly Kindle deliveries go out. If I remember to. Which I never do.

All of this would be solved if Instapaper supported assigning a folder for Kindle posts. Then I could haphazardly fill up “Read Later,” and as I look through that stream, move things into the Kindle folder. Easy. Or at least much easier than the dance I’ve been going through.

I mean was going through. Because at this point, I’m finding this dance so annoying that I have turned Kindle deliveries off entirely. I’ll just read things on my iPhone or laptop, and go back to using the Kindle for reading books.

Don’t get me wrong, I love my Kindle and I love Instapaper. I’m just sick of workarounds.

Who says help files aren’t funny?

I just found this gem in Vim’s help files.

:help map-overview

1
:nunmap can also be used outside of a monastery.

iTerm2 > iTerm

I recently wrote that iTerm is better than Terminal.app.

Later that day, I received this reply on Twitter.

He was right.

iTerm2 is a fork of the original iTerm project. By and large, they are the same, but iTerm2 has a considerable number of improvements. Here are a few.

Smart cursor color

This may seem minor, but it’s nice to have. iTerm2 watches the colors being displayed, and will adjust the cursor color to ensure it is fairly visible on the screen.

Paste history

The last 20 items you copied from iTerm2, or pasted into it, are memorized. You can access that list with a hotkey combo.

Even if you use a clipboard history manager, like the one built into LaunchBar, this is useful because it is limited to the iTerm2 application. You can copy and paste all you want in other parts of the system, and the last 20 items that dealt with iTerm2 are always waiting for you when you get back.

Find and autocomplete

The Find feature in iTerm2 is really nice. Cmd-F brings up a search bar at the bottom of the window, similar to the one used in Safari. Enter your search string. Hit Tab to select to the end of the word (or Shift-Tab to go to the beginning of the word). You can find, select, and copy, all without needing to touch a mouse or trackpad.

But even better is autocomplete. Start typing, then hit Cmd-; to bring up an autocomplete list, built from the contents of the window as well as the aforementioned paste history. Type some more to refine the selection list. Use the Up and Down arrow keys to choose what you are after, then Enter to insert it at the cursor position.

Instant replay

Last, but absolutely not least, is the most innovative feature I’ve seen in a terminal emulator since… well, since ever. This is iTerm2’s “wow factor.”

Instant reply is TiVo for your terminal session.

No, I don’t mean you can scroll backward. Any terminal can do that. Instant replay is like having someone stand over your shoulder with a videocamera, recording your session. At any moment you can turn to them and ask for the tape to be rewound and played back.

If that doesn’t sound useful to you, you aren’t thinking hard enough.

How many times have you lost output because something like vim or less render in a way that doesn’t appear in your scrollback buffer? What about curses-driven interfaces, which vanish forever when they close? Even when they’re still running, the screens change and you can lose what you were looking at before. Instant reply captures all of it. You can scrub back and forth in time, just like when you play a video file.

It even remembers when you resized the window, and resizes it appropriately as you step back and forth in time.

Give iTerm2 a try. I’m loving it.