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!
About the Author