Mike Slinn

tmux

— Draft —

Published 2015-09-14.
Time to read: 3 minutes.

Tmux is a console window pane manager that is very effective for Scala programming if you are familiar with vi or emacs.

tmux is a terminal multiplexer. It lets us use a single environment to launch multiple terminals, or windows, each running its own process or program. tmux is quick, flexible and sessions can be made to be persistent. You might be interested in the book entitled , by Brian Hogan.

Tmux does not require vim or emacs, but you should either use vim or emacs with tmux in order to get full benefit. I use vim. Here are some video tutorials on vim. Practical is a good book.

Installation

Mac

Scala REPL
$ scala> brew install tmux

Install iTerm as discussed in the Installing Scala lecture and use iTerm to launch tmux.

Ubuntu

tmux is installed by default.

Windows

I’ve tried running tmux on ConEmu and Cmder. That was not a pleasant experience. The new Windows Terminal and Console API should show usable results in the early fall of 2019.

All OSes

To start tmux on every shell login, or connect to the default session if it exists, add the following at the end of ~/.bashrc, ~/.bash_profile or ~/.profile.

  # If running ConEmu do not run tmux
  [[ "$(set |grep -i conemu)" ]] && return

  # Only run tmux if running interactively
  [[ $- != *i* ]] && return
  [[ -z "$TMUX" ]] && tmux attach || tmux

Settings

These settings work on all operating systems. Store the contents of the gist shown below in ~/.tmux.conf. Here is what the settings do.

  • Changes the key binding prefix that you need to type before issuing a tmux command from Ctrl-b to Ctrl-a.
  • Enables mouse control of window panes.
  • Creates new panes with the same current directory as the previously active pane.
  • Changes vertical and horizontal window split key bindings to the tmux prefix followed by \ and -, respectively.
  • Uses the default enter copy mode key binding: tmux prefix Ctrl-[.
  • Uses the default paste buffer key binding: tmux prefix Ctrl-].
  • Launches the bash man command with the key binding: tmux prefix /.
  • Reload tmux configuration file (~/.tmux.conf) with the key binding: tmux prefix r.

Key Bindings

Key bindings used in the remainder of this lecture are.

tmux

  • Ctrl-a \ split the current pane vertically.
  • Ctrl-a - split the current pane horizontally.
  • Ctrl-a d detach the tmux session. Reconnect with tmux attach (this is done automatically when you log in if you follow the installation instructions in this lecture).
  • Ctrl-a left-arrow make the pane to the left active.
  • Ctrl-a right-arrow make the pane to the right active.
  • Ctrl-a up-arrow make the upper pane active.
  • Ctrl-a down-arrow make the lower pane active.

vi

  • : Explore open file using file browser.
  • : w write (save) file in buffer.
  • : b w close file in buffer without exiting vi.
  • : help tutor run built-in vi tutorial.

Sample Session

  1. Start a new session in the courseNotes/ directory:
    Scala REPL
    $ scala> tmux
  2. Create a new vertical pane: Ctrl-a \
  3. Make the original (left-most) pane active: Ctrl-a left-arrow
  4. Open vi in the currently active pane:
    Scala REPL
    $ scala> vi
  5. Browse to a Scala source file: : Ex tab (expands to Explore) Enter
  6. Use arrow keys and Enter to navigate to the src/main/scala directory. Select TypeHierarchy.scala and press Enter.
  7. Make the right-most pane active: Ctrl-a right-arrow.
  8. Cause the project to be compiled and run the DogHog console application any time that a file in the courseNotes/ directory tree is modified (we discussed how SBT tasks can be triggered when the source tree changes in the SBT Tasks and Settings lecture):
    Scala REPL
    $ scala> sbt ~"runMain DogHog"
  9. Go back to the edited file, add a new blank line at the top, and save it: shift-O (that is capital "o") Esc : w. Notice that the project recompiles in the rightmost pane and DogHog runs there.
  10. Delete the blank line you inserted and save the project: d d : w. Notice that the project recompiles in the rightmost pane and DogHog runs.

  11. Reattach to the tmux session:
    Scala REPL
    $ scala> tmux attach

    Everything appears just as you left it.

  12. Make the right-most pane active and create a new pane under it: Ctrl-a right-arrow Ctrl-a -
  13. Open an SBT console in the new pane and enter a Scala expression:
    Scala REPL
    $ scala> sbt console
    ...
      lots of output ...scala> 
    scala>scala>  2+3
    res0: Int = 5
  14. Grab pane borders with the mouse and resize them.
  15. Rotate the panes: Ctrl-a Ctrl-o
  16. Rotate the panes twice more so they go back to how they were: Ctrl-a Ctrl-o Ctrl-a Ctrl-o
  17. Make the top right pane active: Ctrl-a up-arrow
  18. Make the left-most pane active: Ctrl-a left-arrow
  19. Close the file that you are editing without exiting vi: : b w Enter
  20. Detach from the tmux session: Ctrl-a d

Deeper Integration of Scala with vi

(This section is in process)

  1. Install Pathogen, a package manager for vi:
    Scala REPL
    $ scala> mkdir -p ~/.vim/autoload ~/.vim/bundle
    $ scala> curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
    $ scala> echo "execute pathogen#infect()" >> ~/.vimrc
  2. Install staircase:
    Scala REPL
    $ scala> git clone https://github.com/adinapoli/staircase.git ~/.vim/bundle/staircase

    Now whenever you start vi from an SBT project, Staircase will automatically run SBT console.

  3. Install Systastic:
    git clone https://github.com/scrooloose/syntastic.git .vim/bundle/syntastic
    cat >> ~/.vimrc <<EOF
    " Sysastic settings from https://github.com/scrooloose/syntastic#3-recommended-settings set statusline+=%#warningmsg# set statusline+=%{SyntasticStatuslineFlag()} set statusline+=%*
    let g:syntastic_always_populate_loc_list = 1 let g:syntastic_auto_loc_list = 1 let g:syntastic_check_on_open = 1 let g:syntastic_check_on_wq = 0 EOF
  4. This seems interesting: https://bleibinha.us/blog/2013/08/my-vim-setup-for-scala

* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.