How to Get Started With Linux Command-Line Editing

How to Get Started With Linux Command-Line Editing

Summary

  • Modern shells support command-line editing for efficient recall and correction of commands.
  • Command-line editing is more efficient than using the shell’s history mechanism.
  • Shells like KornShell, Bash, Zsh, and Fish offer command-line editing; Emacs-style editing is recommended.

Have you used the Linux terminal and wished there was a way you could go back to previous commands without retyping everything? Modern shells let you do exactly that with command-line editing.

What Is Command-Line Editing?

Command-line editing is a way to recall and correct commands by using text-editing commands in the shell. This is similar to using commands in a text editor. Because text editors are commonly used in the terminal along with the shell, many modern shells provide key bindings similar to those of the major text editors, both Emacs and Vi. These will be covered later in this article.

You can move around the command line or recall previous entries the way you’d move around a text file in an editor.

Why Use Command-Line Editing?

Manual page for zsh's command-line editing feature.

The main reason to use command-line editing is that it’s a lot more efficient than using the shell history mechanism. While the shell history commands introduced with the BSD C shell were welcome when they were developed in the 1980s, as with a lot of shell constructs, they can be cumbersome to remember.

When Unix was created, the shell history was a scroll of paper, because the original terminals were teleprinters, also known by the brand name Teletype. The shell history mechanism invented for the BSD C shell was also designed for these terminals, as they were still common through the mid-late 1970s. Later video terminals made it possible to edit and erase characters, and some programmers had the bright idea that you should be able to go through your command history and be able to edit it at will.

Related

History of the Unix Terminal: How Video Killed the Printer Star

We’ve come a long way from Teletypes.

The shell history commands are symbolic and unintuitive. Command-line editing mechanisms depend on keybindings that you might already know, such as Emacs or Vi/Vim.

You can also jump around the command line more easily with the command-line editing capabilities than you can ordinarily.

If you see that you’ve made a mistake earlier on in your command line, you can jump to the beginning to fix it. If you’re trying to remember a command you ran, you can run a search to find it. You can jump to a previous entry and then modify it.

If you want to modify a command, you can just move up and change the commands or file names you want to use, and then execute the command.

Think of using command-line editing as moving through your shell command history as if it were a text file.

Which Shells Offer Command-Line Editing?

Many of the modern shells available on Linux systems support command-line editing. The shell that popularized the concept was the KornShell, originally written in the 1980s at Bell Labs. This shell, named after its creator David Korn, offered the ability for users to run commands similar to those used in the Emacs and Vi editors. This pattern has largely been in use today on other shells.

Bash has also offered command-line editing from the get-go, as well as Zsh. With Bash, the most common default shell on Linux distributions, most Linux distros offer it out of the box. Other shells, like fish, also offer it. tcsh, which is inspired by the C shell, also offers Vi and Emacs features. This means that learning command-line editing techniques will pay off no matter what shell you use.

The choice of which editor mode to use is typically set in an environment variable or the shell’s configuration command.

Editing Emacs Style

The easiest way to get started with command-line editing is to use the Emacs mode, even if you use another editor. This is also the default style on most shells. The reason is that this seems more of a natural mode, since Emacs doesn’t distinguish between command and insert modes the way Vi and Vim do. Even though my favorite editor is Vim, I just think its command set is better-suited to editing in that program, and it doesn’t work as well in the shell. I’ll demonstrate command-line editing in Zsh, which is my shell of choice, but they should also work in other shells like Bash.

To set up editing Emacs-style, you’ll use one of two commands, depending on the shell you have.

In Bash, type:

         set -o emacs
    

Or in Zsh:

        bindkey -e
    

On most modern shells, Emacs-style editing is the default anyway.

The first commands you should master are moving back and forth through your shell history. You can use the up and down arrow keys. You can also use the Ctrl+p and Ctrl+n to move forward and back through your history. You can remember these mnemonically since “p” stands for “previous” and “n” stands for “next.”

Use lowercase letters, because uppercase characters are significant in the shell and everywhere else in Linux.

You can also use the arrow keys to move forward and back on the command line. Ctrl+f and Ctrl+b move forward and back. Again, you can remember these with the mnemonics “forward” for “f” and “backward” for “b.” It’s better to try them a few times to develop the muscle memory, especially for things you do frequently. Using the arrow keys or the Ctrl+letter combinations is a matter of taste and comfort.

Moving the cursor on the Debian Zsh command line.

You can go back and forth in your history, but what’s useful is to be able to search. To search backward in your history, press Ctrl+r. Then you can start typing what you want to look for, and results will appear in real time that contain the search string. The search string will be highlighted. Press Ctrl+r to repeat the backward search. Forward search is Ctrl+f and it works the same way. When you get to a previous command, you can either press Enter to run it again or you can edit it. You might want to do this if you want to use a different command or file name.

Emacs-style backward search in Zsh.

You can delete previous characters using the backspace key, but you can also delete the next one by pressing Ctrl+d. To delete a word, press Meta+d. To delete a whole line, press Ctrk+k.

One move I use a lot is jumping to the beginning and end of a line. To go to the beginning of a line, press Ctrl+a. To go to the end, press Ctrl+e. These commands are handy when I realize I want to change something in the line I’m working on, but the cursor is closer to one end of the line.

You can also move by word boundaries instead of just by individual cursor movements. You can use the Meta key, which is either the Alt or the Escape key. The Meta key modifies the previous commands. For example, to move forward by one word, press Meta+f, and to go back, press Meta+b.

Editing VI Style

You can use Vi-style keybindings in shells that support command-line editing, but even though I’m a Vim user, I would suggest considering Emacs commands. The main reason is that, unlike in Vim, there’s no visual indicator that shows if you’re in insert or command mode. I find Vi-style commands do not work as well in the shell. Emacs seems better-suited to command-line editing, and the use of Emacs-style commands as a default seems justified.

Related

11 Vim Tips That Will Save You Hours of Editing Time

Be a Vim pro.

Still, if you do want to use Vi-style commands, you can set them up in one of two ways:

        
set -o vi
bindkey -v

In Vi mode, you’ll start in “insert mode,” where you type shell commands. To exit into command mode, where you move around, press Escape. To move back, press h. To move forward, press l. To move up in previous history entries, press h. To move down, press j.

Moving the cursor in zsh's Vi command-line editing mode.

You can move forward by words with the w key, and back with the e key in input mode.

You can search backward with the ? key in command mode, and forward search using the / key, similar to the forward and backward search commands.

Backward search using Vi command-line editing mode in Zsh.

To delete text in command mode, position the cursor over it and press the d key. To delete a word, press dw. To delete a whole line, use “dd” by pressing the d key twice.


These commands are only a sampling of what’s available for command-line editing in modern Linux shells. With enough practice and finger training, you’ll be able to fly through your command history and re-run and edit commands at will. You’ll dramatically speed up your use of the Linux terminal.

Leave a Comment

Your email address will not be published. Required fields are marked *