Command Line Navigation Tips and Obscure Shortcuts, Part 1.5

Page content

Show All Available Shortcuts

$ bind -p

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

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

$ 
or
$ name-of-a-new-file.txt
# Press Ctrl+X then Ctrl+E
# For me on my BTW/Linux system ctrl+e did the job
# Your editor opens with the command
# Edit it, save, and close
# saves you from having to run "vim name-of-a-new-file.txt"
# not a huge time saver but its good to know

Clear the Screen

I use this one about once a day at least.

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

Tab Tab - Show all possible completions

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

Alt+* - Insert all possible completions into the command line. This one is the * character or shift+8.

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

Obscure but Useful

Yank (Copy) and Paste

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"
             ↑ cursor here
# Press Ctrl+K to cut from cursor to end
$ echo "this
             ↑ cursor here
# 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 ource destination
# Ctrl+E (end of line)
$ mv destination 
		        ↑ cursor here
# Ctrl+Y (paste)
$ mv ource destinations
				       ↑ cursor here

Alt+Y - Cycle through previous kills (after pressing Ctrl+Y). Not the same as ctrl+y.

Repeat Commands

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

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

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