Sunday, July 19, 2015

Set -o vi FTW!!!

A programming buddy recently reposted some tips on using the OSX Terminal, from Tech Republic.
My terse response was

set -o vi

Which, of course, calls for some explanation. This configuration option to a Posix shell like Bash, sets it up to use the behavior of the ancient and ever popular "VI" editor as a command-line terminal interface.

Transliterating most of the Tech Republic article, here are the roughly equivalent shortcuts:

Puts the terminal command line into "Command Mode".  Single letter commands from VI will now work, including searches.

I or i or a or A
Puts the terminal command line into "Insert Mode".
Uppercase I puts the cursor at the beginning of the line, lowercase i leaves the cursor where it is.
Uppercase A puts the cursor at the end of the line, lowercase a puts the cursor after the next character.
Hit Escape to leave insert mode.

w and b
Command Mode: These shortcuts allow you to move the cursor between separate words in a command line.
Use 'b' to move back and use 'w' to move forward down the line.

Command Mode: This shortcut moves the cursor to the end of the current word on the command line.

d b
Using this key sequence will delete the word immediately before, or to the left of, the cursor.

d b b p
This will the swap two words that appear immediately before the cursor.
So, if "this is" sits before the word the cursor is on, using d b b p will change that to "is this."

/somesuch RETURN
If you need to locate a previously used command in Terminal,
use /somesearch and hit the RETURN key.
It will perform a search on the command history and allow you to find a previously used command
that you may need to access again.
Hit 'n' to find the next match.

?somesuch RETURN
Same as /, except searches history in reverse order.

Using '$' will take you to the end, or the far right, of the line where your cursor is.

This shortcut is the opposite of '$'. Typing '0' will take you back to the beginning, or the far left,
of the line you are currently working on.

Control + C
If you need to kill what's currently running, us Control and C in Terminal to abort the current application.

d 0
This shortcut clears the entirety of the line before the cursor.
So, if you get to the end of a line and realize the whole thing is wrong, use 0 D to delete it all.

0 D
These shortcuts will clear the entire line.

Using D will have a similar effect as using 0 D, but will only clear the line that appears after the cursor.
It is helpful if you need to change or delete the latter half of a line.

Comand + Q
This will clear the entire Terminal by killing the underlying process.
Let's face it, you weren't getting anything useful done anyway.

Control + L
This will clear the entire Terminal screen you're working on, deleting everything.
The same thing can be accomplished by typing "clear" into Terminal.

Control + D
This will cause you to exit the current shell in Terminal.
The same thing can be accomplished by typing "exit" into Terminal.

Control + Z
Suspends what you are currently running in the foreground and makes it a sleeping background process.
Type 'bg' and hit Return to awaken what you were running as a background process.
Type 'fg' to awaken what you were running in the foreground.
Type 'kill %1' to send the kill signal to the first suspended job.

This executes the last command entered. If you run into permission issues, try entering sudo before !!.

Control + H
Essentially the same thing as backspace, but it is useful if you want to stay anchored on the home row keys.

Typing "top" into Terminal will display all of your active processes.
Similar to what you'd get from Activity Monitor, but within Terminal. Press "Q" to quit.

history + a number
If you've lost track of a command you typed earlier, you can type "history" into Terminal to retrieve a history of your commands. However, if you want to view a specific number of past commands simply type a space then a number after history. So, "history 5" would show you the last five commands you typed.

bind -p | grep -v '^#\|self-insert\|^$'
This shortcut shows the key bindings that are active in the current editing mode (Command mode and Insert mode have different bindings).

ls -ltr
Show an extended list of files, ordered by time of creation, in reverse order (most recently created last).

find . -name "*.php" -exec grep -l "somesuch" {} \;
Do a deep search for the string "somesuch" in all files ending with ".php", starting from the current "." directory.

Post a Comment