Hunter GillaneHunter Gillane
Terminal: Beyond Ctrl + A and Ctrl + E
edit Posted by Hunter Gillane on Saturday November 12, 2011 at 02:53PM

Update: I had some of the Ctrl and Option commands switched. Fixed now.

As Rails developers, we spend most of our day in two places: an IDE/text editor and the command line. While we spend much time learning shortcuts that help us write and refactor our source code more quickly, many of us are perfectly okay just learning Ctrl + A and Ctrl + E and holding down the arrow keys to place the cursor where we want to get to on the command line.

Don't do that. With only a handful of additional commands, your day to day interaction with the terminal can be much more pleasant. Here are a few ways to do that using terminal and the bash shell.

A Command Line Editing Starter Pack

Here's a quick list of commands that you'll get a lot of day to day usage from, if you aren't already:

  • Ctrl + A: Move cursor to the beginning of the line
  • Ctrl + E: Move cursor to the end of the line
  • Ctrl + K: Delete from cursor to the end of the line
  • Option + F: Move cursor one word forward
  • Option + B: Move cursor one word backward
  • Option + D: Delete next word
  • Option + Delete: Delete previous word

Note: You'll need to set your meta key to Option before the commands that use Option will work (see below).

These seven simple commands will help you move more nimbly when editing text on the commands line. There is certainly plenty more you can do, which you can find in the bash manual.

A Word on Modes

By default, the bash shell run in emacs mode. This means that emacs shortcuts are enabled by default.

Emacs mode uses a Meta key for some commands, and the Option key is a good choice. In Terminal on OS X, you'll need to enable this by going to Preferences > Settings > Keyboard and checking "Use option as meta key."

You can also tell bash you want to use vi mode for command line editing with set -o vi, if for some reason you want to type i before every cd or ls you write.

End

Nothing new or overwhelmingly exciting here, and there is certainly much more to dive into if you are so inclined. However, this small set of commands will have you covered for the majority of your day to day command line editing needs and is what I would consider constitutes a minimum level of proficiency.

Give your arrow keys a break!

Comments

  1. grosser grosser on November 12, 2011 at 06:29PM

    You can make home/end keys work on osx terminal, and get rid of this ctrl+e/a annoyance :)

    http://tech.inhelsinki.nl/gnu_developement_under_mac_os_x/

  2. Mark Wilden Mark Wilden on November 12, 2011 at 07:36PM

    Ctrl+F/B move by character on my system.

    The best part of learning these keystrokes is that they're not Terminal-specific. You can use them practically anywhere in OS X.

  3. Hunter Gillane Hunter Gillane on November 12, 2011 at 07:57PM

    @mark I accidentally had Ctrl instead Option for those. Fixed now. Thanks!

  4. Jason Noble Jason Noble on November 12, 2011 at 08:44PM

    When using set -o vi, the command prompt by default is in text entry mode, to enter command mode, hit Esc like you would in vi. For example, you can just type cd or ls without first typing i.

  5. Mark Wilden Mark Wilden on November 13, 2011 at 04:38PM

    One more useful key: Ctrl + D deletes the character after the cursor. Again, I probably use these keystrokes in Gmail more than any place.

  6. Jason B. Jason B. on November 18, 2011 at 03:44PM

    Is this what they are teaching you out in Boulder? Thanks for the tips mang!