Command Line Navigation Part 2

Page content

Change Case

Alt+U - Make word uppercase from cursor to end of word

$ sudo systemctl start nginx
                        ↑ cursor here
# Press Alt+U
$ sudo systemctl start NGINX

Alt+L - Make word lowercase from cursor to end of word

Alt+C - Capitalize word from cursor to end of word

$ my important file.txt
  ↑ cursor here
# Press Alt+C
$ My important file.txt

History Shortcuts

Search Command History

This is a very powerful tool that I too use almost everyday.

Ctrl+R - Search backward through command history (reverse search)

# Press Ctrl+R
(reverse-i-search)`': 
# Start typing
(reverse-i-search)`sys': sudo systemctl restart nginx
# Press Enter to execute, or Ctrl+R again to find next match

This is probably the most useful shortcut you’ll learn. Instead of pressing up arrow 50 times, just Ctrl+R and type a few letters.

$ history | grep sys
# Will show all occurences of commands ran with sys in the name and their line number.
6557 cd sys_admin/
6499 sudo systemctl daemon-reload
11496 sudo systemctl start lemond
...
# You can run these command again without typing in all the words by running with bang:
$ !6557
# This will run the command cd sys_admin/

Another way to run this similarly would be to run:

First run ctrl+r word to reverse search then Ctrl+S to search forward through command history

Note: Ctrl+S might freeze your terminal (XOFF). If it does, press Ctrl+Q to unfreeze. Or disable it with: stty -ixon until you logout. You can add stty -ixon to your .bashrc to make it permanent since most people don’t use screen freeze much anymore.

These ones are cool, I just wish they were more vim-like:

Ctrl+P - Previous command (same as up arrow, but you don’t move your hand)

Ctrl+N - Next command (same as down arrow)

Alt+< - Jump to first command in history

Alt+> - Jump to last command in history. What this did to my terminal was jump me to the very end to an empty tty and not the last command.

Use Previous Command Arguments

Alt+. (dot) - Insert last argument from previous command

$ vim /etc/nginx/sites-available/blog.conf
# Oops, need to edit with sudo
$ sudo vim 
           ↑ cursor here
# Press Alt+. 
$ sudo vim /etc/nginx/sites-available/blog.conf
           ↑ last argument inserted

Press Alt+. multiple times to cycle through previous arguments.

!$ - Reference last argument of previous command In bash $ is used as a variable to reference something. In this case it will be the ! character which is the history expansion character.

$ mkdir /var/www/newsite
$ cd !$
# Expands to:
$ cd /var/www/newsite

!* - Reference all arguments from previous command

$ ls file1.txt file2.txt file3.txt
$ rm !*
# Expands to:
$ rm file1.txt file2.txt file3.txt

!! - Repeat entire previous command

$ apt update
Permission denied
$ sudo !!
# Expands to:
$ sudo apt update

!:1 - Reference first argument of previous command

!:2 - Reference second argument, and so on…

My Most Used Shortcuts

After years in the terminal, these are the ones I actually use every day:

  1. Ctrl+R - Search history (use this constantly)
  2. ctrl+p - Previous command (same as up arrow)
  3. ctrl+k - Next command (same as down arrow)
  4. Ctrl+A / Ctrl+E - Jump to start/end of line
  5. Ctrl+U - Clear line (panic button)
  6. Ctrl+W - Delete last word
  7. Alt+. - Insert last argument
  8. Ctrl+L - Clear screen
  9. !! - Repeat last command. Especially when running sudo !! after i forgot to enter in sudo at the beginning
  10. Ctrl+X, Ctrl+E - Edit in vim or default editor
  11. Ctrl+_ - Undo
  12. Alt+T - Swap words (for when I type cp or tar arguments backwards)

Practice Makes Permanent

Don’t try to memorize all of these at once. Pick 2-3 that solve a problem you have:

  • Always typing sudo !! after a permission error? That’s one to learn.
  • Constantly messing up long file paths? Learn Alt+B (move one character backwards. Same as left arrow) and Alt+F (same as right arrow).
  • Tired of pressing backspace 50 times? Learn Ctrl+W and Ctrl+U.

Use them for a week, and they’ll become muscle memory. Then come back and learn 2-3 more.

Quick Reference Card

# Navigation
Ctrl+A          Beginning of line
Ctrl+E          End of line
Alt+B           Back one word
Alt+F           Forward one word
Ctrl+]          Jump to character forward
Ctrl+Alt+]      Jump to character backward

# Editing
Ctrl+W          Delete word backward
Alt+D           Delete word forward
Ctrl+U          Delete to line start
Ctrl+K          Delete to line end
Ctrl+H          Delete char backward
Ctrl+D          Delete char forward
Ctrl+T          Transpose chars
Alt+T           Transpose words
Ctrl+_          Undo
Alt+R           Revert line

# History
Ctrl+R          Search backward
Ctrl+S          Search forward
Ctrl+P          Previous command
Ctrl+N          Next command
Alt+.           Last argument
!!              Previous command
!$              Last argument
!*              All arguments

# Advanced
Ctrl+X, Ctrl+E  Edit in editor
Ctrl+L          Clear screen
Alt+#           Comment line
Ctrl+C          Kill process
Ctrl+Z          Suspend process
Ctrl+D          EOF/Exit

# Copy/Paste
Ctrl+K          Cut to end
Ctrl+Y          Paste (yank)
Alt+Y           Cycle yanks