Command Line Navigation Tips and Obscure Shortcuts, Part 2

Command Line Navigation Tips and Obscure Shortcuts, Part 2
Page content

Advanced Tricks

Edit Command in Your Editor

Ctrl+X, Ctrl+E - Open current command in your default editor (usually vim or nano)

$ 
# Press Ctrl+X then Ctrl+E
# Your editor opens with the command
# Edit it, save, and close
# The edited command runs

Real use case: You’re building a complex command with lots of flags. Instead of fighting with the one-line editor, pop it into vim, format it nicely, then run it.

Clear the Screen

Ctrl+L - Clear the screen (same as typing clear, but faster)

The screen clears, but your command history is preserved. Scroll up to see previous commands.

Process Control

Ctrl+C - Interrupt (kill) the current command

Ctrl+Z - Suspend the current command (puts it in background, paused)

$ vim largefile.txt
# Need to check something real quick
# Press Ctrl+Z
[1]+  Stopped                 vim largefile.txt
$ ls
# Do your thing
$ fg
# Returns to vim

Ctrl+D - Send EOF (End of File) or exit shell if line is empty

Execute Without Running

Alt+# - Comment out the current line and press enter

$ rm -rf /important/directory
# Wait, let me think about this first
# Press Alt+#
$ #rm -rf /important/directory
# Command is added to history as a comment, not executed

Later you can Ctrl+R to find it and remove the #.

Terminal Control

Freeze and Unfreeze Output

Ctrl+S - Freeze terminal output (XOFF)

Ctrl+Q - Unfreeze terminal output (XON)

Real use case: Output is scrolling too fast to read? Ctrl+S to pause, read it, then Ctrl+Q to continue. (Though honestly, I usually just pipe to less instead)

Autocomplete

Tab - Autocomplete filename/command (you already know this one)

Tab Tab - Show all possible completions

Alt+? - Show all possible completions (same as Tab Tab)

Alt+* - Insert all possible completions into the command line

$ ls file*.txt
# Press Alt+*
$ ls file1.txt file2.txt file3.txt
   ↑ all matches inserted

Obscure but Useful

Yank (Copy) and Paste

Yes, bash has its own clipboard separate from your system clipboard.

Ctrl+K then Ctrl+Y - Cut line, then paste it back

$ echo "this is a test"
# Press Ctrl+K to cut from cursor to end
$ echo 
# Press Ctrl+Y to paste it back
$ echo "this is a test"

Ctrl+W then Ctrl+Y - Cut word, then paste it back

This is useful for moving parts of a command around:

$ mv source destination
      ↑ cursor here
# Ctrl+W (cuts "source")
$ mv  destination
# Ctrl+E (end of line)
$ mv destination 
# Ctrl+Y (paste)
$ mv destination source

Alt+Y - Cycle through previous kills (after pressing Ctrl+Y)

Repeat Commands

Alt+[number] then a command - Repeat that command [number] times

# Press Alt+5 then type #
$ #####
↑ inserted 5 hashes

Show All Available Shortcuts

Ctrl+X, Ctrl+H - Show all bash shortcuts (requires bash 4.0+)

Or use the command:

$ bind -p

This shows every single shortcut that bash knows about. There are hundreds.

Customize Your Own Shortcuts

You can create your own shortcuts in ~/.inputrc:

# ~/.inputrc

# Make Ctrl+Backspace delete previous word
"\C-h": backward-kill-word

# Make up arrow search history based on what you've typed
"\e[A": history-search-backward
"\e[B": history-search-forward

# Case-insensitive tab completion
set completion-ignore-case on

# Show all completions immediately
set show-all-if-ambiguous on

After editing, reload with:

$ bind -f ~/.inputrc