Skip to content

Instantly share code, notes, and snippets.

@iklobato
Last active January 16, 2024 15:33
Show Gist options
  • Save iklobato/9ac21d0c730bcd11997c320641e9e922 to your computer and use it in GitHub Desktop.
Save iklobato/9ac21d0c730bcd11997c320641e9e922 to your computer and use it in GitHub Desktop.
Enable Word Navigation in iTerm 2 Using Option (Alt) and Arrow Keys

Optimize iTerm 2 Word Navigation Configuration

Enhance your iTerm 2 command-line experience with improved word navigation. This guide covers two approaches: a convenient one-liner script and a manual method.

One-Liner Approach

Learn how to quickly configure word navigation in iTerm 2 using a one-liner script. This script enhances key bindings with the Option (Alt) key and arrow keys, facilitating smooth movement one word backward and forward.

Implementation

  1. Copy and paste the following one-liner into your terminal:

    echo "bindkey -e && bindkey '\e\e[C' forward-word && bindkey '\e\e[D' backward-word" >> "$(if [ -n "$BASH_VERSION" ]; then echo ~/.bashrc; elif [ -n "$ZSH_VERSION" ]; then echo ~/.zshrc; else echo "Unsupported shell. Exiting."; exit 1; fi)" && source "$(if [ -n "$BASH_VERSION" ]; then echo ~/.bashrc; elif [ -n "$ZSH_VERSION" ]; then echo ~/.zshrc; else echo "Unsupported shell. Exiting."; exit 1; fi)"
  2. Execute the command.

  3. Word navigation is now seamlessly enabled using the Option (Alt) key and arrow keys in iTerm 2.

Key Points

  • This script dynamically appends configurations to your Bash or Zsh profile, catering to your specific shell environment.

  • Ensure your terminal supports the Option (Alt) key and arrow key combinations.

  • For troubleshooting, confirm you are using a supported shell (Bash or Zsh).


Manual Approach

For those who prefer a hands-on approach to configure word navigation in iTerm 2, follow these manual steps:

Procedure

  1. Open your Bash or Zsh profile file for editing. Common paths are ~/.bashrc for Bash and ~/.zshrc for Zsh.

  2. Append the following line at the end of the file:

    bindkey -e && bindkey '\e\e[C' forward-word && bindkey '\e\e[D' backward-word
  3. Save and close the file.

  4. Open a new terminal window or restart your terminal.

  5. Experience improved word navigation using the Option (Alt) key and arrow keys.

Additional Information

  • Confirm your terminal supports Option (Alt) key and arrow key combinations.

  • This manual approach requires direct editing of the profile file.

  • In case of issues, double-check the correctness of the added line in your profile file.


Keywords: iTerm 2, word navigation, key bindings, Option (Alt) key, arrow keys, Bash, Zsh, command-line editing, terminal customization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment