In prezto how to get CTRL-RARROW working? - zsh

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

Related

What are the default mappings for the up and down arrows keys in zsh

I am trying to bind the default functionality of the up and down arrow keys in zsh to something else. I.e. Now when I press up, it basically shows me the previous command and when I press up again it shows me the command before that. I would like to bind this functionality to another key (say ^k and ^j). So the bind would look like:
bindkey '^k' up-line-or-history
bindkey '^j' down-line-or-history
However, I was searching around and found many possibilities: up-line, up-line-or-history etc. Which mappings are the default used by zsh for the up and down arrow keys ? I'm guessing its up-line-or-history and down-line-or-history but I can't be too sure.
Version:
zsh 5.7.1 (x86_64-apple-darwin19.0)
up-line is unbound by default; from man zshzle
up-line (unbound) (unbound) (unbound)
which you can confirm by grepping for up-line in the output of bindkeys:
% bindkey | grep up-line
"^P" up-line-or-history
"^[OA" up-line-or-history
"^[[A" up-line-or-history
The documentation mentions that up-line-history is bound to ^P and up-arrow:
up-line-or-history (^P ESC-[A) (k) (ESC-[A)
Both ^[OA and ^[[A refer to the up-arrow key; I believe the duplication depends on your terminal emulator's settings, with the sequence generated by up-arrow depending on the "mode" of the terminal.

How to setup Vi Editing Mode for zsh

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).
...

Bind delete key in vi mode

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.

Disable Zsh history completely

I would like to disable Zsh history (arrow up) and zle history search (namely esc+p) completely. How can I achieve this?
My current .zshrc:
unsetopt hist_append
unsetopt hist_expand
HISTFILE=
HISTSIZE=SAVEHIST=0
Currently I have history buffer of one, but I'd like to have history of zero.
21-10-2016 Update:
I've added
bindkey -r "^[p"
bindkey -r "^Xr"
bindkey -r "^Xs"
bindkey -r "^[[A"
bindkey -r "^[[B"
bindkey -r "^[n"
to get rid of history features that I use (esc+p is deeply hardwired to my backbone - so difficult to unlearn).
I don't see anything in the zsh man page that completely disables history. Even setting HISTSIZE=0 seems to reset the value of HISTSIZE to 1.
You'll probably have better luck changing the key bindings with bindkey so that history features never occur. For example, bindkey -r "^[[A" for my up-arrow key (note that I actually typed a caret and two brackets, not an escape key).
I use following entries in my .zshrc:
alias disablehistory="function zshaddhistory() { return 1 }"
If function zshaddhistory is defined, it can control whether history line will be saved.
Therefore, alias disablehistory will just define function that always returns 1 (don't save).
alias disablehistory="function zshaddhistory() { return 1 }"
alias enablehistory="unset -f zshaddhistory"
enablehistory just unsets function set by previous alias.
If you want to disable history completely, permanently define zshaddhistory in your .zshrc
Try configuring your ZSH setup to run a postexec function that empties/deletes your .zhistory file. It's a hacky workaround but should probably work.

Using fzf with tmux and zsh on Mac

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

Resources