most recent 30 from unix.stackexchange.com 2026-03-01T05:15:55Z https://unix.stackexchange.com/feeds/tag?tagnames=zle https://creativecommons.org/licenses/by-sa/4.0/rdf https://unix.stackexchange.com/q/786633 3 user172877 https://unix.stackexchange.com/users/0 2024-11-14T14:07:12Z 2025-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/801139 9 mgiuffrida https://unix.stackexchange.com/users/123247 2025-11-07T17:13:56Z 2025-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/776962 0 Nathan https://unix.stackexchange.com/users/118804 2024-05-22T15:38:54Z 2025-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 &quot;$(fzf --zsh)&quot; </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 &quot;^I&quot; fzf-completion &quot;^R&quot; fzf-history-widget &quot;^T&quot; fzf-file-widget &quot;^X.&quot; fzf-tab-debug &quot;^[c&quot; fzf-cd-widget </code></pre> https://unix.stackexchange.com/q/797896 5 edison23 https://unix.stackexchange.com/users/138286 2025-07-13T13:10:01Z 2025-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>&lt;ALT+.&gt;</code> (ALT+period) in BASH invokes the last word from the previous command. E.g., I do <code>ls file.txt &lt;ENTER&gt;</code>, then <code>cat &lt;ALT+.&gt;</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>!$&lt;TAB&gt;</code> instead of <code>&lt;ALT+.&gt;</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 &amp; Linux Stack Exchange</a>—where I learned I can use <code>&lt;ALT&gt;+_</code> but that's not exactly comfortable either for two reasons: Firstly, underscore is bound to <code>&lt;SHIFT&gt;+-</code> on US and CZ keyboards which I use (so it's actually uncomfortable <code>&lt;ALT&gt;+&lt;SHIFT&gt;+-</code> combo which has the potential to change keyboard layout with <code>&lt;ALT&gt;+&lt;SHIFT&gt;</code>), and secondly, it produces the same error &quot;No such shell function insert-last-word&quot; 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>&lt;HOME&gt;</code> and <code>&lt;END&gt;</code> to jump to the start/end of the current line. I can do &lt;CTRL+A&gt;/&lt;CTRL+E&gt; 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>&lt;ALT&gt;+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 *?_-.[]~=/&amp;;!#$%^(){}&lt;&gt; </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&lt;TAB&gt;</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>&lt;CTRL+R&gt;</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 &amp;&amp; colors PS1=&quot;%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%~ %{$reset_color%}$ &quot; # Set key bindings to emulate Bash behavior bindkey -e # Use emacs key bindings # Should bind &lt;ALT+.&gt; to inserting last word but it works only for the last word of *the same command*, # e.g. `cat foo; echo bar; cat &lt;alt+.&gt;` gives me `cat foo` instead of `cat bar`. Not desired. # bindkey '^[.' history-beginning-search-backward # This works but throws &quot;No such shell function `insert-last-word'&quot; 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 &lt;ALT+LEFT/RIGHT&gt; (works) # TODO: Any option to customize word delimiters? bindkey &quot;^[[1;3C&quot; forward-word bindkey &quot;^[[1;3D&quot; 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=&quot;auto&quot; --time-style=posix-long-iso --group-directories-first' # CRC32 into filename # rhash required, I think alias embedcrc32='rhash -e --embed-crc-delimiter=&quot;.&quot;' # Copy node path # Use `| xsel -bi` on X11 fpath() { echo -n `readlink -f &quot;${1}&quot;` | 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/793509 0 Ahmad Ismail https://unix.stackexchange.com/users/206574 2025-04-09T10:25:23Z 2025-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 &quot;https://samplesite.com/101146/07/data-processing/13/?#/101146-132519042909.html&quot; | grep -oP 'https://dl\d*\.samplesite\.com/Files/[^&quot;]+\.rar' | sed 's/\?nocache=[^&amp;]*//g' </code></pre> <p>when I press <kbd>ctrl-shift-left</kbd>, I want to stop the cursor at <code>'</code> and <code>&quot;</code>.</p> <p>How can I do that?</p> https://unix.stackexchange.com/q/562023 1 canic https://unix.stackexchange.com/users/279803 2020-01-14T11:00:17Z 2025-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/784056 1 Ahmad Ismail https://unix.stackexchange.com/users/206574 2024-09-25T11:47:08Z 2024-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=&quot;&quot; local after_char=&quot;&quot; # Check if the cursor is not at the beginning of the line if [[ -n &quot;$LBUFFER&quot; ]]; then before_char=${LBUFFER[-1]} # Last character of LBUFFER fi # Check if the cursor is not at the end of the line if [[ -n &quot;$RBUFFER&quot; ]]; then after_char=${RBUFFER[1]} # First character of RBUFFER fi # Print the characters before and after echo &quot;${before_char}${after_char}&quot; } # 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/780988 1 Sarp User https://unix.stackexchange.com/users/608970 2024-07-29T04:35:19Z 2024-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/723345 1 ian https://unix.stackexchange.com/users/67701 2022-11-02T05:30:27Z 2024-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/578888 2 grenmester https://unix.stackexchange.com/users/165678 2020-04-09T07:23:01Z 2023-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/755767 1 xdhmoore https://unix.stackexchange.com/users/145025 2023-09-04T21:49:43Z 2023-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/475310 6 bluenote10 https://unix.stackexchange.com/users/63722 2018-10-13T21:02:30Z 2023-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/690910 6 mayostard https://unix.stackexchange.com/users/514705 2022-02-16T12:42:35Z 2023-05-02T11:11:53Z <p>In bash, I used this:</p> <pre><code>% cat .inputrc &quot;\e[A&quot;: history-search-backward &quot;\e[B&quot;: history-search-forward </code></pre> <p>I'd like the same behavior in .zsh, so I've tried these bindkeys:</p> <pre><code>&quot;^[[A&quot; history-beginning-search-backward &quot;^[[B&quot; 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/628423 3 Hazem Elmahy https://unix.stackexchange.com/users/447221 2021-01-10T01:36:08Z 2023-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/258787 6 Leandros https://unix.stackexchange.com/users/23994 2016-01-30T21:39:25Z 2023-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/735012 0 codepoet https://unix.stackexchange.com/users/456507 2023-02-10T07:20:36Z 2023-02-10T07:36:48Z <p>I would like to have a near-flawless &quot;single file selector&quot; 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 &quot;functionally correct&quot;)</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&gt;/dev/null cd ./foo touch abcd ebcf gbch ijkl mnop qrst unov wxyz cd .. function foofileselector { cd ./foo/ local curcontext=foofileselector::: vared -p &quot;Select file: &quot; -c foofile; cd .. } function _foofileselector { _files -W ./foo/ } zstyle ':completion:foofileselector:*:' insert-tab false completer _foofileselector autoload -Uz compinit &amp;&amp; compinit foofileselector [[ -z $foofile ]] &amp;&amp; foofile=&quot;NIL&quot; echo &quot;You selected: $foofile&quot; 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/725920 0 buduboti https://unix.stackexchange.com/users/370998 2022-11-22T17:08:32Z 2022-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/724639 2 jds https://unix.stackexchange.com/users/548988 2022-11-12T18:08:53Z 2022-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 &quot;bck-i-search:&quot; or change the string, or colorize the search entries...</p> <p>Thanks</p> https://unix.stackexchange.com/q/665461 2 Martin Vegter https://unix.stackexchange.com/users/43007 2021-08-20T10:12:02Z 2022-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='~!#$%^&amp;*(){}[]&lt;&gt;?.+;-' </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>&quot;</code></p> https://unix.stackexchange.com/q/708925 3 Ahmad Ismail https://unix.stackexchange.com/users/206574 2022-07-07T07:29:52Z 2022-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 &quot;$1;:pp&quot;)\ }\ </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/708627 0 kohane15 https://unix.stackexchange.com/users/460942 2022-07-05T05:21:15Z 2022-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 &quot;$(zoxide query -i)&quot; --choosedir=$HOME/.rangerdir &lt; $TTY' LASTDIR=$(&lt; ~/.rangerdir) cd &quot;$LASTDIR&quot; || 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/685008 6 mcp https://unix.stackexchange.com/users/409331 2022-01-04T17:30:28Z 2022-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/686944 3 Tom Hale https://unix.stackexchange.com/users/143394 2022-01-19T05:23:54Z 2022-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 &quot;global keymap&quot; 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/645137 2 Steven Lu https://unix.stackexchange.com/users/12497 2021-04-14T23:44:57Z 2021-04-16T07:34:02Z <p>I found that I was able to implement <kbd>Ctrl</kbd>+<kbd>Delete</kbd> in bash using <code>&quot;^[[3;5~&quot;: 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/185054 3 ThiefMaster https://unix.stackexchange.com/users/2505 2015-02-16T02:58:19Z 2020-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/250690 25 ironsand https://unix.stackexchange.com/users/44001 2015-12-21T11:21:29Z 2020-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/152263 5 HopefullyHelpful https://unix.stackexchange.com/users/82078 2014-08-26T14:56:22Z 2020-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/373795 13 user86041 https://unix.stackexchange.com/users/0 2017-06-27T23:54:55Z 2020-09-09T13:16:44Z <p>In <a href="/questions/tagged/bash" class="post-tag" title="show questions tagged &#39;bash&#39;" 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 &#39;readline&#39;" 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 &#39;zsh&#39;" 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/595350 3 noibe https://unix.stackexchange.com/users/368116 2020-06-27T10:02:04Z 2020-06-27T16:22:14Z <p>I have the following function in my <code>.zshrc</code></p> <pre><code>function foo() { filename=&quot;$(fzf)&quot; } 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/377184 3 user852573 https://unix.stackexchange.com/users/238602 2017-07-08T14:53:25Z 2020-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>