You probably spend a lot of time on CAEN, or SSHed into CAEN, or working on a terminal on your local machine. It’s probably worth learning a few keyboard shortcuts so that you can spend less time fiddling with your terminal and more time programming.

Some terminology: the shell is the program that you use to interact with programs on CAEN and the files in your home directory. It’s often used interchangeably with terms like terminal, command line, and command prompt.

The default shell on CAEN is bash. In fact, it’s the default shell on many systems, such as OS X and many breeds of Linux. There are other shells; many people swear by zsh, so feel free to try another one out. The tips listed here are compatible with both bash and zsh, and probably other shells as well.

You don’t need to read this document from beginning to end. Scan through the headings and look at any appealing sections. If you had to read only one thing, it should be tab-completion.

Keyboard shortcuts

bash uses Emacs keybindings by default, so if you happen to use Emacs then you probably already know what to do.

Tab-completion

You can tab-complete file and directory names in bash. For example:

$ cd e<tab>
$ ./p<tab> < te<tab>-1<tab>

might be equivalent to

$ cd eecs281
$ ./program < test-1.txt

Tab-completion saves time and helps you to avoid spelling mistakes.

If a filename is ambiguous, you may see a listing of filenames, like this:

$ cd f<tab>
foo1/  foo2/

You can continue typing the filename and then press tab again to fully complete the filename.

Searching through history

Everyone who’s used a shell has at some point in their life spammed the up-key to find their last g++ command, or last program invocation, or whatever else kids do nowadays on their terminals. Fortunately, bash has a command for exactly this. At your terminal prompt, type Ctrl-R. It should look like this:

(reverse-i-search)`':

Then start typing. For example, if I type g++ it might look like this:

(reverse-i-search)`g++': g++ main.cpp

The thing after the colon is the first match. Tap Ctrl-R again to see the match before that, and so on.

When you find the command you want, press Enter to run it. You can also edit the command before running it by pressing an arrow key or text manipulation key (see the next section).

Text manipulation

Type Ctrl-W to delete the word before the cursor. Type Ctrl-A to go to the beginning of the line (handy if you forgot a sudo or a ./), and Ctrl-E to go to the end of a line.

Screen manipulation

To exit your terminal, you can type Ctrl-D to send EOF (end-of-file). You can also do this anywhere else you want to signify the end of a file. One useful place is when you’re done typing things into stdin.

Instead of pressing Enter many times or running the clear command, you can just type Ctrl-L to clear the screen. (I personally remember it because Ctrl-C for “clear” is taken, so we use the next letter in the alphabet.) If you do this, you might remark that it’s hard to scroll up to the beginning of your compiler output — see the following section on pagers for dealing with that.

Scrolling through program output

Sometimes, commands produce a lot of output, and it’s a pain to scroll through it all. We can use a program called a pager to peruse it at our leisure. One ubiquitous pager is less.

Viewing files

To view the output of a file with less, invoke less with the filename:

$ less my-file.txt

You’ll get output like this. The colon at the bottom is for less’s status line.

You probably spend a lot of time on CAEN, or SSHed into CAEN, or working on a
terminal on your local machine. It's probably worth learning a few keyboard
shortcuts so that you can spend less time fiddling with your terminal and more
time programming.

First, some terminology: the *shell* is the program that you use to interact
with programs on CAEN and the files in your home directory. It's often used
interchangeably with terms like *terminal*, *command line*, and *command
prompt*.

<aside class="aside-info">
:

Scrolling: Use up and down. To scroll a page at a time, use PgUp and PgDn. You can also use Vim keybindings: I often use Ctrl-U and Ctrl-D to go up and down pages.

Quitting: Type q.

Searching: Type / and then your search string to search forward. Use ? to search backward instead. (Notice that ? is just Shift-/.)

Jump to top/bottom: Type G to go to the bottom of the file, and g to go to the top. (These are similar to Vim keybindings. If you forget which is which, try both: one is bound to perform the desired action.)

Viewing command output

If you want to view the output from your program, or the output from your compiler, you can pipe directly into less, instead of saving the output to a file and then reading it:

$ ./my-program | less

This only forwards stdout to less. If you want to forward stderr as well, you can use the |& operator.

$ g++ main.cpp |& less

Now you can use all the less commands from the previous section to scroll and search through your output.

Showing file differences

If you want to run tests and show differences automatically, take a look at Painless Automated Testing.

The diff command takes two files and tells you how they’re different. For example:

$ diff file-1 file-2
3c3
< Different line 1
---
> Different line 2

If there’s a lot of changes, you might want to pass the output from diff into less, as specified above.

Better still, you can use the vimdiff command for a convenient, colorized side-by-side diff:

$ vimdiff file-1 file-2

You can use all of the less keybindings you learned above, except that to quit, you need to type :qall. You can also use any other Vim keybindings you know, since this is actually Vim.

Highlighting differences in vim.

Multiple terminals at once

Many people are okay with having multiple terminal windows open, so this section won’t be useful to them. But if you hate having to SSH into CAEN for every new terminal window you open, or you just want to look cool, you can use a terminal multiplexer. A terminal multiplexer will let you have multiple sessions at once. CAEN has screen and tmux installed; we’ll talk about tmux since it’s mostly better.

Using windows

Launch tmux in your terminal on CAEN. You should get a new, blank screen, except with a status line at the bottom. (It’ll say something like 0:bash.)

A new tmux session.

Run a simple command, like echo hi, and see that it works the same as your regular terminal.

Getting help: Run Ctrl-b ? (which means Ctrl-b, then lift up your fingers, then ?) to get a list of current keybindings. Press q to exit that screen.

Creating windows: Use Ctrl-b c. (Make sure to stop pressing Ctrl before pressing c.) This creates a new, empty terminal window, which you can run different commands in. You can see all the open windows in the statusline at the bottom.

Two tmux windows are open at once.

Switching windows: Use Ctrl-b n and Ctrl-b p (for next and previous). You can jump directly to a window with Ctrl-b followed by the index of the window (for example, Ctrl-b 0 to jump to the window labelled 0).

Destroying windows: If you want to close a window, just exit the shell. You can do this with the exit command, or you can type Ctrl-d at an empty prompt. When all windows are closed, tmux will exit.

Examining output: If you want to scroll up and see old output, you can type Ctrl-b PgUp and Ctrl-b PgDn. You can search forward through old output with Ctrl-b / and backwards with Ctrl-b ?. This can be convenient if you forgot to pipe your output into a pager or a file.

Scroll through old output.

Using panes

You can also show more than one shell on the screen at once, by using “panes”. Type Ctrl-b " to split the current window into an upper and lower pane. Type Ctrl-b % to split the current window into a left and right pane.

Multiple tmux panes open at once.

To switch between panes, use Ctrl-b followed by an arrow key.

Panes will be destroyed when the shell exits, just the same as windows.

Binding keys

To see all of the active keyboard bindings available for tmux, run Ctrl-b ?.

You can rebind all of these actions, which is fortunate since Ctrl-b is not very ergonomic. Create the file ~/.tmux.conf if it doesn’t already exist, and add line set-option -g prefix C-a. This sets your “prefix” key, which is Ctrl-b by default, to be Ctrl-a. You can set it to whatever you want, of course.

tmux is quite customizable. Take a look at the internet, the man-pages, or some example .tmux.conf files if you want more inspiration. Here’s mine, although I wouldn’t recommend that you copy it verbatim: https://github.com/arxanas/dotfiles/blob/master/.tmux.conf.