Click here to Skip to main content
15,882,152 members
Articles / Web Development / Node.js

Effective Shell Part 1: Navigating the Command Line

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
11 Jun 2017CPOL3 min read 9.1K   6   2
Text in the command line can quickly get unwieldy. Here are some simple tricks you can use to navigate in the command line.

Effective Shell Part 1: Navigating the Command Line

This is the first part of a series I am writing which contains practical tips for using the shell more effectively.

I can't think of a better place to start than navigating the command line. As you start to do more and more in the shell, text in the command line can quickly become hard to handle. In this article, I'll show some simple tricks for working with the command line more effectively.

Here's a quick reference diagram, the rest of the article goes into the details!

Effective Shell Part 1: Navigating the Command Line

This article, examples and diagrams are available at github.com/dwmkerr/effective-shell.

Basic Navigation

Let's assume we have a very simple command we are writing, which is going to write a quote to a text file:

Bash
echo "The trouble with writing fiction is that it has to make sense, 
whereas real life doesn't. -- Iain M. Banks" >> quote.txt  

Navigating around long lines of text is a slow process if you are only relying on the arrow keys, so take the time to learn the following shortcuts:

ActionShortcutExample
Go to beginning / end

Ctrl + a, Ctrl + b

Effective Shell Part 1: Navigating the Command Line
Go backwards / forwards one wordAlt + b / Alt + fEffective Shell Part 1: Navigating the Command Line
Delete a word / undoCtrl + w / Ctrl + -Effective Shell Part 1: Navigating the Command Line
Delete next wordAlt + dEffective Shell Part 1: Navigating the Command Line
Delete all the way to the beginning1Ctrl + uEffective Shell Part 1: Navigating the Command Line
Delete all the way to the endCtrl + kEffective Shell Part 1: Navigating the Command Line

Searching for Commands

Another indispensable command is the 'Search History' command, invoked with Ctrl + R:

Effective Shell Part 1: Navigating the Command Line

As you type, your command history is searched, the most recent commands coming first. Use the following shortcuts to complete the operation:

ActionShortcutExample
Find the next occurrenceCtrl + rEffective Shell Part 1: Navigating the Command Line
Run the commandEnterEffective Shell Part 1: Navigating the Command Line
Edit the commandRight ArrowEffective Shell Part 1: Navigating the Command Line
Stop searchingCtrl + gEffective Shell Part 1: Navigating the Command Line

Editing In-Place

These tips and tricks are helpful, but if you are working with a really long or complex command, you might find it useful just to jump into your favourite editor.

Use Ctrl + x , Ctrl + e to edit-in place:

Effective Shell Part 1: Navigating the Command Line

In a later article, I'll talk a little more about how to configure the default editor.

Clear the Screen

Probably the shortcut I use the most is Ctrl + l, which clears the screen without trashing your current command. Here's how it looks:

Effective Shell Part 1: Navigating the Command Line

Pro Tip: Transposing!

If you've mastered all of the commands here and feel like adding something else to your repertoire, try this:

Effective Shell Part 1: Navigating the Command Line

The Alt + t shortcut will transpose the last two words. Use Ctrl + t to transpose the last two letters:

Effective Shell Part 1: Navigating the Command Line

These were new to me when I was researching for this article. I can't see myself ever being able to remember the commands more quickly than just deleting the last two words or characters and re-typing them, but there you go!

Closing Thoughts

If you are ever looking to go deeper, then search the web for GNU Readline, which is the library used under the hood to handle the command line in many shells. You can actually configure lower level details of how all shells which use readline work, with the .inputrc configuration file.

The great thing about learning these shortcuts is that they will work in any prompt which uses GNU Readline. This means everything you've learnt applies to:

  1. Bash
  2. zsh
  3. The Python REPL
  4. The Node.js REPL

And probably a whole bunch more2.

All of these shortcuts should be familiar to Emacs users. There is in fact a 'Vi Mode' option for readline, which allows you to use vi commands to work with text. You can enter this mode with set -o vi, I'll likely come back to this in detail in a later article.

There's a great cheat sheet on emacs readline commands at readline.kablamo.org/emacs, which is a very useful reference if you want to dig deeper. For this article, I've tried to focus on what I think are the most useful commands (and transpose just so you can show off!).

Hope that was useful! GIFs were made with LICEcap.


Footnotes

  1. If you are using zsh, then this will clear the entire line. ↩
  2. If you know of any more, please let me know and I'll update the article! ↩

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Degryse Kris19-Jun-17 20:52
Degryse Kris19-Jun-17 20:52 
QuestionTagged as C++ / MFC? Pin
Andreas Kapust15-Jun-17 2:30
Andreas Kapust15-Jun-17 2:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.