Environmental Engineering Tools Special — Tmux/Screen

Li Yin
Algorithms and Coding Interviews
7 min readApr 6, 2019

Introduction

Why do I feel the necessity to have a special dedicated to engineering environmental tools such as vi/vim, linux command, ssh, tmux/screen, ftp, iPython/Jupyter Notebook? These are usually essential workflow needed to perform engineering tasks.

Personally, I struggled badly first from the lack of knowledge and experience to use these tools in my second internship as a Computer Vision based Research Intern in at JD.com’s AI lab in Mountain View. Compared with my mentor or any other colleagues, my work progress was slow due to problems: (1) cant perform editing in vi/vim efficiently which lead me to write Python code to edit file, while it is slow due to writing of code and executing of the program.

(2) Write code in Python instead of iPython/ Jupyter notebook. For debugging, playing simple code, iPython/Jupyter notebook is more convenient in printing code, making notes, showing previous results, and saving the previous state which makes it easier to debug.

(3) I worked remotely from a Macbook Pro to connect on a Linux Server. However, I forgot to use sshfs to mount the coding directory to local, I used github to sync instead. Using github to sync is such a huge pain due to my entry level knowledge about github commands. I was constantly stuck with git push and git pull , and wasted even more time when there is oversized file that is added to commit.

(4) I could not use screen well. Doesnt know how to split a screen, create a new session. The problem was I used many local terminals to connect on the server using ssh. I constantly got confused by multiple terminals and what purpose each one is for.

In this internship, I saw how efficient my mentor works on his machine. I decided to learn from him, and to learn more so that I can be more comfortable and more efficient which turned out it was a very good investment of my time to learn these non-task performing environmental tools.

Now, let us get started. The content will follow such fashion: simple tutorial with the most useful knowledge I filtered out, and links to more resource.

Tmux/Screen

Software engineers now constantly need to work on Server or even Cloud environment other than on local machine as the booming of big data and deep neural network. No matter if you like it or not, we need to work with the command line. And being able to work from the most primary tool terminal decides our productivity in some degree. Other than using the default terminal comes with your platform and system, there are third-party tools help us to manage terminal sessions, split panes in order to work more efficient.

The first one we are going to introduce is Tmux, the terminal multiplexer. First lets brief on what it can do and how it helps:

Brief on Functionalities

Tmux has three levels of hierarchy when it comes to organizing views: Sessions, windows, and panes.

Windows and Panes: Within one terminal window you can open multiple windows and split-views (called “panes” in tmux lingo). Each pane will contain its own, independently running terminal instance. This allows you to have multiple terminal commands and applications running visually next to each other without the need to open multiple terminal emulator windows.

Session: On top of that tmux keeps these windows and panes in a session. You can exit a session at any point. This is called “detaching”. tmux will keep this session alive until you kill the tmux server (e.g. when you reboot)2. This is incredibly useful because at any later point in time you can pick that session up exactly from where you left it by simply “attaching” to that session.

When you lose your ssh connection the tmux session will simply be detached but will keep running on the server in the background including all the processes that run within your session. To continue your session simply ssh to the server again and attach to the running session.

Prefix

Prefix is a shortcut to invoke tmux management inside of session, which is C-B , short for Ctrl+B .

Step 1: Installment

Step 2: Learn Session

Because I tend to isolate projects to a single session, allowing me to have a complete context switch when needed, I tend to name them the project I am working on, and force their default folder to the project folder. You can switch between sessions using tmux-s, rename them using tmux-$, and set their default folder by running tmux set default-folder $(pwd) inside the session.

We should be able to manage session includes creation, detachment, attachment, naming, termination, checking both out side and inside of the session:

Outside of Session

Normally we use tmux commands to manage sessions outside of sessions in the terminal.

Inside of Session

We use C-b to invoke session management to be able to type in the :commands same as the outside of session. Such as kill current session or new more sessions.

Also, there are other commands such as:

s  list sessions
$ name session

Create a Session: We can simply use tmux to start a new session with an index number as its default name. For example, the name our my session is [0] , and it also shows the current directory.

Also, we can name this session when we are creating the session with tmux new -s session_name .

Rename a Session: Inside of session, lets rename this session use C-b , and then input $ , and then type in the session name you want.

Detach a Session: Now, lets try to detach from this session: Use C-b , and then b to detach from it. Note: to detach and kill the current session, use Ctrl+d .

List all Sessions: tmux ls

Attach to a Session: tmux a -t session_name. Or use tmux a to attach to the last session.

Kill a Session: tmux kill-session -t session_name . Or inside of the session, use C-b , then input :kill-session .

Switch between Sessions: before this, we created another session with name remote_session. Inside of the local_project session, we can use C-b and then type s to invoke the following screen:

Set default working directory

Step3: Learn Windows

Now that we have known how to manage different session, we try to do windows. When we new a window, they automatically has the same working environment include current directory:

C-b c: Create a new window (appears in status bar)C-b 0: Switch to window 0C-b 1: Switch to window 1
Left is window 0 and on the right is window 1

It is kind of hard to notice the name of each window, so

Rename the window: C-b $, for instance, we rename one window to w1 .

List Windows: C-b w

More useful commands:

n  next window
p previous window
f find window
, name window
& kill window
x kill current window

Step 4: Learn Panes

Splits are usually the reason people find tmux in the first place. Two commands are used for splitting:

C-b %  :split window with another vertical pane
C-b " :split window with another horizontal pane

Note: sometimes on Mac, we need to use shift+% instead.

Now, we see multiple panes, we can use C-b with arrows to switch between panes.

C-b o  swap panes
C-b q show pane numbers
C-b x kill pane

Other Commands

Scroll down the screen:

C-b [

And type q to quit.

Use fn +arrows to scroll by pages.

More Resource

Git Repo

--

--