How to Type Less and Work Faster in the Linux Terminal

Estimated read time 8 min read


Linux laptop showing a bash prompt
fatmawati achmad zaenuri/Shutterstock.com

Using Linux means—sooner or later—using the command line. But typing commands in the terminal window can be slow and repetitive. These keyboard tips will turbo-charge your terminal window experience.

The Command Line

Linux is a command-line operating system. You can choose to drop a desktop environment on top of it, such as the GNOME or KDE desktop environments, but still, underneath, there’s the text interface that hasn’t really changed since the days of the first Unix implementations in the 1970s.

In a terminal window, Linux uses a shell such as Bash to read your commands and to either act on them itself or pass them to the appropriate command or application.

New shells have been released, old shells have all but retired, and some of the long-standing shells have had updates. Regardless, what hasn’t changed is the need for users to type commands. The user must type their instructions into a command line, just like they did half a century ago.

The commands must be spelled correctly and, because they’re case-sensitive, the case of every letter must be correct too. Commands also contain all sorts of symbols and, if they involve filesystem paths, they can be extremely long. And the longer the command and the more unusual symbols it contains, the greater the risk of mistakes.

Learning the myriad Linux commands and harnessing the power of the command line is tough enough without dealing with typos and other keyboard mishaps.

The hints, tips, and techniques we’re going to show you will make using the command line a faster and more productive endeavor.

RELATED: Command Lines: Why Do People Still Bother With Them?

Tab Completion

If you provide enough letters to let the shell match a command, filename, or directory name, hitting the “Tab” key will insert the matching command, filename, or directory name on the command line for you.

In our home directory on our test computer we typed:

cd Doc

Starting to type a directory name

Then we hit the “Tab” key.

The directory name auto-completed

The rest of “Documents” was added for us. You can keep doing this to flesh out an entire path.

cd Documents/pro

Starting to type a subdirectory name

We know we have a directory called “projects” in our “Documents” directory, so typing “pro” and hitting “Tab” completes that for us, too.

The subdirectory name auto-completed

If you can’t remember what the name of the next directory starts with, hit “Tab” twice and the shell will show you a list of the possible directories. It then populates the command line with as much of the command as you’ve entered and waits for more input.

Type a few letters of the directory you want—enough to differentiate it from the other entries—and hit “Tab” to have the shell add it to your command line.

A list of subdirectories

If you don’t type enough characters to uniquely identify the directory you want, you’ll see a list of the directories that match what you’ve typed so far.

cd Documents/projects/tent-

A subset of matching subdirectories

Using “Tab” to generate directory paths is an easy way to get a productivity boost. Tab completion is also smart, it behaves differently for different commands. If you were using ls instead of cd, it knows that it should include files as well as directories.

ls Documents/projects/

A list of files and directories

Hitting “Tab” twice includes files in the possible matches.

You can use “Tab” with command names too. Let’s say you want to use the systemctl command to enable a daemon. Type “sudo sys” and hit “Tab” twice.

sudo sys

Tab completion options for "system"

You’ll see all the commands that start with “sys.”

Add “temc” to your command and hit “Tab” once more to complete “systemctl.” Now type “en” and hit “Tab.”

sudo systemctl en

Using tab completion with systemctl enable

The rest of the word “enable” is added to your command line.

"enable" autocompleted for the systemctl command

The best way to avoid typing mistakes is to not type. Use “Tab” and let the shell do it for you.

Editing Keyboard Shortcuts

As much as tab completion helps, you’ll still find yourself needing to move through the command you’ve typed and make changes.

There’s a bunch of keystroke shortcuts that’ll speed up your movements and edit on the command line. Get these locked into your muscle memory and you’ll never hold down the arrow keys again, waiting for the cursor to track back and forth.

  • Ctrl+A: Move to the start of the line. Same as Home.
  • Ctrl+E: Move to the end of the line. Same as End.
  • Alt+F: Move forward through the line one word at a time. Same as Ctrl+Right Arrow.
  • Alt+B: Move backward through the line one word at a time. Same as Ctrl+Left Arrow.
  • Ctrl+F: Move forward through the line one letter at a time. Same as Right Arrow.
  • Ctrl+B: Move backward through the line one letter at a time. Same as Left Arrow.

These shortcuts delete text.

  • Ctrl+U: Delete from the cursor position to the start of the line.
  • Ctrl+K: Delete from the cursor position to the end of the line.
  • Ctrl+W: Delete a word to the left. Same as Alt+Backspace.
  • Alt+D: Delete a word to the right.
  • Ctrl+/: Undo. Yes, the command line has an undo option.

A quick “Ctrl+U, Ctrl+K” will delete the entire line.

History Commands

Why bother retyping something that you’ve already typed once? Bash stores your previous commands in the command history, and lets you replay any command.

RELATED: How to Use the history Command on Linux

From the command line, you can use the Up Arrow and Down Arrow keys to browse through the list of commands. Hitting Enter will execute the displayed command. You can use the history command to see the entire list of commands.

history

The history command

The commands are numbered.

The output from the history command

To replay any of the commands, use the exclamation point “!” followed immediately by the number of the command you want to reuse. Don’t put whitespace after the exclamation point.

!1102

Replaying command 1102

Instead of the number of a command, you can use the start of the command itself. For example, if you use !gedit, the shell will execute the last command you used that started with “gedit.”

!gedit

Replaying a command with a search clue, not a number

To be safe, you might want to use the :p (print) modifier. This prints the command but doesn’t execute it. Again, don’t include any whitespace in the command.

!gedit:p

Printing a command instead of executing it

If the command found is the one you wanted to run, you can run it by hitting the Up Arrow key to put it back on the command line, then hitting Enter. Of course, you could hit the Up Arrow key and then edit the command before hitting Enter if it needs modifying.

Two exclamation points “!!” represent the last command. If you forget to use sudo with a command, just type “sudo !!” to rerun the last command with sudo.

sudo !!

Using !! to repeat the last command

Another useful tidbit is that you can use Alt+. (period) to attach the last word of the previous command to your command line.

Press Ctrl+R to start the search. Then type a few letters of the command you’re looking for and hit Ctrl+R.

sudo apt

Using the Ctrl+r interactive search

If a match is displayed but it isn’t the command you want, press Ctrl+R again to jump to the next match. Keep hitting Ctrl+R until you see the command you want.

Using Ctrl+r to repeat the search

Hitting Enter will execute the command, pressing Home, End, Right Arrow, or Left Arrow will let you edit the command before you run it.

Ctrl+G will exit the search without doing anything.

Other Useful Shortcuts

These shortcuts are must-haves, too.

  • cd: Takes you to your home directory. Same as the cd ~ command.
  • cd –: Jumps back and forth between your two most recent directories.
  • Ctrl+l”: Clears the terminal window. Same as the clear command, but doesn’t clog up your history.
  • Ctrl+d: Closes the terminal window. Same as the exit command, but doesn’t clog up your history.
  • Super Key+Arrow Keys: Snaps and resizes your terminal window to the left and right of your screen, to full size, and back to usual size.

How to Learn These Keystrokes

Make a shortlist of the commands that sound most useful, and write them on a piece of paper. Keep it close, refer to it, and use those keystrokes.

Once you’ve got those down, start a new shortlist.

RELATED: 37 Important Linux Commands You Should Know





Source link

You May Also Like

More From Author