In this question, the inimitable Dennis Williamson mentions how to bind a keystroke to a command that will run in the background of the Bash shell, with bind -x. How can the same thing be done in Zsh?
(I couldn't find this in the zshzle man page, but it's possible that I overlooked it.)
You can use zle -M to display info underneath your command line. For example:
.who() {
zle -M "$(who)"
}
# Create a new widget `who` that calls our function `.who`.
zle -N who .who
# Bind said widget to alt-shift-W.
bindkey '^[W' who
Related
I'm new to zsh and am trying to bind a key sequence to a function with the following in my .zshrc:
say_hello(){
echo "hello"
}
zle -N say_hello
bindkey '^Y' say_hello
Pressing Ctrl-Y will call the function and I'll see "hello" printed to the terminal but after I need to press Enter again before I'm given another zsh prompt. Calling the function by just typing in say_hello at the zsh prompt and pressing Enter does what I want - I see hello printed and then I'm given another zsh prompt. How can I get this behavior when binding the function to a key sequence?
Above is a simple example, really the function I'm trying to write is below:
my_cd() {
if [[ "$#" -ne 0 ]]; then
cd $(autojump $#)
return
fi
dir_to_cd_to=$(fasd_cd -dl | fzf --height 40% --reverse --inline-info)
# above isn't so important - dir_to_cd_to could be obtained in any way
cd "$dir_to_cd_to"
}
zle -N my_cd
bindkey -v '^Y' 'my_cd'
To display messages in a zle widget, you're supposed to use zle -M rather than echo. echo will output your message at whatever the current cursor position is which isn't especially helpful. If you really want to use echo, calling zle reset-prompt afterwards will redraw a fresh prompt. If you don't want a potential mess in your terminal, consider starting with \r to move the cursor to the beginning of the line and ending with $termcap[ce] to clear to the end of the line.
I'm trying to auto-expand aliases as I type in zsh using the globalias plugin from oh-my-zsh. It was working when I first installed the plugin, but now it has broken. I think it broke after I rearranged the lines in my ~/.zshrc but I'm not sure.
I've tried to simplify the problem by removing everything except the following lines form my ~/.zshrc (I got this from a reddit thread but it also looks v similar to the function in the globalias plugin):
function expand-alias() {
zle _expand_alias
zle self-insert
}
zle -N expand-alias
bindkey -M main ' ' expand-alias
When I source this file and type a space in my prompt, I see
No such widget `_expand_alias'
I've tried googling and it seems like _expand_alias should be a built-in ZLE function, but when I look for it with zle -la | grep _expand_alias it's not there.
I'm not sure what to try next? I can't find a way to reset zsh or the built-in ZLE commands.
I solved it by adding the line autoload -Uz compinit && compinit near the start of my ~/.zshrc.
Hope this helps someone!
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
Where can I find a list of zsh commands that I can use with bindkey, with descriptions?
Each time that I look for name of some standard action (e.g., end-of-line), I need to google and guess that the command found is what I look for.
Related:
Interpret zsh bindkey escaped sequences (SO)
The Z-Shell Line Editor (doc)
bindkey -l will give you a list of existing keymap names.
bindkey -M <keymap> will list all the bindings in a given keymap.
If you use the zsh command line in emacs mode, then the emacs keymap is likely to be most important for you.
If you use it in vi mode, then you’d be interested in viins and vicmd.
(See the zshzle(1) man page for more details.)
Once you have a list of keybindings, you can search the official ZLE documentation for the name of the action (or “widget” in zsh parlance).
zle -al
lists all registered zle commands
Commands available for use in the line editor are referred to as widgets. The standard widgets are listed in the zshzle manpage in the STANDARD WIDGETS section. That manpage is also available from the zsh website
Zsh Line Editor Doc: https://web.cs.elte.hu/local/texinfo/zsh/zsh_10.html
Look up system current bindkey setting: $ bindkey, eg($ bindkey|grep case, looking for down-case);
$ zle -al used for list all registered zle commands;
Bind your personal key for zsh command , $ vim ~/.zshrc, add
# bindkey
bindkey "^U" backward-kill-line
bindkey "^u" backward-kill-line
bindkey "^[l" down-case-word
bindkey "^[L" down-case-word
# alt+<- | alt+->
bindkey "^[f" forward-word
bindkey "^[b" backward-word
# ctrl+<- | ctrl+->
bindkey "^[[1;5D" backward-word
bindkey "^[[1;5C" forward-word
See other things: oh-my-zsh down-case-word bug: https://github.com/robbyrussell/oh-my-zsh/commit/55a9d685fd960390a4f400ac461d901049a78beb
I'm on zsh via putty. For me the bindings were different. You can find this out with CTRL+V followed by for example the left arrow. It will display the used character sequence. So for me it was:
bindkey "^[[D" backward-word
bindkey "^[[C" forward-word
bindkey "^H" backward-kill-word
After installing oh-my-zsh, I typed bindkey 'anything' then press tab, then say yes. The list of available bindkeys will be flushed out
Is there a way to do the type of auto-suggestion Fish does in Zsh?
https://github.com/tarruda/zsh-autosuggestions does exactly what I wanted. If you want fish-style autopredictions in zsh, use that.
Zsh has predict, run the commands below this and then hit Ctrl-X 1 or just type predict-on to give it a try
#-*-shell-script-*-
autoload predict-on
autoload predict-off
# you may also wish to bind it to some keys...
zle -N predict-on
zle -N predict-off
bindkey '^X1' predict-on
bindkey '^X2' predict-off