Skip to main content
  1. Posts/

Binding Keys in Zsh

··307 words·2 mins·
Table of Contents

In this post, I want to share how to use bindkeys command to solve a few issues when using Zsh.

Can not use HOME and END keys normally

After using Zsh, I found that some terminals can not understand Home and End keys correctly, i.e., I can not go to the beginning or end of a line by pressing Home or End key.

This is because the key codes that terminal recognizes when you press certain keys can not be recognized by Zsh. You can manually find which key code is sent when you press a key. You can use showkey -a to print the key codes. Here is what it shows when I press Home and End keys.

After we know the key codes for these two keys, we can bind the codes to the operation we want to perform, that is begin-of-line and end-of-line. Add the following settings to your .zshrc:

bindkey '^[[H' beginning-of-line
bindkey '^[[F' end-of-line

Bind key to run a custom command

One reader of this post asked a question on how to use shortcut keys to run the following command:

nvim $(fzf)

I searched the web and found a solution. Suppose that we want to bind Ctrl + O to the above command, we can add the following setting to .zshrc:

bindkey -s '^o' 'nvim $(fzf)^M'
# you may also use the following one
# bindkey -s '^o' 'nvim $(fzf)\n'

In the above setting, -s option is used to translate the input string to output string so that when you press the shortcut, it is replaced with the command you want to run. ^M or \n is used to represent the Enter key so that the command is run automatically.



My Experience with Several Zsh Plugin Managers
··481 words·3 mins
Boosting Your Productivity on Terminal with Zsh and Plugins
··716 words·4 mins
Building Zsh from Source and Configuring It on CentOS
··717 words·4 mins