Skip to content

Latest commit

 

History

History
58 lines (39 loc) · 3.49 KB

File metadata and controls

58 lines (39 loc) · 3.49 KB

Keyboard Shortcuts

Lets learn some fancy Bash keyboard shortcuts!

Shells have been around for a long time, and they used to be our primary method of interacting with computers, so it's no wonder that their programmers have implemented a few time-saving features.

Notation: Ctrl-a means holding down the control key and pressing a at the same time.

Moving the cursor

We can use the left and right arrow keys to move our cursor. If you're typing a lot, moving your pinky can get tiring, so we can also use Ctrl-f and Ctrl-b to move forward and back by one character.

Either way, one character at a time is still pretty slow. Alt-f and Alt-b will move the cursor forward and backward one word at a time instead.

Lastly, we can go to the start of the line with Ctrl-a (a is the first letter!) and to the end with Ctrl-e.

Typing commands

Some commands have pretty long names—to save time, we can press tab to use Bash's autocomplete. This works for paths, programs, and some arguments and flags.

If we press tab and there are multiple options for the autocomplete, Bash will not choose for us and nothing will happen. If we press tab twice in rapid succession, Bash will list out all possible completions. The same thing happens if we press Alt-?.

History

When we run a command, it is stored in a file at ~/.bash_history. We can use the up and down arrows to view, edit, and rerun previous commands without typing them out again in full.

Equivalently, we can press Ctrl-n and Ctrl-p for the next and previous lines.

Halting programs

When we need to halt a program for whatever reason, the first option is usually Ctrl-c.

Modifying text

You might have noticed that Ctrl-c, Ctrl-x, and Ctrl-v don't work to copy, cut and paste in Bash—but there are still ways to do so within Bash itself.

  • Ctrl-d deletes the next character
  • Ctrl-k cuts from the cursor until the end of the line
  • Ctrl-u cuts from the cursor to the beginning of the line
  • Alt-d cuts from the cursor until the end of the current word
  • Alt-backspace cuts from the cursor until the beginning of the current word

We can paste cut text with Ctrl-y

Bash can swap characters and letters

  • Ctrl-t swaps (transposes) the current character with the previous one
  • Alt-tswaps the current word with the previous one

Bash can also convert between letter cases:

  • Alt-l converts all the characters from the cursor until the end of the line to lowercase
  • Alt-u works as above, but converts to uppercase.

Advanced tricks

We can use Alt-# to save our current buffer without running it. This uses Bash's comment syntax: any line that begins in # will not be interpreted by Bash.

We can search our history with Ctrl-r. This can be a huge time saver!

We can append the last word of the previous command to the current buffer with Alt-., or the last word of earlier commands by repeating the hotkey. This is surprisingly useful: for instance, we often run mkdir <myfolderwithalongname> and then cd <myfolderwithalongname>.

Exercise

Type out ehco hello world!. Without using the arrow keys and without pressing backspace or delete, correct the command to say echo.

Run echo "Find me!", then press enter several times. Execute the same command again without typing it in full and without using your arrow keys.

Type the quick brown fox jumps over the lazy dog. Now reverse the sentence word-by-word without deleting or cutting any characters.

Type e and then press tab twice. What do you see?