Code Self Study Forum

Becoming faster with the command line

A few people have asked about the command line recently. Here are some common keyboard shortcuts that make using the command line much faster:

  • ctrl + a – move to beginning of line
  • ctrl + e – move to end of line
  • ctrl + k – delete to end of line
  • ctrl + u – delete to beginning of line
  • alt + b – move back one word
  • alt + f – move forward one word
  • alt + d – delete one word forward
  • ctrl + p and ctrl + n – move up and down through command history (also and )
  • ctrl + r – search through previously-typed commands
  • ctrl + x + u – undo
  • ctrl + x + e – open the current line in a text editor (uses $EDITOR – probably vim by default)

Those work in Linux and probably on Windows if you’re using bash. I’m not sure how alt is handled on Mac.

If anyone has other tips for using the command line more quickly, post them below.

2 Likes

The fact is, most of these (except for the last four) are just emacs commands. When you say “command line”, I assume you mean the bash interactive shell. In fact, these are applicable more widely than the bash context. They are common in most keyboard text interaction contexts. Next time you are interacting with a text input box, whether it be a text input on a Web form or a text input box for a local application or utility on your computer, try some of the emacs control sequences, especially the first three above, but also

  • <ctrl>-y (“yank” buffer) after a <ctrl>-k
  • <ctrl>-b (back one character)
  • <ctrl>-f (forward one character)
  • <ctrl>-t (transpose characters)

By the way, <alt> is just <cmd> on the Mac in this context, and in many others.

1 Like

I guess bash is default on most computers. I’m using zsh at the moment.

Terminal User Guide: Keyboard shortcuts for Terminal on Mac

Terminal Shortcuts

1 Like

Thanks – that question just came up here at the cafe a bit earlier. :slight_smile:

I will use the command line history as a way of saving time. The command by itself is history which shows a report with PIDs. You can then re-run the one you want with the !1234 syntax.

But since I’m always doing the following construct history | grep foo I created a profile alias called fistory (“find” + “history”) which is the first three parts of that. So fistory service would then display any commands which include that term.

1 Like

Here’s a cheatsheet that I found this morning. Click “Emacs Mode” for the default keybindings. “Vim mode” starts at the top of the page.

https://gnureadline-reference.herokuapp.com/reference/