most recent 30 from unix.stackexchange.com2026-03-01T05:15:55Zhttps://unix.stackexchange.com/feeds/tag?tagnames=zlehttps://creativecommons.org/licenses/by-sa/4.0/rdfhttps://unix.stackexchange.com/q/7866333user172877https://unix.stackexchange.com/users/02024-11-14T14:07:12Z2025-12-11T14:02:19Z
<p>I would like to run some code when zle becomes active.</p>
<p>The only way I know how to do that is</p>
<pre class="lang-bash prettyprint-override"><code>zle-line-init() {
echo hi
}
zle -N zle-line-init
</code></pre>
<p>But AFAIK this will replace any potential existing <code>zle-line-init</code> hooks.</p>
<p>How can I either add to existing hooks (like how precmd has a <code>precmd_functions</code> array), or restore the previous state of <code>zle-line-init</code> after my function runs?</p>
https://unix.stackexchange.com/q/8011399mgiuffridahttps://unix.stackexchange.com/users/1232472025-11-07T17:13:56Z2025-11-08T09:12:48Z
<p>Why does zsh delete a trailing comma when I type a closing brace?</p>
<p>For example, trying to compare two files <code>foo.txt.bak</code> and <code>foo.txt</code>:</p>
<pre class="lang-bash prettyprint-override"><code>diff foo.txt{.bak,}
</code></pre>
<p>When I type that closing brace, the comma is deleted, resulting in the command only receiving one argument, <code>foo.txt.bak</code>.</p>
<p>This only happens if the suffix inside the closing brace was tab-completed. If I type the above literally, the comma isn't stripped.</p>
<p>I've played around with zstyle settings and zsh opts but haven't found a fix. (This also repros with no config file, <code>zsh -f</code>).</p>
https://unix.stackexchange.com/q/7769620Nathanhttps://unix.stackexchange.com/users/1188042024-05-22T15:38:54Z2025-10-06T09:31:03Z
<p>I am using tmux with the zsh shell.</p>
<p>I try to setup fzf, and I do it like this (inside my zshrc):</p>
<pre><code>eval "$(fzf --zsh)"
</code></pre>
<p>This works, as long as I am not in tmux. I can use Ctrl-R and get the fzf history search. But inside tmux this does nothing.</p>
<p>When I try to invoke the history back search manually, I get:</p>
<pre><code>❯ fzf-history-widget
fzf-history-widget:zle:14: widgets can only be called when ZLE is active
</code></pre>
<p>A bit of googling suggests, that I need to load zle first, but also:</p>
<pre><code>❯ zmodload zsh/zle
</code></pre>
<p>Does not work.</p>
<p>What is wrong? What can I do?</p>
<p>Edit:</p>
<p>As requested, here is the the output of <code>bindkey | grep fzf</code>:</p>
<pre><code>❯ bindkey | grep fzf
"^I" fzf-completion
"^R" fzf-history-widget
"^T" fzf-file-widget
"^X." fzf-tab-debug
"^[c" fzf-cd-widget
</code></pre>
https://unix.stackexchange.com/q/7978965edison23https://unix.stackexchange.com/users/1382862025-07-13T13:10:01Z2025-07-31T20:14:33Z
<p>I recently upgraded KDE Plasma to v6, got ZSH instead of BASH in it and because I like some of its smart completion features and such, I decided to try to learn working with it. But I have a hard time getting used to it as a decades long BASH user. Maybe I'm searching wrong but I can't figure out how to configure some basic stuff that I find convenient in BASH.</p>
<p>I'm having the following troubles:</p>
<ol>
<li><p><code><ALT+.></code> (ALT+period) in BASH invokes the last word from the previous command. E.g., I do <code>ls file.txt <ENTER></code>, then <code>cat <ALT+.></code> and I get <code>cat file.txt</code>. The caret (cursor) is after the word inserted by the key combo.
This doesn't work for me in ZSH. I can do <code>!$<TAB></code> instead of <code><ALT+.></code> but that's not too convenient. I also have working solution written by GPT but it throws me an error even tho it works. See my <code>.zshrc</code> below.
I have also found this question—<a href="https://unix.stackexchange.com/questions/685008/how-to-use-alt-in-zsh-with-vim-bindings">vi mode - How to use <code>Alt + .</code> in zsh with Vim bindings - Unix & Linux Stack Exchange</a>—where I learned I can use <code><ALT>+_</code> but that's not exactly comfortable either for two reasons: Firstly, underscore is bound to <code><SHIFT>+-</code> on US and CZ keyboards which I use (so it's actually uncomfortable <code><ALT>+<SHIFT>+-</code> combo which has the potential to change keyboard layout with <code><ALT>+<SHIFT></code>), and secondly, it produces the same error "No such shell function insert-last-word" as the binding I already have. I tried <code>append-last-word</code> instead which doesn't work at all.</p>
</li>
<li><p><code><HOME></code> and <code><END></code> to jump to the start/end of the current line.
I can do <CTRL+A>/<CTRL+E> to do the start/end jumps but that's not exactly convenient either.</p>
</li>
<li><p>I have there working binding to use <code><ALT>+LEFT/WRITE_ARROW</code> to jump by words. However, this doesn't seem to respect <code>$WORDCHARS</code>, e.g., if there's a path (includes <code>/</code>), it jumps over it as a whole. What may I be doing wrong? My <code>$WORDCHARS</code>:</p>
</li>
</ol>
<pre class="lang-bash prettyprint-override"><code>$ echo $WORDCHARS
*?_-.[]~=/&;!#$%^(){}<>
</code></pre>
<ol start="4">
<li>Edit: one more thing: I just installed a package (<code>sudo pacman -S hdparm</code> successfully. And right after that, when I try to invoke the command, I type <code>hdpar<TAB></code>, expecting to get the <code>hdparm</code> auto-completed. Nope, I get various completely random suggestions (<code>cdparanoia gdparttopng</code>), none of them being <code>hdparm</code>. When I type the command in full, though, it works, so that means it <em>is</em> a valid command. Do I need to run some completion update? :o (sorry, confused and slightly disillusioned).</li>
</ol>
<p>I found that <code>bindkey -e # Use emacs key bindings</code> brings some things to life, such as <code><CTRL+R></code> for reverse search. But not all of them, see my biggest pains above.</p>
<p>Here's the config I have in <code>~/.zshrc</code>: (I'm posting it in its entirety but everything below line 52 is out of this question's scope, it works fine and is unrelated.) Please see the comments in the file for explanations of what doesn't work and how</p>
<pre class="lang-bash prettyprint-override"><code># The following lines were added by compinstall
zstyle ':completion:*' completer _expand _complete _ignored _approximate
zstyle ':completion:*' format 'Completing %d'
zstyle ':completion:*' list-colors ''
zstyle ':completion:*' matcher-list '' 'm:{[:lower:]}={[:upper:]}' 'm:{[:lower:][:upper:]}={[:upper:][:lower:]}' 'r:|[._-]=** r:|=** l:|=*'
zstyle ':completion:*' menu select=2
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
zstyle ':completion:*' use-compctl true
zstyle :compinstall filename '/home/edison23/.zshrc'
autoload -Uz compinit
compinit
# End of lines added by compinstall
# Lines configured by zsh-newuser-install
HISTFILE=~/.bash_history
HISTSIZE=1000000
SAVEHIST=1000000
unsetopt autocd
bindkey -v
# End of lines configured by zsh-newuser-install
#PS1='%d $ '
autoload -U colors && colors
PS1="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%~ %{$reset_color%}$ "
# Set key bindings to emulate Bash behavior
bindkey -e # Use emacs key bindings
# Should bind <ALT+.> to inserting last word but it works only for the last word of *the same command*,
# e.g. `cat foo; echo bar; cat <alt+.>` gives me `cat foo` instead of `cat bar`. Not desired.
# bindkey '^[.' history-beginning-search-backward
# This works but throws "No such shell function `insert-last-word'" error at the same time which is annoying, to say the least.
# Define the widget
zle -N insert-last-word
# Bind the widget to Alt + .
bindkey '^[.' insert-last-word
# Move to the beginning and end of the line using Home and End keys
# Doesn't work at all
bindkey 'Home' beginning-of-line # Home key to go to the start of the line
bindkey 'End' end-of-line # End key to go to the end of the line
# Move by words using <ALT+LEFT/RIGHT> (works)
# TODO: Any option to customize word delimiters?
bindkey "^[[1;3C" forward-word
bindkey "^[[1;3D" backward-word
# Everything below is fine and of no relation to the question posted on SO
# ls aliases
alias ll='ls -la -v --time-style=posix-long-iso --group-directories-first --color=always'
alias la='ls -A'
alias l='ls -CF'
#ls hidden files, no sorting
alias ls='ls -v --color="auto" --time-style=posix-long-iso --group-directories-first'
# CRC32 into filename
# rhash required, I think
alias embedcrc32='rhash -e --embed-crc-delimiter="."'
# Copy node path
# Use `| xsel -bi` on X11
fpath() {
echo -n `readlink -f "${1}"` | wl-copy
}
# color diff
alias dwdiff_rw='dwdiff -P -cred,green -A best -w [- -x -] -y {+ -z +}'
</code></pre>
<p>Thank you very much for your time, help, and patience with a ZSH newbie. Any bonus useful tips and tricks appreciated as well :)</p>
https://unix.stackexchange.com/q/7935090Ahmad Ismailhttps://unix.stackexchange.com/users/2065742025-04-09T10:25:23Z2025-04-12T12:57:05Z
<p>I have a zle like:</p>
<pre><code>function _ctrl-shift-left() {
((REGION_ACTIVE)) || zle set-mark-command # Selection
zle backward-word
}
zle -N _ctrl-shift-left
bindkey '^[[1;6D' _ctrl-shift-left # Ctrl+Shift+Left to select till the beginning of the previous word.
</code></pre>
<p>For example for the bellow command</p>
<pre><code>$ curl -s "https://samplesite.com/101146/07/data-processing/13/?#/101146-132519042909.html" | grep -oP 'https://dl\d*\.samplesite\.com/Files/[^"]+\.rar' | sed 's/\?nocache=[^&]*//g'
</code></pre>
<p>when I press <kbd>ctrl-shift-left</kbd>, I want to stop the cursor at <code>'</code> and <code>"</code>.</p>
<p>How can I do that?</p>
https://unix.stackexchange.com/q/5620231canichttps://unix.stackexchange.com/users/2798032020-01-14T11:00:17Z2025-01-01T09:47:00Z
<p>zle widgets working by Ctrl-Right keys, Ctrl-Left keys (first widget is starting selection mode):</p>
<pre><code>c-sel-r-reg () {
local WORDCHARS=${WORDCHARS/\/}
zle select-in-word
}
zle -N c-sel-r-reg
bindkey "^[Oc" c-sel-r-reg
c-l-reg () {
local WORDCHARS=${WORDCHARS/\/}
zle vi-backward-word
zle end-of-line
zle -U " "
}
zle -N c-l-reg
bindkey "^[Od" c-l-reg
</code></pre>
https://unix.stackexchange.com/q/7840561Ahmad Ismailhttps://unix.stackexchange.com/users/2065742024-09-25T11:47:08Z2024-09-26T06:58:53Z
<p>I need a zle function that will print the character before and after the cursor.</p>
<pre><code>print-char-before-after() {
# Get the position of the cursor
local cursor_pos=$CURSOR
# Get the text in the current line
local line_text=${BUFFER}
}
# Bind the function to a key combination (for example, Ctrl+P)
zle -N print-char-before-after
bindkey '^P' print-char-before-after
</code></pre>
<p>Considering the pipe to be the cursor, if input is:</p>
<pre><code>This is a inp|ut
</code></pre>
<p>The output will be <code>pu</code></p>
<p>It needs to work on beginning and end of line and also on multiline buffer.</p>
<p>if input is:</p>
<pre><code>|This is a input
</code></pre>
<p>The output will be <code>T</code></p>
<p>if input is:</p>
<pre><code>This is a input|
</code></pre>
<p>The output will be <code>t</code></p>
<p>How can I do that?</p>
<p><strong>UPDATE 1:</strong></p>
<p>The code that sort of solves my problem is:</p>
<pre><code>print-char-before-after() {
# Get the characters from the left and right buffers
local before_char=""
local after_char=""
# Check if the cursor is not at the beginning of the line
if [[ -n "$LBUFFER" ]]; then
before_char=${LBUFFER[-1]} # Last character of LBUFFER
fi
# Check if the cursor is not at the end of the line
if [[ -n "$RBUFFER" ]]; then
after_char=${RBUFFER[1]} # First character of RBUFFER
fi
# Print the characters before and after
echo "${before_char}${after_char}"
}
# Bind the function to a key combination (for example, Ctrl+P)
zle -N print-char-before-after
bindkey '^P' print-char-before-after
</code></pre>
https://unix.stackexchange.com/q/7809881Sarp Userhttps://unix.stackexchange.com/users/6089702024-07-29T04:35:19Z2024-08-04T12:43:52Z
<p>I am trying to create a keybinding for CMD (on Mac)/CTRL (on Linux) + Backspace to delete everything before the cursor. I cannot for the life of me figure out what key sequence I should use. I am currently trying this on a Mac but would like to learn about the correct sequence for Linux as well. I tried <code>^[[1;9~</code>, <code>^[[3;9?</code> and a few other, but nothing seems to work. I was able to verify the functionality of my widget by using <code>^?</code> (just the backspace key) and it seems to work fine, I just can't figure out how to combine it with the CMD/CTRL key. Any help would be much appreciated.</p>
<p>On a side note, could you help me decode what the key sequences mean? I have figured out that the last number is the combination of modifier keys, and any letters that come after are the key being modified. What is the first number? What are the double square brackets? What is the semicolon? Why are some sequences only <code>^A</code> while others are including all the other stuff?</p>
https://unix.stackexchange.com/q/7233451ianhttps://unix.stackexchange.com/users/677012022-11-02T05:30:27Z2024-03-27T16:46:43Z
<p>For example, if I hit the right combination of keys to enter <code>viins</code> and walk away from the terminal and forget, how can I tell when I get back? How can I tell what the default mode is? The guide has this:</p>
<blockquote>
<p>If you’re in the wrong mode, ‘bindkey -e’ in ~/.zshrc takes you to emacs mode and ‘bindkey -v’ to vi mode.</p>
</blockquote>
<p>But what <em>is</em> the mode? I'd like to know before typing. The $KEYMAP special variable is, as far as I can see, only available <em>within</em> zle widgets, and I can find nothing in <code>man zshzle</code> nor the zshell user guide as to how to tell what the current mode is.</p>
<p>Any help or insight will be much appreciated.</p>
https://unix.stackexchange.com/q/5788882grenmesterhttps://unix.stackexchange.com/users/1656782020-04-09T07:23:01Z2023-09-16T08:11:08Z
<p>From what I have seen, both <code>set -o emacs</code> and <code>bindkey -e</code> are used to modify the behavior of your terminal line editor to emulate <code>emacs</code> whereas <code>set -o vi</code> and <code>bindkey -v</code> are used to emulate <code>vi</code> keybindings. Since both <code>set -o</code> and <code>bindkey</code> seem to do the same thing, I was wondering what the differences between the two were and if there were any differences at all.</p>
<p>It seems like <code>set -o</code> works for most shells whereas <code>bindkey</code> is a <code>zle</code> specific command. Is this the only difference? Also, do they bind the same keys or are there some keys where <code>set -o</code> and <code>bindkey</code> bind to different operations?</p>
https://unix.stackexchange.com/q/7557671xdhmoorehttps://unix.stackexchange.com/users/1450252023-09-04T21:49:43Z2023-09-10T16:38:11Z
<p>In the <a href="https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html" rel="nofollow noreferrer">documentation for the Zsh Line Editor</a>, there is a section that says:</p>
<pre><code>For either in-string or out-string, the following escape sequences are recognised:
\a
bell character
\b
backspace
\e, \E
escape
\f
form feed
\n
linefeed (newline)
\r
carriage return
\t
horizontal tab
\v
vertical tab
\NNN
character code in octal
\xNN
character code in hexadecimal
\uNNNN
unicode character code in hexadecimal
\UNNNNNNNN
unicode character code in hexadecimal
\M[-]X
character with meta bit set
\C[-]X
control character
^X
control character
In all other cases, ‘\’ escapes the following character. Delete is written as ‘^?’. Note that ‘\M^?’ and ‘^\M?’ are not the same...
</code></pre>
<p>How should those last two sequences be interpreted? My guess is:</p>
<pre><code>\M^? - delete with the meta bit set?
^\M? - control + question mark with the meta bit set
</code></pre>
<p>Is this correct?</p>
https://unix.stackexchange.com/q/4753106bluenote10https://unix.stackexchange.com/users/637222018-10-13T21:02:30Z2023-08-05T10:10:06Z
<p>This is a follow-up to <a href="https://unix.stackexchange.com/q/373795/63722">this question</a>: I'm trying to create a keyboard shortcut for the terminal file manager <a href="https://github.com/ranger/ranger" rel="noreferrer">ranger</a>, in order to jump from the zsh prompt into the file manager with a single keystroke. I was following the linked answer, adding this to my <code>.zshrc</code>:</p>
<pre class="lang-bash prettyprint-override"><code>run_ranger () { echo; ranger; zle redisplay }
zle -N run_ranger
bindkey '^f' run_ranger
</code></pre>
<p>The key binding itself works, however ranger fails to start with <code>Error: Must run ranger from terminal</code>. I had a look at the <a href="https://github.com/ranger/ranger/blob/325f41e6a841ab1413020b9d2d86c3922d90f407/ranger/core/main.py#L137" rel="noreferrer">ranger code (Python)</a> and it performs the common check <code>sys.stdin.isatty()</code> to verify that is has a TTY stdin. How can I modify the zle widget so that stdin is properly set?</p>
https://unix.stackexchange.com/q/6909106mayostardhttps://unix.stackexchange.com/users/5147052022-02-16T12:42:35Z2023-05-02T11:11:53Z
<p>In bash, I used this:</p>
<pre><code>% cat .inputrc
"\e[A": history-search-backward
"\e[B": history-search-forward
</code></pre>
<p>I'd like the same behavior in .zsh, so I've tried these bindkeys:</p>
<pre><code>"^[[A" history-beginning-search-backward
"^[[B" history-beginning-search-forward
</code></pre>
<p>These are ALMOST what I want, except when I just up arrow (with no entered text) to see the immediately previous command, the cursor is positioned at the beginning of the line, rather than the end.</p>
<p>'bindkey '^[[A' history-search-backward' is close, it puts the cursor at the end BUT it only searches for the first word, e.g. if I type 'ssh foo' it will return the most recent 'ssh' in the history, not the most recent line starting with 'ssh foo'. Also, this positions the cursor at the end of the line when I do search, which is also not what I want (both bash's <code>history-search-backwards</code> and zsh's <code>history-beginning-search-backward</code> keep the cursor where I stopped typ</p>
<p>is it possible to get the cursor positioned at the end of the line with the same functionality as <code>history-beginning-search-backward</code>?</p>
https://unix.stackexchange.com/q/6284233Hazem Elmahyhttps://unix.stackexchange.com/users/4472212021-01-10T01:36:08Z2023-04-26T11:08:31Z
<p>Zsh vi mode doesn't have ctrl-o behavior set by default how do I get to work like in vim ?</p>
https://unix.stackexchange.com/q/2587876Leandroshttps://unix.stackexchange.com/users/239942016-01-30T21:39:25Z2023-03-24T01:31:01Z
<p>When editing multi-line commands, with escaped newlines, I cannot move up lines.</p>
<p>For example, suppose I enter <code>echo \</code> one one line, then I press <kbd>Enter</kbd>, and then I want to edit the <code>echo \</code> part of the command. Pressing <kbd>Up</kbd> doesn't move back to the first command line.</p>
<p>This works for long commands which wrap, but not with escaped newlines:</p>
<pre><code>_physical_up_line() { zle backward-char -n $COLUMNS }
_physical_down_line() { zle forward-char -n $COLUMNS }
zle -N physical-up-line _physical_up_line
zle -N physical-down-line _physical_down_line
bindkey -M vicmd "R" physical-up-line
bindkey -M vicmd "N" physical-down-line
</code></pre>
https://unix.stackexchange.com/q/7350120codepoethttps://unix.stackexchange.com/users/4565072023-02-10T07:20:36Z2023-02-10T07:36:48Z
<p>I would like to have a near-flawless "single file selector" with zsh. So far I haven't seen one in questions as they tend to focus on zle settings for one's command line. Let's say we are writing a single file selector that prompts user to provide a single file in some directory. Below is my beginning code that works but needs multiple improvements:</p>
<ol>
<li>Highlight: current menu selection item should be highlighted with reverse-video.</li>
<li>Order: the possible file completions should be shown below the prompt.</li>
<li>Prompt stepping: Prompt shouldn't step below after printing possible options above. This is related to 2.</li>
<li>Only one: Tab complete only first item, as we are asking for only 1 file name from user, not multiple. (This is what I mean by "functionally correct")</li>
<li>Size: If there are more possible completions than can fit below the prompt, that case is handled elegantly. Assume alternate screen, and no screen movement or scrolling available.</li>
<li>Bonus: If you could think of yet another nice feature to have for a single file selector, feel free to add. Though it would be nice to have it in a separate listing or with comments to identify how to remove your that from solution for 1-5 above.</li>
</ol>
<p>Note: The answer would be self-contained zsh code. It wouldn't ask user to edit .zshrc or install some zsh extension/plugin/framework/library.</p>
<p>Create a file <code>foofiles.zsh</code> in a temp directory, chmod and run it:</p>
<pre class="lang-bash prettyprint-override"><code>#!/usr/bin/env zsh
setopt extendedglob
setopt globcomplete
setopt nomenucomplete
mkdir ./foo 2>/dev/null
cd ./foo
touch abcd ebcf gbch ijkl mnop qrst unov wxyz
cd ..
function foofileselector {
cd ./foo/
local curcontext=foofileselector:::
vared -p "Select file: " -c foofile;
cd ..
}
function _foofileselector {
_files -W ./foo/
}
zstyle ':completion:foofileselector:*:' insert-tab false completer _foofileselector
autoload -Uz compinit && compinit
foofileselector
[[ -z $foofile ]] && foofile="NIL"
echo "You selected: $foofile"
rm -rf ./foo/
</code></pre>
<p>This is how it looks like:
<a href="https://i.sstatic.net/rqhHB.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/rqhHB.gif" alt="foofiles.zsh file completion run" /></a></p>
<p>This is close (<a href="https://github.com/zimfw/zimfw" rel="nofollow noreferrer">zim</a>'s file completion):
<a href="https://i.sstatic.net/Zr4Zq.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Zr4Zq.gif" alt="zim file completion run" /></a></p>
https://unix.stackexchange.com/q/7259200budubotihttps://unix.stackexchange.com/users/3709982022-11-22T17:08:32Z2022-11-23T11:14:17Z
<p>Since I use both bash and zsh, I'd like to learn the zle's emacs keymap (since this is the default in bash). My problem is that macOS's option key does not work with the meta shortcuts (like M-. for yanking the last argument from the last command). Is there a way to set the option key to meta?</p>
https://unix.stackexchange.com/q/7246392jdshttps://unix.stackexchange.com/users/5489882022-11-12T18:08:53Z2022-11-12T18:08:53Z
<p>Is there a way to change the prompt of zle widgets such as history-incremental-backward-search?</p>
<p>For instance I wonder if we can colorize "bck-i-search:" or change the string, or colorize the search entries...</p>
<p>Thanks</p>
https://unix.stackexchange.com/q/6654612Martin Vegterhttps://unix.stackexchange.com/users/430072021-08-20T10:12:02Z2022-10-22T16:06:10Z
<p>When using <kbd>Ctrl+w</kbd> or <kbd>Alt+Backspace</kbd> or <kbd>Alt+d</kbd> keyboard shortcuts to delete words backward/forward, I know I can define the WORDCHARS characters:</p>
<pre><code>WORDCHARS='~!#$%^&*(){}[]<>?.+;-'
</code></pre>
<p>for example, using <kbd>Ctrl+w</kbd> from the end of this string, will successively delete each word</p>
<pre><code>echo aaa bbb ccc ddd eee
</code></pre>
<p>but if I have quoted strings, how can I treat the whole quoted block as one entity?</p>
<pre><code>echo 'aaa bbb ccc' 'ddd eee fff'
</code></pre>
<p>so, <kbd>Ctrl+w</kbd> should delete whole 'ddd eee fff' in one go.
Same behavior should be for strings quoted with double quotes <code>"</code></p>
https://unix.stackexchange.com/q/7089253Ahmad Ismailhttps://unix.stackexchange.com/users/2065742022-07-07T07:29:52Z2022-07-14T16:06:11Z
<p>I am working on a zsh widget to fuzzy search history.</p>
<pre><code>skim-history() {
origquery=${BUFFER}
output=$(history -1 1 | sd '^[ ]*[0-9]*[ ]*' '' | sk --tac --no-sort --exact)
if [ $? -eq 0 ]; then
BUFFER=$output
else
BUFFER=$origquery
fi
CURSOR=$#BUFFER
}
zle -N skim-history
bindkey '^R' skim-history
</code></pre>
<p>In <code>history -1 1 | sd '^[ ]*[0-9]*[ ]*' '' | sk --tac --no-sort --exact</code>, <a href="https://github.com/chmln/sd" rel="nofollow noreferrer">sd</a> is used instead of sed, which just removes numbers from the beginning. <a href="https://github.com/lotabout/skim" rel="nofollow noreferrer">sk</a> is an alternative for fzf.</p>
<p>This widget works pretty well except multi-line commands.</p>
<p>For example: it shows</p>
<p><a href="https://i.sstatic.net/ZlcIw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ZlcIw.png" alt="enter image description here" /></a></p>
<p>like:</p>
<p><a href="https://i.sstatic.net/gbZtS.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/gbZtS.png" alt="enter image description here" /></a></p>
<p>So, it replace newline with <code>\n</code>. This is not something i want.</p>
<p>The command looks like the following in <code>.zsh_history</code> file:</p>
<pre><code>: 1657176206:0;function brcd() {\
cd $(broot --only-folders --hidden --cmd "$1;:pp")\
}\
</code></pre>
<p>What I found is !NUM get the history in correct format.</p>
<p><a href="https://i.sstatic.net/uFGbc.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/uFGbc.png" alt="enter image description here" /></a></p>
<p>what changes do I need to make on my widget? How can I solve this problem?</p>
https://unix.stackexchange.com/q/7086270kohane15https://unix.stackexchange.com/users/4609422022-07-05T05:21:15Z2022-07-07T14:52:44Z
<p>Hope you're doing well.</p>
<p>I have the following in my <code>.zshrc</code>:</p>
<pre class="lang-bash prettyprint-override"><code># 1. invoke zoxide to pick dir
# 2. open target dir in ranger
# 3. when exiting ranger, cd into last dir
zoxide_to_ranger () {
eval 'ranger "$(zoxide query -i)" --choosedir=$HOME/.rangerdir < $TTY'
LASTDIR=$(< ~/.rangerdir)
cd "$LASTDIR" || exit
# ensure `precmds` are run after `cd`
local precmd
for precmd in $precmd_functions; do
$precmd
done
zle reset-prompt
}
zle -N zoxide_to_ranger
bindkey '^f' zoxide_to_ranger
</code></pre>
<p>As suggested in the inline comment, it's a handy tool such that when I trigger <code>Ctrl-f</code>, it'll prompt me a fzf menu, and upon selection it'll open ranger in that dir.</p>
<p>The problem is that, when I want to cancel selection by pressing <code>Esc</code>, it'll still open up ranger and send me the home dir, which is not what I want.</p>
<p>So my question is, how do I cancel such a zle function after triggering the bindkey?</p>
<p>Config:</p>
<p>zsh 5.9 (x86_64-pc-linux-gnu)</p>
<p>OS: Manjaro Linux</p>
<p>Any input is much appreciated. Thank you:)</p>
https://unix.stackexchange.com/q/6850086mcphttps://unix.stackexchange.com/users/4093312022-01-04T17:30:28Z2022-03-27T07:35:44Z
<p>I am using zsh with <code>bindkeys -v</code>.</p>
<p><code>Alt + .</code> does not work as expected. It seems to repeat what is currently typed in stdin, but not entered, on the next line.</p>
<p><a href="https://unix.stackexchange.com/questions/490801/alt-dot-shows-instead-of-a-previous-argument">This post</a> seems to imply it does work as it does in bash, which is to grab the last argument to the last command entered.</p>
<p>What is needed to make this work as intended?</p>
https://unix.stackexchange.com/q/6869443Tom Halehttps://unix.stackexchange.com/users/1433942022-01-19T05:23:54Z2022-01-19T20:29:51Z
<p>In the <code>zsh</code> manual <a href="https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html#Local-Keymaps" rel="nofollow noreferrer">18.2.2 Local Keymaps</a> it refers to the "global keymap" thrice.</p>
<p>Where is this term defined?</p>
<p>How do I find out what is my current global keymap, and how would I change it?</p>
https://unix.stackexchange.com/q/6451372Steven Luhttps://unix.stackexchange.com/users/124972021-04-14T23:44:57Z2021-04-16T07:34:02Z
<p>I found that I was able to implement <kbd>Ctrl</kbd>+<kbd>Delete</kbd> in bash using <code>"^[[3;5~": shell-kill-word</code> inside my <code>~/.inputrc</code>.
However, zsh does not use inputrc, and ZLE has no <code>shell-kill-word</code> option that I can find in the <a href="http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html" rel="nofollow noreferrer">doc</a>.</p>
<p>Is this possible? We can surely do it with some custom functions right? What might such a function look like?</p>
https://unix.stackexchange.com/q/1850543ThiefMasterhttps://unix.stackexchange.com/users/25052015-02-16T02:58:19Z2020-12-26T22:14:56Z
<p>If I have just some whitespace at be beginning of my zsh prompt, I can usually <code>^W</code> (<code>backward-kill-word</code>) it away. However, as soon as I load <code>select-word-style</code> - even if I don't select any special style - this doesn't work anymore. When I <code>select-word-style shell</code> it works again, but that's not the style I want by default...</p>
<p>Is there a way to avoid this annoying behavior? Of course I could create a custom widget which removes the whitespace if necessary and otherwise calls <code>backward-kill-word</code>, but that feels like a pretty dirty solution.</p>
https://unix.stackexchange.com/q/25069025ironsandhttps://unix.stackexchange.com/users/440012015-12-21T11:21:29Z2020-12-16T17:18:10Z
<p>I want to delete a word by <kbd>Ctrl</kbd>+<kbd>W</kbd> in <code>zsh</code> like.</p>
<pre><code>vim /foo/bar^W
vim /foo/
</code></pre>
<p>And found a solution for <code>bash</code>, but <code>bind</code> is not in zsh function.</p>
<p><a href="https://unix.stackexchange.com/questions/27927/is-it-possible-to-configure-ctrl-w-delete-word">Is it possible to configure ctrl-w (delete word)?</a></p>
<p>How can I configure <kbd>Ctrl</kbd>+<kbd>W</kbd> as a <code>delete-word</code>?</p>
https://unix.stackexchange.com/q/1522635HopefullyHelpfulhttps://unix.stackexchange.com/users/820782014-08-26T14:56:22Z2020-09-26T16:49:14Z
<p>I need a way to use the current line which the users typed into as variable for a shell function.</p>
<p>my current code, which can be called by ctrl+r</p>
<pre><code>zle -N search
bindkey "^R" search
search () {
read str;
fc -ln -30 | grep $(printf "%q\n" "$str");
}
</code></pre>
<p>or simply, to call it as a function</p>
<pre><code>search () {
fc -ln -30 | grep $(printf "%q\n" "$1");
}
</code></pre>
<p>updated: target pseudo code, to call as a function called by ctrl+r that needs no further input prompt</p>
<pre><code>zle -N search
bindkey "^R" search
search ()
echo ""; #for better formatting because ctrl+R is not enter so the BUFFER(current line) gets corrupted and looks messy and the current input is not correctly shown
fc -ln -30 | grep $(printf "%q\n" "$BUFFER"); #edited to be the solution where $BUFFER is the current terminal line
}
</code></pre>
https://unix.stackexchange.com/q/37379513user86041https://unix.stackexchange.com/users/02017-06-27T23:54:55Z2020-09-09T13:16:44Z
<p>In <a href="/questions/tagged/bash" class="post-tag" title="show questions tagged 'bash'" rel="tag">bash</a> one can do the following:</p>
<pre><code>bind -x '"\C-l":ls'
</code></pre>
<p>to map <kbd>Ctrl</kbd>+<kbd>L</kbd> to external (ie. system's rather than <a href="/questions/tagged/readline" class="post-tag" title="show questions tagged 'readline'" rel="tag">readline</a> function)</p>
<p><code>ls</code> command to list directory contents.</p>
<p>How to do the same in <a href="/questions/tagged/zsh" class="post-tag" title="show questions tagged 'zsh'" rel="tag">zsh</a>, and preferably with <kbd>Alt</kbd> (<code>\M-l</code>?)</p>
<p>instead of <kbd>Ctrl</kbd> as it is seemingly already bound to <code>clear</code> to clear the screen.</p>
https://unix.stackexchange.com/q/5953503noibehttps://unix.stackexchange.com/users/3681162020-06-27T10:02:04Z2020-06-27T16:22:14Z
<p>I have the following function in my <code>.zshrc</code></p>
<pre><code>function foo() {
filename="$(fzf)"
}
zle -N foo
bindkey '^X' foo
</code></pre>
<p>now if I call <code>foo</code> by typing it, it works as expected, but if I call it via the key-binding with <code>^X</code>, the <code>fzf</code> buffer is empty.</p>
<p>Can anyone reproduce this? How can I fix this?</p>
https://unix.stackexchange.com/q/3771843user852573https://unix.stackexchange.com/users/2386022017-07-08T14:53:25Z2020-05-20T23:54:26Z
<p>I'm using the <code>zsh</code> shell, and I'm trying to install a few key bindings to use keys similar to the ones I would use in a Vim buffer, when a completion menu is open.</p>
<p>So, inside the <code>menuselect</code> keymap I've bound the keys <code>j</code> and <code>k</code> to the <code>zle</code> widgets <code>down-line-or-history</code> and <code>up-line-or-history</code>, by adding the following lines inside <code>~/.zshrc</code>:</p>
<pre><code>bindkey -M menuselect 'j' down-line-or-history
bindkey -M menuselect 'k' up-line-or-history
</code></pre>
<p><code>down-line-or-history</code> and <code>up-line-or-history</code> are described in <code>man zshzle</code> as follows:</p>
<pre><code>down-line-or-history (^N ESC-[B) (j) (ESC-[B)
Move down a line in the buffer, or if already at the bottom line, move to the next event in the his‐
tory list.
up-line-or-history (^P ESC-[A) (k) (ESC-[A)
Move up a line in the buffer, or if already at the top line, move to the previous event in the history
list.
</code></pre>
<p>Now, I would like to bind <code>C-d</code> and <code>C-u</code> to the same widgets, but repeating them an arbitrary number of times, for example <code>5</code>.</p>
<p>At first I tried this simple code:</p>
<pre><code>some-widget() {
zle backward-char -n 5
}
zle -N some-widget
bindkey '^D' some-widget
</code></pre>
<p>It binds <code>C-d</code> to the zle widget <code>backward-char</code>, but repeats it <code>5</code> times.</p>
<p>Then, I tried to rewrite the code, moving the key binding from the default keymap to the <code>menuselect</code> keymap:</p>
<pre><code>some-widget() {
zle backward-char -n 5
}
zle -N some-widget
bindkey -M menuselect '^D' some-widget
</code></pre>
<p>But it doesn't work as I expected, because when I hit <code>C-d</code> while a completion menu is open, <code>zle</code> seems to execute the default widget bound to <code>C-d</code>, which is <code>delete-char-or-list</code>:</p>
<pre><code>delete-char-or-list (^D) (unbound) (unbound)
Delete the character under the cursor. If the cursor is at the end of the line, list possible comple‐
tions for the current word.
</code></pre>
<p>It exits the current completion menu, and lists possible completions for the current word, instead of moving the cursor backward <code>5</code> times.</p>
<p>If it had worked as I expected, I would probably have ended up using this final code:</p>
<pre><code>fast-down-line-or-history() {
zle down-line-or-history -n 5
}
zle -N fast-down-line-or-history
bindkey -M menuselect '^D' fast-down-line-or-history
fast-up-line-or-history() {
zle up-line-or-history -n 5
}
zle -N fast-up-line-or-history
bindkey -M menuselect '^U' fast-up-line-or-history
</code></pre>
<p>But since it doesn't, I need to find how to repeat a <code>zle</code> widget when a completion menu is open.</p>
<p>How to modify the previous code, so that <code>down-line-or-history</code> is repeated <code>5</code> times when hitting <code>C-d</code> while the completion menu is open?</p>