Command Line Navigation Tips and Obscure Shortcuts

Page content

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 try to use on a daily basis, 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’ll show more later on.

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.

This one is kind of close to vim where b will move backup one word but different for moving forward since w is used to do that in vim.

Jump to Specific Character

This one is obscure and hard to remember but powerful I think.

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 until the next space

Ctrl+W - Delete the word and everything connected before the cursor until the next space

$ sudo apt install nginx apache2 mysql-server
                                             ↑ cursor here
$ mv /var/www/blog/index.html /tmp/backup
                                         ↑ cursor here
# Press Ctrl+W
$ sudo apt install nginx apache2 
                                 ↑ deleted "mysql-server"
$ mv /var/www/blog/index.html 
		                      ↑ deleted "/tmp/backup"

As you can see /tmp/backup was deleted as well and not just backup

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

$ rm -rf /tmp/old_files /var/cache/
              ↑ cursor here
$ mv /var/www/blog/index.html /tmp/backup
                              ↑ cursor here
# Press Alt+D
$ rm -rf /tmp/ /var/cache/
              ↑ deleted "old_files"
$ mv /var/www/blog/index.html /backup
		                      ↑ deleted "/tmp"

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 moving your hand or using your pinkie)

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). Can only be used once and restores to the history of that line.

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.

Final Thoughts

Some of these commands are hard to remember but after putting in some time and effort these things can surely make your commandline adventures even faster and more fun.