I want to set vi in editing mode in zsh (I am using oh-my-zsh) at start automatically when I open my shell, so at the beginning of my .zshrc I have tried the following code:
set -o vi
or
bindkey -v
but when pressing enter in the shell I cannot enter the vi mode.
If I tried one of the two commands in the shell, it works.
Basically I want zsh to start in vi edit mode.
Any ideas how to solve this problem?
bindkey -v is enough to enable vi mode in ZSH. If you are worried the setting will be overwritten by another plugin, put the setting at the bottom of your ~/.zshrc.
After vi mode is enabled, you enter the "insert" mode by default. To enter "normal" mode, use Esc. And i or a to switch back to "insert" mode.
BTW, softmoth/zsh-vim-mode is the most powerful vim mode plugin I've ever used in ZSH.
Using bindkey -v may take over functionality such as history search with control+R and control+S. To restore that particular behavior, add the following lines after bindkey -v:
bindkey ^R history-incremental-search-backward
bindkey ^S history-incremental-search-forward
Other bindings can be found in the ZSH manual Standard Widgets section.
If you are using https://ohmyz.sh/ you can add vi-mode to the list of plugins in ~/.zshrc:
plugins=(git vi-mode)
If you don't mind to use a plugin for vi mode in zsh, here is a better choice for you to quickly reach it.
zsh-vi-mode: A better and friendly vi(vim) mode plugin for ZSH.
After adding this plugin, then you can input with vi-mode like this:
Features
Cursor movement (Navigation).
Insert & Replace (Insert mode).
Text Objects.
Searching text.
Undo, Redo, Cut, Copy, Paste, and Delete.
Surrounds (Add, Replace, Delete, and Move Around).
Switch keywords (Increase/Decrease Number, Boolean, etc. In progress).
...
Related
I am using the vi-mode plugin of oh-my-zsh. In my .zshrc, I have
bindkey '^[[3~' delete-char
where ^[[3~ is the escape code of my delete key. However, this only works in insert mode, but not in command mode. When I type
$ abcd
move the cursor to the beginning of the line and hit del in command mode, I get
$ ABCd
so apparently the character sequence of the delete key is interpreted literally. How can I make the delete key actually delete a character in command mode?
bindkey -a '^[[3~' delete-char
Zsh has a variety of different keymaps and by default, bindkey will bind keys in the normal insert mode keymap. The command mode keymap is selected with -M vicmd. -a is a shortcut for that. You can list the keymaps with bindkey -l. You'll see that there is also viopp which is used for movements after a key like c or d. There's also visual for visual selection mode.
It's 2020 now, and I'm not sure if #okapi 's answer is out of date or just missing a piece, but for me, I had to use:
bindkey -a '^[[3~' vi-delete-char
delete-char without the vi- prefix didn't do the trick but adding it did.
I really like fzf when I use it, but it's difficult to actually use it. For example, I don't want to have to type vim $(fzf) every time I want to fuzzy find for a file. Ideally, I'd like to be able to just type ctrl-E to enter fzf and start editing the file after selecting it by pressing enter.
I don't know what keys are being pressed to accomplish what's done in the video on the github page (https://github.com/junegunn/fzf). Pressing tab just does normal auto-completion (and I don't want to do the ** style autocompletion. I want to enter fzf-tmux and have it paste the result into my command)
Can anyone help me?
The install script of fzf will setup such key bindings for you, CTRL-T, CTRL-R, and ALT-C. Refer to the the project home page for the details. The code for the key bindings can be found here.
If you do not like the default ones fzf provides, you can try writing your own.
# A simple widget for dictionary words
fzf-dict-widget() {
LBUFFER="$LBUFFER$(cat /usr/share/dict/words | fzf-tmux -m | paste -sd" " -) "
zle reset-prompt
}
bindkey '^E' fzf-dict-widget
zle -N fzf-dict-widget
after installing prezto when I press CTRL - RIGHTARROW I can see these characters
source python;5C;5C;5C;5C
Whereas emacs key bindings like ALT- f work fine.
I just want my default keybindings where I can navigate using CTRL keys.
My efforts:
Raised a issue on github + browsed other similar issues as well.
Couldnt figure out how their solution would help my case.
Tried setting zstyle ':prezto:module:editor' key-bindings '' but it did not
work.
I have also checked modeles/editor/init.zsh but the script is
too long n I dont wanna make random changes and later keep
maintaining those.
Can anyone suggest a way so that my keybindings remain "UNCHANGED" even after .zpreztorc is loaded ?
If you're using the prezto editor module, it will override your key bindings. If you have it set to emacs mode with
zstyle ':prezto:module:editor' key-bindings 'emacs'
you will need to add your key bindings to that named keymap. You can do that with
bindkey -M emacs '^[[1;5C' forward-word
bindkey -M emacs '^[[1;5D' backward-word
This will need to be run after the editor module is loaded. You can do that by adding it to the bottom of your .zshrc file. I use the vi keymap, so I need to add the key bindings to both the viins and vicmd keymaps.
for keymap in 'emacs' 'viins' 'vicmd'; do
# [Ctrl-RightArrow] - move forward one word
bindkey -M $keymap '^[[1;5C' forward-word
# [Ctrl-LeftArrow] - move backward one word
bindkey -M $keymap '^[[1;5D' backward-word
done
unset keymap
I'm trying to bind the command usualy binded to ^W with ctrl+backspace.
I have two problem here, one for each parameter of the bindkey command:
what is string to mean the ctrl+backspace
what is the command to delete the previous word
One may use bindkey '^H' backward-kill-word.
Note that, on old versions of GNOME terminal, it won't work; see How do I get Ctrl-Backspace to delete a word in vim within gnome-terminal? and Bug 420039 - VTE doesn't distinguish between backspace and control-backspace.
As reported by thorbjornwolf in his comment, commit 23c7cd0f fixed it.
As I pointed out here there is a chance that the keystrokes are different in some systems.
If the output of showkey -a is:
Ctrl+Backspace is ^?
then you should add the following line in your ~/.zshrc file:
bindkey '^?' backward-kill-word
I usually find interesting zsh keybinding settings (through bindkey command) around the web. My question is how do I interpret what these escaped sequences mapped to? For instance, here is a snippet from oh-my-zsh's key-bindings.zsh
bindkey "^[[H" beginning-of-line
bindkey "^[[1~" beginning-of-line
bindkey "^[[F" end-of-line
bindkey "^[[4~" end-of-line
Is there a reference on how do these keymaps represented? Also, is it zsh-specific or platform specific at all?
I am aware that I can use either cat or Ctrl-V to find the corresponding escaped sequence for certain keys. Given that I could brute force to find the reverse match, but this would not work for the keys that do not exist on my keyboard (e.g. Home/End on Mac laptops). Thus, I'd prefer methods that could determine the keys regardless of the physical keyboard.
If speaking of a typical unix/linux flow of events the picture is roughly the following.
The terminal emulator program recieves the X events such as so and so button pressed, another button is released. Those events can be tracked with xev utility, for example. The terminal emulator then translates those events into escape sequences.
This translation is not set in stone. It can be configured. Different terminal emulators are configured differently. For example xterm translation can be set up in .Xdefaults like that:
XTerm*VT100*Translations:#override \
Ctrl<Key>Left: string(0x1B) string(OD) \n\
Ctrl<Key>Right: string(0x1B) string(OC) \n\
Note 0x1B which is ESC. ESC is also printed as ^[.
Now, zsh uses zle (and bash uses readline library for the same purpose)
which interprets some of the sequences to move around the input line and perform editing actions.
The following texts should provide more additional details.
Zsh Line editor description
Wikipedia article on escape sequences
and
Xterm Control Sequences
My answer is for modern readers in 2021 using MacOSX with default zsh Termnial:
Run your Terminal, press ⌘ + , to open Preferences.
Select Profiles > Keyboard tab, then here you are, there are all your mappings.