Command Line Navigation Tips and Obscure Shortcuts

Command Line Navigation Tips and Obscure Shortcuts
Page content

Why Learn Commandline Shortcuts?

If you’re spending any amount of time in the terminal, learning shortcuts can potentially save you hours in the future. Most people know the arrow keys and backspace, but bash has decades of shortcuts built in that almost nobody uses.

I’ve been using Linux since 2011, and I’m still discovering new ones. Here are the ones I actually use daily, plus some obscure gems.

Jump to Beginning or End of Line

Ctrl+A - Jump to the beginning of the line

# Your cursor is here ↓
$ sudo systemctl restart nginx.service
# Press Ctrl+A
$ sudo systemctl restart nginx.service
↑ cursor jumps here

Ctrl+E - Jump to the end of the line

# Your cursor is here ↓
$ sudo systemctl restart nginx.service
# Press Ctrl+E
$ sudo systemctl restart nginx.service
                                      ↑ cursor jumps here

Real use case: You typed a long command, then realize you need to add sudo at the beginning. Hit Ctrl+A, type sudo , and you’re done. No holding down the left arrow for 5 seconds or more. Additionally, you can type in sudo !! which will run the last command you entered but will run it under sudo now! This one I show more later on, too.

Move Back or Forward One Word

Alt+B - Move backward one word

# Cursor at end
$ mv /var/www/blog/index.html /tmp/backup/
                                          ↑ cursor here
# Press Alt+B once
$ mv /var/www/blog/index.html /tmp/backup/
                                    ↑ cursor here (moved back one word)
# Press Alt+B again
$ mv /var/www/blog/index.html /tmp/backup/
                          ↑ cursor here

Alt+F - Move forward one word (the opposite of Alt+B)

Pro tip: The shell treats dots, slashes, and other symbols as word separators. So /var/www/blog is actually three words: var, www, and blog.

Jump to Specific Character

This one is obscure but powerful.

Ctrl+] followed by a character - Jump forward to the next occurrence of that character

$ rsync -avz /home/user/documents /backup/
                ↑ cursor here
# Press Ctrl+] then type /
$ rsync -avz /home/user/documents /backup/
                     ↑ jumps to next /

Ctrl+Alt+] followed by a character - Jump backward to that character

Useful when you need to quickly navigate to specific parts of a long path or command.

Editing Shortcuts

Delete a Word

Ctrl+W - Delete the word before the cursor

$ sudo apt install nginx apache2 mysql-server
                                              ↑ cursor here
# Press Ctrl+W
$ sudo apt install nginx apache2 
                                ↑ deleted "mysql-server"

Alt+D - Delete the word after the cursor (forward delete)

$ rm -rf /tmp/old_files /var/cache/
              ↑ cursor here
# Press Alt+D
$ rm -rf /tmp/ /var/cache/
              ↑ deleted "old_files"

Real use case: You pasted a command with an extra argument you don’t want. Just Alt+D to delete it without backspacing character by character.

Delete to Beginning or End of Line

Ctrl+U - Delete from cursor to beginning of line

$ sudo systemctl restart nginx.service
                            ↑ cursor here
# Press Ctrl+U
$ nginx.service
↑ everything before cursor deleted

Ctrl+K - Delete from cursor to end of line

$ sudo systemctl restart nginx.service
              ↑ cursor here
# Press Ctrl+K
$ sudo systemctl
                ↑ everything after cursor deleted

Pro tip: Ctrl+U is your panic button. Typed your password in plain text? Ctrl+U immediately clears the line.

Delete Single Character

Ctrl+H - Delete character before cursor (same as backspace, but doesn’t require moving your hand)

Ctrl+D - Delete character under cursor (forward delete)

Undo and Redo

Ctrl+_ (underscore) - Undo the last change

$ rm -rf /important/files
# Oh crap, didn't mean to type that
# Press Ctrl+_
$ 
↑ command undone

You can press Ctrl+_ multiple times to undo multiple changes.

Alt+R - Undo all changes to the current line (revert to original)

Transpose (Swap) Text

Ctrl+T - Swap the two characters before the cursor

$ teh quick brown fox
     ↑ cursor here
# Press Ctrl+T
$ the quick brown fox
     ↑ swapped 'e' and 'h'

Alt+T - Swap the two words before the cursor

$ cp destination source
                      ↑ cursor here
# Press Alt+T
$ cp source destination
                      ↑ words swapped

Real use case: You always type cp arguments in the wrong order? Fix it instantly with Alt+T.

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

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.

Ctrl+S - 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

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

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 
# 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

$ 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+A / Ctrl+E - Jump to start/end of line
  3. Ctrl+U - Clear line (panic button)
  4. Ctrl+W - Delete last word
  5. Alt+. - Insert last argument
  6. Ctrl+L - Clear screen
  7. !! - Repeat last command
  8. Ctrl+X, Ctrl+E - Edit in vim
  9. Ctrl+_ - Undo
  10. Alt+T - Swap words (for when I type cp arguments backward)

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 and Alt+F.
  • 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

Final Thoughts

Most sysadmins I know use maybe 50% or less of bash’s capabilities. They arrow-key through history, backspace mistakes character by character, and retype commands instead of editing them.

Learning these shortcuts won’t make you a better sysadmin, but it will make you a faster one. And when you’re SSH’d into a production server fixing an issue at 2 AM, every second counts.

So pick a few, practice them this week, and watch your terminal efficiency skyrocket.

Now get out there and Ctrl+R your way to glory.