How to customize vim key bindings outside vim (eg: terminal)?

笑着哭i 提交于 2020-03-06 09:31:24

问题


In my vim editor, I have remapped some of the actions, as I don't like their default key bindings. (For example, I use Ctrl h/l to go to the beginning/end of the line instead of 0/$, as the former bindings are much easier to use.)

I am also using vim binding in my terminal (bash and tmux) and some commands like "less" use vim bindings as well. My customization doesn't work there, so I need to remember my custom as well as the default key bindings, which is pretty inconvenient.

How can I customize the key bindings outside vim (eg: in bash, tmux and less) ?


回答1:


Although many utilities claim to implement Vim and/or Emacs key bindings, they do not actually involve Vim or Emacs in the implementation. I don't know about Emacs, but Vim doesn't offer any kind of general-purpose key management library, so there would be no way to use Vim from a different program even if one wanted to.

Tmux and Less both have their own completely idiosyncratic key management systems. They are both well-documented. Once you figure out the bindings you want, you can save them in configuration files. (.tmux.conf and .less, respectively. .less is actually a binary file created by lesskey; .tmux.conf is a text file.)

Bash uses the Readline library, which is a general-purpose library and is also used by many console applications. You can customize all (or at least most) readline-based utilities using a single configuration file. (Normally .inputrc.)

The configuration files mentioned above are in your home directory. Most utilities will also consult (or will fall back to) a system-wide configuration file with a similar name in the /etc directory. Often utilities have command-line arguments which let you use a different configuration file; that's useful for debugging.

For really detailed information, you'll probably find appropriate manpages already on your system. So you could try, for example

man bash
man less
man lesskey
man readline
man tmux

There should also be info pages for Bash and Readline, which can be easier to navigate. Not all distributions install documentation files by default; you might need to install an associated doc package. For example, on Ubuntu and Debian, you'll need the bash-doc and readline-doc packages.




回答2:


For applications using readline, you can customize their key bindings in ~/.inputrc.

set keymap vi-insert
set keymap vi-command
"\C-h": beginning-of-line
"\C-l": end-of-line
# Line navigation in insert mode.
bind -m vi-insert "\C-h":beginning-of-line
bind -m vi-insert "\C-l":end-of-line
# Line navigation in command mode (after hitting ESC).
bind -m vi-command "\C-h":beginning-of-line
bind -m vi-command "\C-l":end-of-line

Reload your inputrc file by running bind -f ~/.inputrc

Now, you can use Ctrl+h and Ctrl+l to move to the beginning or end of the line respectively.

This link contains all the readline commands you can use in your mapping: https://www.gnu.org/software/bash/manual/html_node/Bindable-Readline-Commands.html



来源:https://stackoverflow.com/questions/60143772/how-to-customize-vim-key-bindings-outside-vim-eg-terminal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!