Make the most of your command line environment

Unix shells: bash, tcsh, ksh, csh
Unix shells: bash, tcsh, ksh, csh | Source

Top command line annoyances

While command lines offer a powerful alternative to point-and-click graphical interfaces, they also bring their own set of annoyances. Can you relate to any of the following scenarios?

  • I just typed in this long command and only missed it by one character! How long do I have to hold down the arrow key to correct my mistake!?
  • I only took 2 minutes to check Facebook and came right back. How did my ssh session time out already?
  • If I try to download this huge file on my ssh session, how can I keep from losing my progress? I have to step away from this terminal, how can I check on it if I come in from a different ssh client?

If you relate to any of the above, I have good news for you. There's hope! In this tutorial, you will learn

  • how to tune the default behavior of ssh sessions to prevent most common timeout scenarios
  • keyboard shortcuts for better cursor control on the bash command line
  • how to use the screen virtual terminal to preserve command line state on terminal sessions

Extend the default timeout for ssh

Whether due to firewall policy or QoS enforcement or who-knows-what, connection-oriented network applications like ssh are susceptible to disruption. Secure shell is built on top of tcp connections, so the first thought might be to use tcp keepalives. However, if security is a priority, then best practice is to avoid the easily spoofed keepalive (generic to all tcp connections) and turn instead to ssh-specific options.

As mentioned in some of my other hubs, the ssh client behavior can be tuned by changing settings in ~/.ssh/config. The default ssh timeout is one such setting. To change the default timeout behavior for all ssh sessions, create a match-any-host stanza in the config file:

Host *
ServerAliveInterval 30
ServerAliveCountMax 3

Note that these options are only available to ssh version 2. This configuration instructs the client to send an empty packet over the encrypted tunnel every 30 seconds (ServerAliveInterval). After 3 such intervals (ServerAliveCountMax) the client gives up and considers the session dead.

Which is your favorite Unix shell?

  • bash
  • csh
  • ksh
  • tcsh
  • sh (the original Bourne)
  • Other (please specify with a comment below!)
  • What's this shell thing you're talking about?
See results without voting

Use emacs-style control keys for Bash CLI maneuvering

I once used emacs for a computer science class. While I never developed much appreciation for the editor, I picked up a few keyboard shortcuts. Imagine my surprise when I accidentally tried one of them out on a bash command line, and it worked!

As one of my favorite tech bloggers, Peteris Krumins, points out: this is by design. The two main control modifier keys are Meta and Control, which default to esc (Meta) and control (Control) on my Mac keyboard. I've listed a few of my favorite shortcuts below.

Some of my favorite Bash keyboard shortcuts

Key combo
Description
C-a
Move to the beginning of the line
C-e
Move to the end of the line
C-k
Delete from cursor to end of line
C-y
Paste at point of cursor
M-b
Move cursor back one word
M-backspace
Delete from cursor back one word
M-f
Move cursor forward one word
Key combos turn the command line into a more useful environment

X Windows and VNC

When X Windows first hit the command-line driven Unix environment in 1984, it was a revolutionary development. The graphical user interface was network accessible, able to separate the display of the environment from the host system. There was no visible difference between running an X application locally and viewing the output of an X application that I accessed over the network.

The downside of networked X applications is that the state of the application rested with the fate of the underlying tcp connection. If anything disrupted the connection, the application would just go away. Imagine the frustration of working on a word processor document or CAD drawing, and then losing all your work because the network failed out from under you.

VNC to the rescue!

Virtual Network Computing carried the X Windows idea forward by separating the display server from the mouse, keyboard, and viewer component. Network disruptions only interrupt the updates sent to and from the viewer, but no longer killed the entire window session. Simply restart the viewer, and once it connects back up, your document or drawing is right there where you left it!

screen with multiple windows
screen with multiple windows | Source

The screen virtual terminal saves your spot

The screen utility acts like a server for your virtual terminal session. If you don't specify any child process, screen launches with a new command line shell. With a special keystroke, you can unattach from the screen session. Specify the reattach argument, and you can reattach to that same session, and pick right back up where you left off. With another keystroke, you can set up several "windows" and switch back and forth between them. (See "screen with multiple windows" on the side bar.)

I often explain that screen is to command line what the VNC utility is to X Windows. (See the sidebar for explanation if you're not familiar with VNC or X Windows.) If you start up a screen session after accessing a remote session by ssh, then the status of that terminal window no longer depends on the ssh session but will last until the system reboots or until you quit the screen session.

I use screen for long-running command line operations. I can invoke the job from one place, and whether I detach from screen or not, I can reattach to the screen from remote access as needed. Whenever these scenarios come up, I remember how thankful I am for screen

  • compiling binary utilities from source code
  • searching through monstrous amounts of log files
  • downloading large files over small pipes

Like any well-designed Unix command line application, screen has a variety of configuration options that can be customized to suit your taste. Here's a quick summary of some of my favorite tweaks:

  • re-map the hot key from Ctrl-a to Ctrl-]
  • post the hostname in the lower left corner
  • post the time and date on the bottom row
  • list the names and numbers of the active screen windows

I can't take any credit for the config file posted below. I wish I could remember where I found it, so if you happen to drive by and recognize this wonderful achievement as your original work, please post a comment so I can attribute appropriately!

To use these tweaks on your system, create a text file in your home directory named .screenrc and copy/paste from the config posted below.

Further Reading

Quick reference on screen usage

Task
Command line
launch a new session of screen
screen
list active screen sessions
screen -ls
reattach to first available session
screen -r
reattach to named screen session
screen -dR screenName
detach from current screen
Ctrl-] d
A few tips to get you started with using the screen utility.

.screenrc

# kill startup message
startup_message off

# define a bigger scrollback, default is 100 lines
defscrollback 1024

# remap CTRL-A to CTRL-]
escape ^]]

# status bar
hardstatus on
hardstatus alwayslastline "%{rk}%H %{gk}%c %{yk}%M%d %{wk}%?%-Lw%?%{bw}%n*%f %t%?(%u)%?%{wk}%?%+Lw%?"

More by this Author


Comments 3 comments

WestelCS profile image

WestelCS 2 years ago

Things have their pros and cons. Linux does provide some really powerful shells and Bash being the most preferred, it's still a little bit of annoyance while using it. This hub has a very interesting information both for experienced Linux enthusiasts and Newbies to handle the terminal in an effective manner.


JDubya profile image

JDubya 2 years ago from United States Author

Thanks for the comment, WestelCS. You just gave me an idea for a poll on this hub - stop by and vote!


WestelCS profile image

WestelCS 2 years ago

You're welcome @JDubya

Yeah sure. :)

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working