Readline Tips & Using the Command Line with Bash

Intro

Bash uses the readline library to provide command line editing features

Readline provides both vi and emacs styles of keybindings. By default, emacs mode is on.

Re-run the last command

Emacs style

Press kbd:[C-p] (“p” is the mnemonic for “previous”) to bring the last command line into the prompt and then press kbd:[Enter] to run the command.

Vi style

With vi style, even if we are in normal mode, as soon as we submit a command, we always end up in insert mode again.

Enter normal mode with kbd:[Esc] or kbd:[C-[], then press kbd:[k] to bring the last command line into the prompt, and execute it with kbd:[Enter].

If you need to re-run the same command again (like when you are doing TDD and changed the test or implementation), you’ll have enter “normal mode” before kbd:[k] brings the previous command into the prompt instead of inserting kbd:[k] into the prompt. If that is annoying, you can use kbd:[Alt-k] instead.

Often, kbd:[Alt] (also known as kbd:[Meta]) sends kbd:[Esc] in most environments, so kbd:[Alt-k] is akin to kbd:[Esc] followed by kbd:[k].

Another way to run the last command with bash & readline is with kbd:[!!]. gg