Supercharge Your Mac and Ubuntu Terminals: Navigating Between Words

Add Some Custom Key Bindings to Your zsh Terminal on a Mac and bash on Ubuntu

By: Ajdin Imsirovic 03 June 2023

In this tutorial, I’ll guide you through navigating the console / shell on Mac and Ubuntu.

Table of Contents

Updating key bindings on a Mac

Hey there, fellow Mac enthusiasts and terminal adventurers! Are you tired of tediously navigating commands in your Mac terminal? Well, fret not! Today, we’ll equip you with some handy tricks to navigate your commands with lightning speed. Get ready to turbocharge your terminal experience!

Step 1: Open zsh

Let’s kick off our journey by unlocking some hidden powers through key bindings. We’ll make a few adjustments to our trusty .zshrc file, which holds the key to our terminal customization.

Step 2: List the key bindings

Command + Space, type term, hit Enter.

In the zsh terminal that opens, type bindkey and hit Enter. This will show the current key bindings in your terminal. You might find some key bindings already exist. For example, for me it was:

  • "^[b" backward-word
  • "^[f" forward-word

The former is actually the ESCAPE + b key combination, which moves the cursor backward one word at a time. The latter is the ESCAPE + f key combination, which moves the cursor forward one word at a time.

However, these key combinations are not very intuitive. Let’s change that!

Step 3: Add custom key bindings

First off, it’s important to not override the existing key bindings in your .zshrc file. For example, using OPTION + f would override the often-used “find” command in the terminal. So, let’s avoid that. Using the COMMAND + arrow right key combination is also not a good idea, as it’s often used to navigate between tabs in the terminal.

So, I had to think about what would we the best unused key combination to use.

Once I’ve decided, it’s just a matter of using the bindkey command to bind the key combination to the action.

The best key bindings I could come up with are the following:

  • bindkey '\e\e[C' forward-word
  • bindkey '\e\e[D' backward-word

So, forward-word is now bound to the OPTION + arrow right key combination, and backward-word is now bound to the OPTION + arrow left key combination.

For your changes to take effect, the easiest approach is to restart the terminal. You can also run source ~/.zshrc to reload the .zshrc file.

Pro Tip 1: Unleashing Terminal Superpowers!

While you’re on a roll, let’s unlock a few more terminal superpowers to elevate your command-line game. Add these lines to your .zshrc file:

  • Beginning of Line: the existing key binding is CONTROL + a (under the beginning-of-line entry in the .zshrc file)
  • End of Line: the existing key binding is CONTROL + e (under the end-of-line entry in the .zshrc file)
  • Clear Screen: the existing key binding is CONTROL + L (under the clear-screen entry in the .zshrc file)
  • Delete Word: bindkey '^w' backward-kill-word (under the beginning-of-line entry in the .zshrc file)

Feel free to update these key bindings to your liking.

With these additional bindings, you’ll soar through your commands, swiftly clear the screen, and breeze through editing like a pro.

Pro Tip 2: What’s the caret for?

The caret symbol (^) in the context of key bindings represents the Control key. In Unix-based systems, including macOS, key combinations involving the Control key are often represented using the caret symbol followed by a letter. For example, ^l represents the key combination Control + L.

Step 4: Save the file

Now, let’s embark on a customization adventure! Open your .zshrc file in your preferred text editor and prepare to tango with the code fairies. Can’t locate your .zshrc file? No worries! It’s often found at ~/.zshrc.

Bonus tip: Save commands in a text file

To save yourself some time, you can save your commands in a text file. This way, you can easily copy and paste them into your .zshrc file. This is especially useful if you’re working on multiple machines and want to keep your key bindings consistent.

To save the commands in txt file, run the following command in your terminal:

bindkey > ~/Desktop/keybindings.txt

Then, to find this file, type cd ~/Desktop in your terminal. This will take you to your Desktop folder. You can then open the keybindings.txt file in your preferred text editor.

Updating key bindings on Ubuntu

On Ubuntu, you can configure key bindings for the Bash shell in a similar manner. Here’s how you can achieve the same word navigation functionality using alternative key combinations in Ubuntu.

Step 1. Show the key bindings in the terminal

Open a terminal on your Ubuntu system.

Type bind -p to display the current key bindings in Bash.

Step 2. Find the entries for word navigation

Look for the entries that correspond to the “forward-word” and “backward-word” actions. They typically have key sequences starting with "\\e[1;5" for Ctrl + Alt key combinations. To find these quickly, you can use the following general command:

bind -p | grep -i word

Here’s that command implemented for the “forward-word” entry:

bind -p | grep -i forward-word

And here’s the output I get:

# copy-forward-word (not bound)
"\e\e[C": forward-word
"\e[1;3C": forward-word
"\e[1;5C": forward-word
"\e[5C": forward-word
"\ef": forward-word
# shell-forward-word (not bound)
# vi-forward-word (not bound)

What do these entries mean?

Well, we’ll get back to that a bit later. For now, here’s the most important thing relevant to this topic:

# For Control + right arrow
`"\e[1;5C": forward-word`

# For Control + left arrow
bind `"\e[1;5D": backward-word`

In other words, you’re likely to already have the Control + right arrow combo set for the forward-word action, and the Control + left arrow combo set for the backward-word action.

What if you’re trying to guess the possible command combination? The grep command comes in handy here as well. For example, if you’re guessing there might be an entry related to deleting things, but you don’t know the exact wording, you can use the following command:

bind -p | grep -i delete

And for me, the output was the following:

"\C-h": backward-delete-char
"\C-?": backward-delete-char
"\C-d": delete-char
"\e[3~": delete-char
# delete-char-or-list (not bound)
"\e\\": delete-horizontal-space
# forward-backward-delete-char (not bound)
# vi-delete (not bound)
# vi-delete-to (not bound)
# vi-overstrike-delete (not bound)

Actually, to delete words, which sounded like a reasonable command name, I had to grep for the “kill-word” entries, like this:

bind -p | grep -i kill-word

The results there were as follows:

"\e\C-h": backward-kill-word
"\e\C-?": backward-kill-word
"\e[3;5~": kill-word
"\ed": kill-word
# shell-backward-kill-word (not bound)
# shell-kill-word (not bound)

For me, what that boils down to is that you can delete an entire word in Ubuntu shell by using the Alt + Backspace key combination.

Now here’s the explanation for the previous list of grep results.

Explanation of the bind -p command output for grep -i forward-word

After running the bind -p command on Ubuntu, the output displays the key bindings for various actions in the shell. Here are the key combinations for the “forward-word” action as listed in your command output:

  • "\e\e[C": This represents the key combination of pressing the Escape key twice followed by the Right Arrow key. It is one of the key bindings for moving the cursor forward one word at a time.
  • "\e[1;3C": This represents the key combination of pressing the Escape key followed by the Control key and the Right Arrow key. It is another key binding for moving the cursor forward one word at a time.
  • "\e[1;5C": This represents the key combination of pressing the Escape key followed by the Alt (Option) key and the Right Arrow key. It is yet another key binding for moving the cursor forward one word at a time.
  • "\e[5C": This represents the key combination of pressing the Escape key followed by the 5 key and the Right Arrow key. It is an additional key binding for moving the cursor forward one word at a time.
  • "\ef": This represents a standalone key binding for moving the cursor forward one word. It doesn’t involve any modifier keys like Control or Alt.

These key combinations can be used to navigate forward one word at a time in the terminal on your Ubuntu system.

Please note that different systems and terminal configurations may have variations in the key bindings. Feel free to experiment and choose the key combination that works best for you.

Step 3. Optionally add some custom key bindings

If you don’t find specific bindings for word navigation, you can proceed with adding custom key bindings.

Add the following lines to your .bashrc file:

bind '"\e[1;9C": forward-word'
bind '"\e[1;9D": backward-word'

Danger! Make sure you’re not overriding some important key bindings on your system or for a specific app you often use!

These lines bind the Alt + F key combination to the “forward-word” action and the Alt + B key combination to the “backward-word” action.

If you’re sure about your changes, save the .bashrc file and exit the text editor.

Alternatively, source the updated configuration by running the following command in the terminal:

source ~/.bashrc

This will apply the changes you made to the key bindings without having to exit the currently open bash window.

Now you should be able to use the Alt + F and Alt + B key combinations in the Ubuntu terminal to move the cursor forward and backward one word at a time.

Note: The key codes used for Alt + F and Alt + B are \e[1;9C and \e[1;9D, respectively. These codes may vary depending on your terminal emulator. If the above steps don’t work as expected, you can try using the bind -p command to identify the correct key codes associated with word navigation and update the bind commands accordingly.

Conclusion

There you have it, Mac and Ubuntu aficionados! By customizing your .zshrc or .bashrc file and employing these key bindings, you’ve embarked on a journey towards terminal efficiency.

Feel free to check out my work here: