Setting up Mac navigation shortcuts in iTerm 2

If you’re a Macintosh user, it is probably second nature to use ⌥← to go back one word and ⌥→ to forward a word. Similarly, ⌘← will bring you to the beginning of a line while ⌘→ will jump you to the end. However, by default, iTerm 2, a great replacement for macOS’s included Terminal application, is not set up to work this way. Fortunately, it is possible to map your keys in a way to get this functionality.

In iTerm, go to your Preferences and then the Profiles tab. Click on the Profile you are using and choose the “Keys” tab.Keys preference

From there, you can click on the + button below the existing Key Mappings and use the following settings to emulate the default Macintosh behavior.

Back One Word

Back one word

Forward One Word

Forward one word

Go to Beginning of Line

Beginning of line

Go to End of Line

End of line

 

Searching your shell command history

Recently I was working with a colleague through Zoom and sharing my screen. As he watched me furiously tapping my up arrow key like I was playing a Mario Party mini game to cycle through my history of Unix commands, he gave me a great tip. To search through the history of previous commands you can pipe the history through grep:

history | grep "search term"

Essentially, this command says, “take all my history, send it to grep, and give me results for my search.” The results of the search will then be displayed to your terminal window. I found this tip to be incredibly useful and it has saved some wear and tear on my up arrow.