ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Make the most of your command line environment

Updated on March 20, 2014
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?

See results

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
Move to the beginning of the line
Move to the end of the line
Delete from cursor to end of line
Paste at point of cursor
Move cursor back one word
Delete from cursor back one word
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

Command line
launch a new session of 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.


# 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%?"


    0 of 8192 characters used
    Post Comment

    • WestelCS profile image

      Tim Anthony 

      4 years ago

      You're welcome @JDubya

      Yeah sure. :)

    • JDubya profile imageAUTHOR

      Jeff Wilson 

      4 years ago from United States

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

    • WestelCS profile image

      Tim Anthony 

      4 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.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)