Disable Zsh history completely - zsh

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.

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.

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.

In prezto how to get CTRL-RARROW working?

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

Deleting word like bash in zsh (autoload is not working)

I have the following code in my .zshrc and I have .oh-my-zshell installed.
The following line, however, does not work as it is described in other posts:
autoload -U select-word-style
select-word-style bash
Is there other options I need to add in order to make it work?
if you want '^W' bash behavior you may need to unset WORDCHARS or place them in seperate functions and bind you newly created functions.
As i am not using you example, here is my way to accomplish this, second example is also for bash compatibility using <A-Backspace> which respect WORDCHAR defined as follow
x-bash-backward-kill-word(){
WORDCHARS='' zle backward-kill-word
}
zle -N x-bash-backward-kill-word
bindkey '^W' x-bash-backward-kill-word
x-backward-kill-word(){
WORDCHARS='*?_-[]~\!#$%^(){}<>|`##$%^*()+:?' zle backward-kill-word
}
zle -N x-backward-kill-word
bindkey '\e^?' x-backward-kill-word

bind key to complete filename wherever the context is in Zsh

Sometimes I want a filename instead of what zsh guesses for me. For example, I have a PNG file without a proper .png suffix, which makes zsh think it isn't a picture and won't list it when I type Tab to complete arguments of display.
I am wondering if there is a key sequence that completes for a filename wherever the context is, like ^XC for _correct_filename, or how to configure zsh to do the thing?
You can add a key binding to do what you want:
zle -C complete complete-word complete-files
bindkey '^X\t' complete
complete-files () { compadd - $PREFIX* }
Edit: Added $PREFIX
You can add those lines to your ~/.zshrc file.
That makes the completion list all files when you press Ctrl-x Tab at each step instead of Tab. You could choose another keystroke combination that suits you.
Or to make ImageMagick completions always include all files, try editing (make a backup first) the file /usr/share/zsh/functions/Completion/Unix/_imagemagick (or similar) and change this to comment out the existing line that begins with _files and add the new one shown:
if (( $# )); then
# _files "$#" -g "*.(#i)(${~formats//:/|})(-.)"
_files "$#"
return
fi
Dennis' answer didn't expand tilde for me, so I would get stuff like complete-files: no matches found: ~/ma* when I tried to invoke it on foo ~/ma. I did find an alternate in the zsh FAQ that will expand them, though:
zle -C complete-file complete-word _generic
zstyle ':completion:complete-file::::' completer _files
bindkey '^xF' complete-file

Resources