My favorite tricks for navigating the Linux terminal faster

Make your day less hectic and speed up your work on the Linux command line with these tips.
Register or Login to like

One of the advantages of working in a terminal is that it's faster than most other interfaces. Thanks to the GNU Readline library and the built-in syntax of shells like Bash and Zsh, there are several ways to make your interactions with the command line even faster. Here are five ways to make the most of your time in the terminal.

1. Navigate without the arrow keys

While executing commands on the command line, sometimes you miss a part at the beginning or forget to add certain tags or arguments toward the end. It's common for users to use the Left and Right arrow keys on the keyboard to move through a command to make edits.

There's a better way to get around the command line. You can move the cursor to the beginning of the line with CTRL+A. Similarly, use CTRL+E to move the cursor to the end of the line. Alt+F moves one word forward, and Alt+B moves one word back.

Shortcuts:

  • Instead of Left arrow, left, left, left, use CTRL+A to go to the start of the line or Alt+B to move back one word.
  • Instead of Right arrow, right, right, right, use CTRL+E to move to the end of the line, or Alt+F to move forward a word.

2. Don't use the backspace or delete keys

It's not uncommon to misspell commands. You might be used to using the Backspace key on the keyboard to delete characters in the backward direction and the Delete button to delete them in the forward direction. You can also do this task more efficiently and easily with some helpful keyboard shortcuts.

Instead of deleting commands character by character, you can delete everything from the current cursor position to the beginning of the line or the end.

Use CTRL+U to erase everything from the current cursor position to the beginning of the line. Similarly, CTRL+K erases everything from the current cursor position to the end of the line.

Shortcuts:

  • Instead of Backspace, use CTRL+U.
  • Instead of Delete, use CTRL+K.

3. Execute multiple commands in a single line

Sometimes it's convenient to execute multiple commands in one go, letting a series of commands run while you step away from your computer or turn your attention to something else.

For example, I love contributing to open source, which means working with Git repositories. I find myself running these three commands frequently:

$ git add
$ git commit -m "message"
$ git push origin main

Instead of running these commands in three different lines, I use a semi-colon (;) to concatenate them onto a single line and then execute them in sequence.

Shortcuts:

  • Instead of:
    $ git add .
    $ git commit -m "message"
    $ git push origin main

    Use:

    $ git add .;git commit -m "message";git push origin main
  • Use the ; symbol to concatenate and execute any number of commands in a single line. To stop the sequence of commands when one fails, use && instead:
    $ git add . && git commit -m "message" && git push origin main

4. Alias frequently used commands

You probably run some commands often. Sometimes, these may be lengthy commands or a combination of different commands with the same arguments.

To save myself from retyping these types of commands, I create an alias for the commands I use most frequently. For example, I often contribute to projects stored in a Git repository. Since I use the git push origin main command numerous times daily, I created an alias for it.

To create an alias, open your .bashrc file in your favorite editor and add an alias:

alias gpom= "git push origin main"

Try creating an alias for anything you run regularly.

Note: The .bashrc file is for users using the Bash shell. If your system runs a different shell, you probably need to adjust the configuration file you use and possibly the syntax of the alias command. You can check the name of the default shell in your system with the echo $SHELL command.

After creating the alias, reload your configuration:

$ . ~/.bashrc

And then try your new command:

$ gpom

Shortcut:

Instead of typing the original command, such as:

$ git push origin main

Create an alias with the alias declaration in .bashrc or your shell's configuration file.

5. Search and run a previous command without using the arrow keys

Most terminal users tend to reuse previously executed commands. You might have learned to use the Up arrow button on your keyboard to navigate your shell's history. But when the command you want to reuse is several lines in the past, you must press the Up arrow repeatedly until you find the command you are looking for.

Typically the situation goes like this: Up arrow, up, up, up. Oh, I found it! Enter.

There is an easier way: You can search your history one step at a time using the history command.

When you use the history command, the list of commands appears with a number beside each. These numbers are known as the history-number of the command. You can type !{history-number} on your terminal to run the command of the corresponding number.

Shortcuts:

  • Instead of Up arrow, up, up, up, Enter, type history, and then look for the history-number of the command you want to run:
    $ !{history-number}
  • You can also perform this task a different way: Instead of: Up arrow, up, up, up, Enter, use CTRL+R and type the first few letters of the command you want to repeat.

Command-line shortcuts

Shortcut keys provide an easier and quicker method of navigating and executing commands in the shell. Knowing the little tips and tricks can make your day less hectic and speed up your work on the command line.


This article originally appeared on Red Hat Enable Sysadmin and has been republished with the author's permission.

Anamika-Opensource.com
Anamika is a budding developer from India. She is an opensource enthusiast and a computer science undergrad who enjoys learning about new technological innovations. You can talk to her about anything, whether it's Python, her favorite programming language, Linux (without a doubt), or, of course, opensource.

8 Comments

Just to clarify, as the article correctly states:

  • Ctrl+A: Move to the start of a line
  • Ctrl+E: Move to the end of a line
  • Alt+F: Move foward by a word
  • Alt+B: Move back by a word

Additionally:

  • Ctrl+F: Move forward by a character
  • Ctrl+B: Move back by a character

These shortcuts are provided by GNU Readline, and adds Emacs-like navigation to a shell.

You can read more about it with this command:

info readline --node "Readline bare essentials"
 

I never knew what Ctrl-R did, I always just hit it by accident! Who knew it was useful?

A short script can also save keyboard strokes. In task warrior, in order to mark a task as complete, the normal instruction is, "task 35 done" where 35 is the ID of the task. I wrote a short script. Now, I type "tld 35"

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.