Video

Want to see the full-length video right now for free?

Sign In with GitHub for Free Access

Notes

What is a Line Editor?

When you enter commands in a shell, command line, or REPL, you are using a line editor. One of the most common line editors is Readline (you use Zsh you may also use the Zsh Line Editor, zle, which is functionally equivalent). Line editors may seem simple, but they possess powerful features that you can use to improve your workflow.

Browsing History

One of the most commonly-used features of Readline (that you might associate with your shell or REPL rather than Readline) is history browsing. Use the <up> key to spool through previously entered commands and the <down> key to go back to more recent commands.

With <Control-r> you can perform a search through previous history. Searches do not have to start at the beginning of the line! Search for any fragment of the line you're seeking.

Control-a and Control-e are familiar to Emacs users. You can use them to jump to the start and end of a line. (OS X users can use these keystrokes in dialog boxes and address bars!) Meta-f and Meta-b can jump forwards and backwards by one word. Meta will be the alt key on most keyboards, but you may need to edit your terminal emulator's configuration, particularly in OS X. These keystrokes are a more elegant way to move around the line than the brute force of repeatedly pressing the arrow keys.

Other Emacs keybindings are available in Readline. Control-w can delete a word backwards from the cursor, and Meta-d can delete a word forwards from the cursor.

Readline supports a vi-mode that supports many of vi's motions, but even if you're a hardcore vim user this mode may not be ideal for your workflow. As the mode is vi-like and doesn't include vim's keybindings, some commands such as u to undo don't behave in the way you expect.

Even if you stick to the default Emacs mode, you can leverage vim for larger, more complicated line edits. Both bash and zsh support keystrokes that allow you to take the current line, pop out to an editor with the line in the buffer, and then take the contents on write and quit and bring them back into the line.

Other useful tricks: Control-l clears the screen (you may have a conflict if you use vim-tmux-navigator). Readline has an undo feature! Control-_ undoes the last action, which can be extremely useful if you're editing a long line and make one change too many.

Readline has its own copy-paste functionality, but be aware that in Emacs/Readline that "yank" has the opposite meaning that it has in vim, in that it refers to the pasting action rather than the copying action. Yanking can be performed with Control-y to paste the last killed text and Meta-_ to paste the last argument of the previous command.

The Meta-t key transposes two characters and Control-t transposes two words. Get confused and can't remember which is which? If you perform the wrong action, perform it again and undo your mistake.

Customizing the Line Editor

You could customize your command-line keybindings in your shell configuration, but inputrc allows for Readline customization that will carry over into non-shell Readline uses such as REPLs (IRB, etc.). Here's George's inputrc where he makes a few personal customizations, including enabling menu completion with Control-n to match the mapping in vim.

Further resources