Skip to content

Tmux configuration, that supercharges your tmux to build cozy and cool terminal environment

Notifications You must be signed in to change notification settings

samoshkin/tmux-config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tmux Configuration

Tmux configuration, that supercharges your tmux to build cozy and cool terminal environment.

intro

Table of contents

  1. Features
  2. Installation
  3. General settings
  4. Key bindings
  5. Status line
  6. Nested tmux sessions
  7. Themes and customization
  8. iTerm2 and tmux integration

Features

  • "C-a" prefix instead of "C-b" (screen like)
  • support for nested tmux sessions
  • configurable visual theme/colors, with some elements borrowed from Powerline
  • can apply different configuration whether your session is on local or remote machine
  • supercharged and cozy status line
  • status line: CPU, memory usage, system load average metrics
  • status line: username and hostname, current date time
  • status line: battery information in status line
  • status line: visual indicator when you press prefix
  • status line: visual indicator when pane is zoomed
  • status line: online/offline visual indicator
  • toggle visibility of status line
  • monitor windows for activity/silence
  • scroll and copy mode improvements
  • prompt to rename window right after it's created
  • highlight focused pane
  • merge current session with existing one (move all windows)
  • integration with 3rd party plugins: tmux-sidebar, tmux-copycat, tmux-open, tmux-plugin-sysstat

TBD:

  • retain current path when new pane is created
  • integration with clipboard
  • fix installation script to properly install TPM

Installation

The prerequisite is installed tmux >= "2.4" and Tmux Plugin Manager

$ git clone https://github.com/samoshkin/tmux-config.git
$ ./tmux-config/install.sh

Installation script will copy files to ~/.tmux directory, symlink main ~/.tmux config file. If you already have existing ~/tmux.conf, backup will be created.

Note, that after you launch tmux, plugins needs to be installed. Hit C-a Shift-i keybinding to install all tmux plugins.

  • TODO: automate TPM and tmux plugin installation

General settings

Windows and pane indexing starts from 1 rather than 0. Scrollback history limit is set to 20000. Automatic window renameing is turned off. Aggresive resizing is on. Message line display timeout is 1.5s. Mouse support in on.

256 color palette support is turned on, make sure that your parent terminal is configured propertly. See here and there

# parent terminal
$ echo $TERM
xterm-256color

# jump into a tmux session
$ tmux new
$ echo $TERM
screen-256color

Key bindings

So ~/.tmux.conf overrides default key bindings for many action, to make them more reasonable, easy to recall and comforable to type.

Let's go through them.

If you are an iTerm2 user, third column describes the keybinding of similar "action" in iTerm2. It's possible to reuse very same keys you already get used to and tell iTerm2 to execute analogous tmux actions. See iTerm2 and tmux integration section below.

tmux key Description iTerm2 key
C-a Default prefix, used instead of "C-b". Same prefix is used in screen program, and it's easy to type. The only drawback of "C-a" is that underlying shell does not receive the keystroke to move to the beginning of the line. -
<prefix> C-e Open ~/.tmux.conf file in your $EDITOR -
<prefix> C-r Reload tmux configuration from ~/.tmux.conf file -
<prefix> r Rename current window -
<prefix> R Rename current session -
<prefix> _ Split new pane horizontally ⌘⇧D
<prefix> | Split new pane vertically ⌘D
<prefix> < Select next pane ⌘[
<prefix> > Select previous pane ⌘]
<prefix> ← Select pane on the left ⌘⌥←
<prefix> → Select pane on the right ⌘⌥→
<prefix> ↑ Select pane on the top ⌘⌥↑
<prefix> ↓ Select pane on the bottom ⌘⌥↓
<prefix> C-← Resize pane to the left ^⌘←
<prefix> C-→ Resize pane to the right ^⌘→
<prefix> C-↑ Resize pane to the top ^⌘↑
<prefix> C-↓ Resize pane to the bottom ^⌘↓
<prefix> > Move to next window ⌘⇧]
<prefix> < Move to previous window ⌘⇧[
<prefix> Tab Switch to most recently used window ^Tab
<prefix> L Link window from another session by entering target session and window reference -
<prefix> \ Swap panes back and forth with 1st pane. When in main-horizontal or main-vertical layout, the main panel is always at index 1. This keybinding let you swap secondary pane with main one, and do the opposite. ⌘\
<prefix> C-o Swap current active pane with next one -
<prefix> + Toggle zoom for current pane ⌘⇧Enter
<prefix> x Kill current pane ⌘W
<prefix> X Kill current window ⌘⌥W
<prefix> C-x Kill other windows but current one (with confirmation) -
<prefix> Q Kill current session (with confirmation) -
<prefix> C-u Merge current session with another. Essentially, this moves all windows from current session to another one -
<prefix> d Detach from session -
<prefix> D Detach other clients except current one from session -
<prefix> C-s Toggle status bar visibility -
<prefix> m Monitor current window for activity -
<prefix> M Monitor current window for silence by entering silence period -
<prefix> F12 Switch off all key binding and prefix hanling in current window. See "Nested sessions" paragraph for more info -

Status line

I've started with Powerline as a status line, but then realized it's too fat for my Macbook 15'' display, it hardly can fit all those fancy arrows, widgets and separators, so that I can only see one window "tab".

So I decide to make my feet wet, with the idea to keep it dense, and include essential widgets. Sometimes it tries to replicate OSX topbar (battery, date time).

Left part: status line left

Right part: status line right

The left part contains only current session name.

Window tabs use Powerline arrows glyphs, so you need to install Powerline enabled font to make this work. See Powerline docs for instructions and here is the collection of patched fonts for powerline users

The right part of status line consists of following components:

  • CPU, memory usage, system load average metrics. Powered by tmux-plugin-sysstat (dislaimed, that's my own development, because I haven't managed to find any good plugin with CPU and memory/swap metrics)
  • username and hostname (invaluable when you SSH onto remote host)
  • current date time
  • battery information
  • visual indicator when you press prefix key: [^A].
  • visual indicator when pane is zoomed: [Z]
  • online/offline visual indicator (just pings google.com)

You might want to hide status bar using <prefix> C-s keybinding.

Nested tmux sessions

One prefers using tmux on local machine to supercharge their terminal emulator experience, other use it only for remote scenarios to retain session/state in case of disconnect. Things are getting more complex, when you want to be on both sides. You end up with nested session, and face the question: How you can control inner session, since all keybindings are caught and handled by outer session?. Community provides several possible solutions.

The most common is to press C-a prefix twice. First one is caught by local session, whereas second is passed to remote one. Nothing extra steps need to be done, this works out of the box. However, root keytable bindings are still handled by outer session, and cannot be passed to inner one.

Second attempt to tackle this issue, is to setup 2 individual prefixes, C-b for local session, and C-a for remote session. And, you know, it feels like:

tmux in tmux

And finally accepted solution, turn off all keybindings and key prefix handling in outer session, when working with inner one. This way, outer session just sits aside, without interfering keystrokes passed to inner session. Credits to http:https://stahlke.org/dan/tmux-nested/ and this Github issue

So, how it works. When in outer session, simply press F12 to toggle off all keybindings handling in outer session. Now work with inner session using the same keybinding scheme and same keyprefix. Press F12 to turn on outer session back.

nested sessions

You might notice that when key bindings are "OFF", special [OFF] visual indicator is shown in the status line, and status line changes its style (colored to gray).

Local and remote sessions

Remote session is detected by existence of $SSH_CLIENT variable. When session is remote, following changes are applied:

  • status line is docked to bottom; so it does not stack with status line of local session
  • some widgets are removed from status line: battery, date time. The idea is to economy width, so on wider screens you can open two remote tmux sessions in side-by-side panes of single window of local session.

You can apply remote-specific settings by extending ~/.tmux/.tmux.remote.conf file.

Themes and customization

All colors related to theme are declared as variables. You can change them in ~/.tmux.conf.

# This is a theme CONTRACT, you are required to define variables below
# Change values, but not remove/rename variables itself
color_dark="$color_black"
color_light="$color_white"
color_session_text="$color_blue"
color_status_text="colour245"
color_main="$color_orange"
color_secondary="$color_purple"
color_level_ok="$color_green"
color_level_warn="$color_yellow"
color_level_stress="$color_red"
color_window_off_indicator="colour088"
color_window_off_status_bg="colour238"
color_window_off_status_current_bg="colour254"

Note, that variables are not extracted to dedicated file, as it should be, because for some reasons, tmux does not see variable values after sourcing theme.conf file. Don't know why.

create several panes, windows move between them zoom window open nested tmux session scroll and copy

iTerm2 and tmux integration

If you're an iTerm use same to me, most likely you already have a muscle memory for most common actions and keybindings (split pane, focus pane, fullscreen pane, move between tabs, create new tab, etc). When I switched to tmux, I found new key table more difficult: more keys to type, don't forget to enter prefix and recall if you've already pressed it or not (compare C-a, c with "⌘T", or C-a -> with "⌘⌥->"). iTerm2 keybinding was so natural to me, so I decided to remap most common keybindings to tell iTerm2 to execute corresponding tmux actions.

You can setup new profile in iTerm preferences to override default keybindings, to tell iTerm to send pre-configured sequences of keys, that will trigger corresponding action in tmux.

iterm preferences

For example, when "^⌘↑" pressed, sequence of bytes 0x01 0x1b 0x5b 0x31 0x3b 0x35 0x41 are sent through terminal to running tmux instance, that interprets them as C-a C-↑ keybinding and triggers resize-pane -U according to our .tmux.conf configuration.

You can get binary representation of any keys, using showkey or od commands

$od -t x1

^A^[[1;5A   // press C-a C-↑ on your keyboard
0000000 01 1b 5b 31 3b 35 41
0000007
$ showkey -a
Press any keys - Ctrl-D will terminate this program

^A        1 0001 0x01
^[[1;5A  27 0033 0x1b
         91 0133 0x5b
         49 0061 0x31
         59 0073 0x3b
         53 0065 0x35
         65 0101 0x41

You can remap whatever key in this way, but I do this only for those ones, which have similar analogous action in tmux and are most common(resize pane, zoom pane, create new window, etc). See table with keybindings above.

As additional step, you can setup this new iTerm profile as default one, and tell it to jump into tmux session right off the start.

iterm tmux default profile

You can then go full screen in iTerm, so iTerm tabs and frame do not distract you (anyway now you're using iTerm just as a tunnel to your tmux, everything else happens inside tmux).

full screen mode

About

Tmux configuration, that supercharges your tmux to build cozy and cool terminal environment

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Languages