Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
5
votes
1
answers
119
views
How to configure ZSH to behave more like BASH
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 ho...
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.
I'm having the following troubles:
1. `
(ALT+period) in BASH invokes the last word from the previous command. E.g., I do
ls file.txt , then
cat and I get
cat file.txt`. The caret (cursor) is after the word inserted by the key combo.
This doesn't work for me in ZSH. I can do !$
instead of ` 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
.zshrc` below.
I have also found this question—[vi mode - How to use Alt + .
in zsh with Vim bindings - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/685008/how-to-use-alt-in-zsh-with-vim-bindings)—where I learned I can use +_
but that's not exactly comfortable either for two reasons: Firstly, underscore is bound to +-
on US and CZ keyboards which I use (so it's actually uncomfortable ++-
combo which has the potential to change keyboard layout with +
), and secondly, it produces the same error "No such shell function insert-last-word" as the binding I already have. I tried append-last-word
instead which doesn't work at all.
1. ` and
` to jump to the start/end of the current line.
I can do / to do the start/end jumps but that's not exactly convenient either.
1. I have there working binding to use +LEFT/WRITE_ARROW
to jump by words. However, this doesn't seem to respect $WORDCHARS
, e.g., if there's a path (includes /
), it jumps over it as a whole. What may I be doing wrong? My $WORDCHARS
:
$ echo $WORDCHARS
*?_-.[]~=/&;!#$%^(){}
4. Edit: one more thing: I just installed a package (sudo pacman -S hdparm
successfully. And right after that, when I try to invoke the command, I type hdpar
, expecting to get the hdparm
auto-completed. Nope, I get various completely random suggestions (cdparanoia gdparttopng
), none of them being hdparm
. When I type the command in full, though, it works, so that means it _is_ a valid command. Do I need to run some completion update? :o (sorry, confused and slightly disillusioned).
I found that bindkey -e # Use emacs key bindings
brings some things to life, such as `` for reverse search. But not all of them, see my biggest pains above.
Here's the config I have in ~/.zshrc
: (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
# 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 to inserting last word but it works only for the last word of *the same command*,
# e.g. cat foo; echo bar; cat
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 (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 +}'
Thank you very much for your time, help, and patience with a ZSH newbie. Any bonus useful tips and tricks appreciated as well :)
edison23
(155 rep)
Jul 13, 2025, 01:10 PM
• Last activity: Jul 31, 2025, 08:14 PM
0
votes
2
answers
42
views
Stop the cursor at single and double quote
I have a zle like: 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. For example for the bellow command $ curl -s "htt...
I have a zle like:
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.
For example for the bellow command
$ 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'
when I press ctrl-shift-left, I want to stop the cursor at
'
and "
.
How can I do that?
Ahmad Ismail
(2998 rep)
Apr 9, 2025, 10:25 AM
• Last activity: Apr 12, 2025, 12:57 PM
1
votes
2
answers
361
views
How to cancel selection mode in zsh in other way than add text to $BUFFER?
zle widgets working by Ctrl-Right keys, Ctrl-Left keys (first widget is starting selection mode): 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...
zle widgets working by Ctrl-Right keys, Ctrl-Left keys (first widget is starting selection mode):
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
canic
(61 rep)
Jan 14, 2020, 11:00 AM
• Last activity: Jan 1, 2025, 09:47 AM
3
votes
1
answers
99
views
How to add a zle-line-init hook without replacing existing hook?
I would like to run some code when zle becomes active. The only way I know how to do that is ```zsh zle-line-init() { echo hi } zle -N zle-line-init ``` But AFAIK this will replace any potential existing `zle-line-init` hooks. How can I either add to existing hooks (like how precmd has a `precmd_fun...
I would like to run some code when zle becomes active.
The only way I know how to do that is
zle-line-init() {
echo hi
}
zle -N zle-line-init
But AFAIK this will replace any potential existing zle-line-init
hooks.
How can I either add to existing hooks (like how precmd has a precmd_functions
array), or restore the previous state of zle-line-init
after my function runs?
user172877
Nov 14, 2024, 02:07 PM
• Last activity: Nov 17, 2024, 07:42 AM
1
votes
1
answers
75
views
Print the character before and after the cursor
I need a zle function that will print the character before and after the cursor. 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...
I need a zle function that will print the character before and after the cursor.
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
Considering the pipe to be the cursor, if input is:
This is a inp|ut
The output will be
pu
It needs to work on beginning and end of line and also on multiline buffer.
if input is:
|This is a input
The output will be T
if input is:
This is a input|
The output will be t
How can I do that?
**UPDATE 1:**
The code that sort of solves my problem is:
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} # 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
Ahmad Ismail
(2998 rep)
Sep 25, 2024, 11:47 AM
• Last activity: Sep 26, 2024, 06:58 AM
1
votes
0
answers
119
views
Key sequences for command/ctrl + backspace
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 t...
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
^[[1;9~
, ^[[3;9?
and a few other, but nothing seems to work. I was able to verify the functionality of my widget by using ^?
(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.
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 ^A
while others are including all the other stuff?
Sarp User
(21 rep)
Jul 29, 2024, 04:35 AM
• Last activity: Aug 4, 2024, 12:43 PM
0
votes
0
answers
469
views
How to setup zsh with fzf history backsearch in tmux (I get zle errors)
I am using tmux with the zsh shell. I try to setup fzf, and I do it like this (inside my zshrc): ``` eval "$(fzf --zsh)" ``` 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. When I try to invoke the history back search manua...
I am using tmux with the zsh shell.
I try to setup fzf, and I do it like this (inside my zshrc):
eval "$(fzf --zsh)"
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.
When I try to invoke the history back search manually, I get:
❯ fzf-history-widget
fzf-history-widget:zle:14: widgets can only be called when ZLE is active
A bit of googling suggests, that I need to load zle first, but also:
❯ zmodload zsh/zle
Does not work.
What is wrong? What can I do?
Edit:
As requested, here is the the output of bindkey | grep fzf
:
❯ bindkey | grep fzf
"^I" fzf-completion
"^R" fzf-history-widget
"^T" fzf-file-widget
"^X." fzf-tab-debug
"^[c" fzf-cd-widget
Nathan
(173 rep)
May 22, 2024, 03:38 PM
• Last activity: May 22, 2024, 07:00 PM
1
votes
1
answers
1090
views
What is the current zle keymap/mode?
For example, if I hit the right combination of keys to enter `viins` 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: > If you’re in the wrong mode, ‘bindkey -e’ in ~/.zshrc takes you to emacs mode and ‘bindkey -...
For example, if I hit the right combination of keys to enter
viins
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:
> If you’re in the wrong mode, ‘bindkey -e’ in ~/.zshrc takes you to emacs mode and ‘bindkey -v’ to vi mode.
But what *is* the mode? I'd like to know before typing. The $KEYMAP special variable is, as far as I can see, only available *within* zle widgets, and I can find nothing in man zshzle
nor the zshell user guide as to how to tell what the current mode is.
Any help or insight will be much appreciated.
ian
(169 rep)
Nov 2, 2022, 05:30 AM
• Last activity: Mar 27, 2024, 04:46 PM
1
votes
1
answers
1638
views
What is the difference between `set -o emacs` vs `bindkey -e`?
From what I have seen, both `set -o emacs` and `bindkey -e` are used to modify the behavior of your terminal line editor to emulate `emacs` whereas `set -o vi` and `bindkey -v` are used to emulate `vi` keybindings. Since both `set -o` and `bindkey` seem to do the same thing, I was wondering what the...
From what I have seen, both
set -o emacs
and bindkey -e
are used to modify the behavior of your terminal line editor to emulate emacs
whereas set -o vi
and bindkey -v
are used to emulate vi
keybindings. Since both set -o
and bindkey
seem to do the same thing, I was wondering what the differences between the two were and if there were any differences at all.
It seems like set -o
works for most shells whereas bindkey
is a zle
specific command. Is this the only difference? Also, do they bind the same keys or are there some keys where set -o
and bindkey
bind to different operations?
grenmester
(195 rep)
Apr 9, 2020, 07:23 AM
• Last activity: Sep 16, 2023, 08:11 AM
1
votes
1
answers
335
views
What do these strings, '\M^?' and '^\M?', represent in zsh/ZLE?
In the [documentation for the Zsh Line Editor](https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html), there is a section that says: 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...
In the [documentation for the Zsh Line Editor](https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html) , there is a section that says:
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...
How should those last two sequences be interpreted? My guess is:
\M^? - delete with the meta bit set?
^\M? - control + question mark with the meta bit set
Is this correct?
xdhmoore
(145 rep)
Sep 4, 2023, 09:49 PM
• Last activity: Sep 10, 2023, 04:38 PM
6
votes
2
answers
1359
views
How to bind a keyboard shortcut in zsh to a program requiring stdin?
This is a follow-up to [this question](https://unix.stackexchange.com/q/373795/63722): I'm trying to create a keyboard shortcut for the terminal file manager [ranger](https://github.com/ranger/ranger), in order to jump from the zsh prompt into the file manager with a single keystroke. I was followin...
This is a follow-up to [this question](https://unix.stackexchange.com/q/373795/63722) : I'm trying to create a keyboard shortcut for the terminal file manager [ranger](https://github.com/ranger/ranger) , 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
.zshrc
:
run_ranger () { echo; ranger; zle redisplay }
zle -N run_ranger
bindkey '^f' run_ranger
The key binding itself works, however ranger fails to start with Error: Must run ranger from terminal
. I had a look at the [ranger code (Python)](https://github.com/ranger/ranger/blob/325f41e6a841ab1413020b9d2d86c3922d90f407/ranger/core/main.py#L137) and it performs the common check sys.stdin.isatty()
to verify that is has a TTY stdin. How can I modify the zle widget so that stdin is properly set?
bluenote10
(301 rep)
Oct 13, 2018, 09:02 PM
• Last activity: Aug 5, 2023, 10:10 AM
6
votes
2
answers
1878
views
zsh history-beginning-search-backwards doesn't position cursor as desired
In bash, I used this: ``` % cat .inputrc "\e[A": history-search-backward "\e[B": history-search-forward ``` I'd like the same behavior in .zsh, so I've tried these bindkeys: ``` "^[[A" history-beginning-search-backward "^[[B" history-beginning-search-forward ``` These are ALMOST what I want, except...
In bash, I used this:
% cat .inputrc
"\e[A": history-search-backward
"\e[B": history-search-forward
I'd like the same behavior in .zsh, so I've tried these bindkeys:
"^[[A" history-beginning-search-backward
"^[[B" history-beginning-search-forward
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.
'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 history-search-backwards
and zsh's history-beginning-search-backward
keep the cursor where I stopped typ
is it possible to get the cursor positioned at the end of the line with the same functionality as history-beginning-search-backward
?
mayostard
(61 rep)
Feb 16, 2022, 12:42 PM
• Last activity: May 2, 2023, 11:11 AM
3
votes
1
answers
366
views
Make one normal mode command while in insert mode in zsh's vi mode
Zsh vi mode doesn't have ctrl-o behavior set by default how do I get to work like in vim ?
Zsh vi mode doesn't have ctrl-o behavior set by default how do I get to work like in vim ?
Hazem Elmahy
(33 rep)
Jan 10, 2021, 01:36 AM
• Last activity: Apr 26, 2023, 11:08 AM
6
votes
5
answers
13639
views
Move to the previous line of a multi-line command
When editing multi-line commands, with escaped newlines, I cannot move up lines. For example, suppose I enter `echo \` one one line, then I press Enter , and then I want to edit the `echo \` part of the command. Pressing Up doesn't move back to the first command line. This works for long commands wh...
When editing multi-line commands, with escaped newlines, I cannot move up lines.
For example, suppose I enter
echo \
one one line, then I press Enter, and then I want to edit the echo \
part of the command. Pressing Up doesn't move back to the first command line.
This works for long commands which wrap, but not with escaped newlines:
_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
Leandros
(722 rep)
Jan 30, 2016, 09:39 PM
• Last activity: Mar 24, 2023, 01:31 AM
0
votes
0
answers
204
views
Zsh: make single file selection smart, stylish, and functionally correct (highlights, completions below cursor, etc)
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 beginni...
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:
1. Highlight: current menu selection item should be highlighted with reverse-video.
2. Order: the possible file completions should be shown below the prompt.
3. Prompt stepping: Prompt shouldn't step below after printing possible options above. This is related to 2.
4. 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")
5. 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.
6. 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.
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.
Create a file `
This is close ([zim](https://github.com/zimfw/zimfw) 's file completion):
foofiles.zsh
` in a temp directory, chmod and run it:
#!/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/
This is how it looks like:


codepoet
(626 rep)
Feb 10, 2023, 07:20 AM
• Last activity: Feb 10, 2023, 07:36 AM
0
votes
1
answers
73
views
How to change meta to option (macOS) in zle emacs keymap?
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?
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?
buduboti
(57 rep)
Nov 22, 2022, 05:08 PM
• Last activity: Nov 23, 2022, 11:14 AM
2
votes
0
answers
63
views
how to change prompt of zle widgets (history-incremental-backward-search)?
Is there a way to change the prompt of zle widgets such as history-incremental-backward-search? For instance I wonder if we can colorize "bck-i-search:" or change the string, or colorize the search entries... Thanks
Is there a way to change the prompt of zle widgets such as history-incremental-backward-search?
For instance I wonder if we can colorize "bck-i-search:" or change the string, or colorize the search entries...
Thanks
jds
(21 rep)
Nov 12, 2022, 06:08 PM
2
votes
3
answers
1855
views
zsh keybindings: make backward-kill-word act on whole quoted string
When using Ctrl+w or Alt+Backspace or Alt+d keyboard shortcuts to delete words backward/forward, I know I can define the WORDCHARS characters: WORDCHARS='~!#$%^&*(){}[] ?.+;-' for example, using Ctrl+w from the end of this string, will successively delete each word echo aaa bbb ccc ddd eee but if I...
When using Ctrl+w or Alt+Backspace or Alt+d keyboard shortcuts to delete words backward/forward, I know I can define the WORDCHARS characters:
WORDCHARS='~!#$%^&*(){}[]?.+;-'
for example, using Ctrl+w from the end of this string, will successively delete each word
echo aaa bbb ccc ddd eee
but if I have quoted strings, how can I treat the whole quoted block as one entity?
echo 'aaa bbb ccc' 'ddd eee fff'
so, Ctrl+w should delete whole 'ddd eee fff' in one go.
Same behavior should be for strings quoted with double quotes
"
Martin Vegter
(586 rep)
Aug 20, 2021, 10:12 AM
• Last activity: Oct 22, 2022, 04:06 PM
3
votes
1
answers
318
views
Fuzzy Search History: Show History Keeping The Lines Intact For Multi-Line Commands
I am working on a zsh widget to fuzzy search history. 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 In `...
I am working on a zsh widget to fuzzy search history.
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
In
like:
So, it replace newline with
what changes do I need to make on my widget? How can I solve this problem?
history -1 1 | sd '^[ ]*[0-9]*[ ]*' '' | sk --tac --no-sort --exact
, sd is used instead of sed, which just removes numbers from the beginning. sk is an alternative for fzf.
This widget works pretty well except multi-line commands.
For example: it shows


\n
. This is not something i want.
The command looks like the following in .zsh_history
file:
: 1657176206:0;function brcd() {\
cd $(broot --only-folders --hidden --cmd "$1;:pp")\
}\
What I found is !NUM get the history in correct format.

Ahmad Ismail
(2998 rep)
Jul 7, 2022, 07:29 AM
• Last activity: Jul 14, 2022, 04:06 PM
0
votes
1
answers
130
views
How to cancel a zle function after triggering the bindkey?
Hope you're doing well. I have the following in my `.zshrc`: ```sh # 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 "$LAST...
Hope you're doing well.
I have the following in my
.zshrc
:
# 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
As suggested in the inline comment, it's a handy tool such that when I trigger Ctrl-f
, it'll prompt me a fzf menu, and upon selection it'll open ranger in that dir.
The problem is that, when I want to cancel selection by pressing Esc
, it'll still open up ranger and send me the home dir, which is not what I want.
So my question is, how do I cancel such a zle function after triggering the bindkey?
Config:
zsh 5.9 (x86_64-pc-linux-gnu)
OS: Manjaro Linux
Any input is much appreciated. Thank you:)
kohane15
(254 rep)
Jul 5, 2022, 05:21 AM
• Last activity: Jul 7, 2022, 02:52 PM
Showing page 1 of 20 total questions