Some quick terminal keystroke shortcuts

When I made the switch to Mac from Windows one of the things that I enjoyed the most was having access to a full featured console window. The Terminal in OSX is much more powerful than the Command shell in Windows out of the box. Not only do I have quick access to all of the great Unix commands for things like file viewing and management, I can quickly SSH into my remote Linux based servers very easily.

Most people are aware of the standard things you can do in a Terminal shell, much like you can do in a Windows Command prompt: hit the up arrow to cycle through previous commands and hit the Tab key to help complete the name of a directory/folder. There are a couple of other things that I've found that really help out:

Search Command History (Control-R)
Rather than hitting the Up Arrow 10 times to get to an older and lengthy command you can simply hit Control-R. This will give you a prompt that allows you to start typing in a command and the first match it finds from your command history will be displayed as you type. Since I found this I use it constantly and rarely hit the Up Arrow to repeat a previous command.

Cursor Navigation (Escape-F, Escape-B)
I'm a heavy keyboard user yet when I jump into the Terminal I feel like my fingers are tied sometimes. This is especially true when trying to navigate between words in a long command line; I hit Command-Right or Command-Left and switch tabs in my command windows. Instead of using Control modified keys the Escape key comes into play here. Hitting Escape, then hitting F (forward) or B (back) jumps the cursor from word to word in the command line. It's less efficient than hitting a Control or Command modified key but it beats standing on the Left and Right arrows.

Delete to End of Line (Control-K)
Very often I find myself executing the same command on a different target. This usually involves deleting the last portion of a previously run command. Before picking up some of these shortcuts I would simply up arrow to the previous command and then stand on the Delete key until I got to the point I wanted. Now, I use the Escape-B command to back up to the right point in the previous command and then hit Control-K to delete from that point to the end of the line.

So there are the three shortcut combinations I've found invaluable in the Terminal window, and the best part is they are pretty universal to the Bash shell so when I'm in my SSH'd into one of my Linux servers I can use them there as well.

Got a shortcut you use in Terminal that you use all the time? Drop a note in the Comments below.


slubman said...

Are you often browsing through the history, and just want to change the command name (you know the first word on the line), then just press Control-A, and your cursor is back to the beginning of the line

The reverse (go to the end of the line): Control-E

mudman said...

Go to Terminal/Window Settings.../Keyboard and click on "Use option key as meta key"

This will transfer the function of the "Esc" key to the "Option" key and save some finger stretching :-]

Now, if you hit Option-D, it will delete the word that your cursor sits on....

Keleko said...

Them thar's emacs commands! Go read up on your emacs key commands to find out more. I don't remember all the ones available in bash. If you know vi, you can type in "set -o vi" to use vi commands instead.

Some other emacs commands: ctrl-f and ctrl-b do the same as the left and right arrow keys. I tend to use that instead of the arrow keys because I don't need to remove my hands from the home key area.

ctrl-w deletes the previous word.

Mike D. said...

Nice post.

I just started a my O/S course in Computer Science and we're about to start Unix, hopefully with the help of this post, and the comments that follow, I'll be ahead of the curve.

Qka said...

Not a keyboard shortcut, but you can drag and drop file and directory names from Finder to Terminal. I use that a lot when processing directories, and I don't want to enter the entire path, particularly for the cd command

SadFootSign said...

@keleko "set -o vi" oh man, you just made my day. How freaking cool, thanks.

Ast A. Moore said...

Control-R is excellent! Didn't know about it.

David Alison said...

Some great additional tips there - thanks folks! Keleko - that vi command tip is excellent. I use vi all the time.

Lonnie said...

Is there a good place to go for a tutorial/walk-through of the things you can do in Terminal? The basic commands don't scare me too much, but when I "get inside" programs I get a bit confused and end up huddling in the corner whimpering to myself. For example, the mail service really confused me for a while. Once you're in, how do you get out? BTW, I'm still enjoying this blog and have it in my widgets so I can stay on top of the newest posts. I also made the switch (at work) eight months ago and I must say I actually look forward to walking in the door of the office in the morning because of it!

David Alison said...

@Lonnie: I get a bit confused and end up huddling in the corner whimpering to myself

Unix shells have a tendency to do that to people ;-). There are two lines you can go down for this: the first is any good tutorial (of which there are literally hundreds on the web) for BASH, the default shell for the Terminal. The second is to pick up a Mac specific book on the topic. Though it is targeted at Tiger most everything in A Practical Guide to Unix for Mac OS X Users contains a very thorough treatment on the topic.

Hope this helps!

David Orriss Jr said...

These are all awesome tips... Thanks for sharing..!!

nuthack said...

I recommend visor to you
It's good stuff. ^^

David Alison said...

@nuthack: Thanks - great tip! First I'd seen of that.