I'm writing a simple ZLE widget to quickly create subshells with <C-j>. Here's what I have:
function zle_subshell {
zle -U '$()'
zle .backward-char
}
# register as widget
zle -N zle_subshell
# create kbd
bindkey '^j' zle_subshell
However, it appears that zle .backward-char isn't working. What makes matters more confusing is that if I modify the script to be:
function zle_subshell {
zle -U '$('
zle -U ')'
zle .backward-char
}
I get output like )$(...
It appears that zle_subshell function is being evaluated in reverse. Are there some obvious gotchas with ZLE widgets that I'm unaware of?
The zle -U usage is the pecial case. It seems that the behavior is intended:
zle -U string
...
This pushes the characters in the string onto the input stack of ZLE. After the widget currently executed finishes ZLE will behave as if the characters in the string were typed by the user.
As ZLE uses a stack, if this option is used repeatedly the last string pushed onto the stack will be processed first. However, the characters in each string will be processed in the order in which they appear in the string.
-- zshzle(1), ZLE BUILTINS, zle -U
So, zsh will behave as if ) and $( were typed after the zle_subshell finishes.
We could modify the (R)BUFFER to change the editor buffer directly like this:
function zle_subshell {
RBUFFER='$()'"$RBUFFER"
repeat 2 do zle .forward-char; done
# ((CURSOR=CURSOR+2)) # We could do this instead.
}
Related
I have a key binding to go up by one directory (very useful):
# C-M-u: up-directory
up-directory() {
builtin cd .. && zle reset-prompt
}
zle -N up-directory
bindkey '\e\C-u' up-directory
It works well, except that the prompt is not really reset.
Example, starting in a Git repo (~/.dotfiles):
After C-M-u, I get:
So, I'm well one level up (into ~), but the Git info is still there, while not valid anymore -- I'm not in a Git repo anymore…
How to fix this?
You probably need to execute the precmds before resetting the prompt.
fzf's zsh integration does this:
# Ensure `precmds` are run after `cd`
fzf-redraw-prompt() {
local precmd
for precmd in $precmd_functions; do
$precmd
done
zle reset-prompt
}
So, try something like this:
up-directory() {
builtin cd ..
if (( $? == 0 )); then
local precmd
for precmd in $precmd_functions; do
$precmd
done
zle reset-prompt
fi
}
I reckon there's already a widget for the g~ action in zle. So g~iw will invert the case of a word.
I read the zshzle manual and did not find a widget that would give me the behaviour of gU (capitalize action) in vim.
for example: for the word "path_variable", with the cursor on the v, gUiW would change the world to "PATH_VARIABLE", and so on and so forth.
the widget capitalize-word does not seem to be the answer. I've tested it.
I also found that the key U in visual mode does not capitalize the visually selected text/region. I did not find a widget in the manual that would give me the desired behaviour either.
Is this a matter of writing a custom widget, or would one have to submit a patch upstream with c code changes? How can I bind gU in normal and U in visual mode to achieve the desired behaviour in zle vi-mode?
ZSH 5.3 will have pre built widgets for that. But if you can't wait, here it is:
# credits go to Oliver Kiddle <opk#zsh.org>,
# who personally shared these upper/lower widgets.
# I just corrected a small bug.
vi-lowercase() {
local save_cut="$CUTBUFFER"
local save_cur="$CURSOR"
zle .vi-change || return
zle .vi-cmd-mode
CUTBUFFER="${CUTBUFFER:l}"
if [[ $CURSOR = '0' ]]; then
zle .vi-put-before -n 1
else
zle .vi-put-after -n 1
fi
CUTBUFFER="$save_cut"
CURSOR="$save_cur"
}
vi-uppercase() {
local save_cut="$CUTBUFFER"
local save_cur="$CURSOR"
zle .vi-change || return
zle .vi-cmd-mode
CUTBUFFER="${CUTBUFFER:u}"
if [[ $CURSOR = '0' ]]; then
zle .vi-put-before -n 1
else
zle .vi-put-after -n 1
fi
CUTBUFFER="$save_cut"
CURSOR="$save_cur"
}
# can safely disable this after commit zsh commit #a73ae70 (zsh-5.2-301- ga73ae70)
zle -N vi-lowercase
zle -N vi-uppercase
bindkey -a 'gU' vi-uppercase
bindkey -a 'gu' vi-lowercase
bindkey -M visual 'u' vi-lowercase
bindkey -M visual 'U' vi-uppercase
I have this snippet:
insert_sudo () { zle beginning-of-line; zle -U "sudo "; zle end-of-line; }
zle -N insert-sudo insert_sudo
bindkey "\es" insert-sudo
But \es only appends "sudo" to the end of a line, not the beginning of line as I expected. Please help!
Try the following:
insert_sudo() { BUFFER="sudo $BUFFER"; zle end-of-line; }
This directly modifies the special $BUFFER variable containing the contents of the command line by prepending sudo and then placing the cursor at the end of the line.
The problem with zle -U is that it only takes effect AFTER the widget finishes. From the zsh manual:
After the widget currently executed finishes, ZLE will behave as if the
characters in the string were typed by the user.
I have a zsh prompt I rather like: it evaluates the current time in precmd and displays that on the right side of the prompt:
[Floatie:~] ^_^
cbowns% [9:28:31 on 2012-10-29]
However, this isn't exactly what I want: as you can see below, this time is actually the time the previous command exited, not the time the command was started:
[Floatie:~] ^_^
cbowns% date [9:28:26 on 2012-10-29]
Mon Oct 29 09:28:31 PDT 2012
[Floatie:~] ^_^
cbowns% date [9:28:31 on 2012-10-29]
Mon Oct 29 09:28:37 PDT 2012
[Floatie:~] ^_^
cbowns% [9:28:37 on 2012-10-29]
Is there a hook in zsh to run a command just before the shell starts a new command so I can update the prompt timestamp then? (I saw Constantly updated clock in zsh prompt?, but I don't need it constantly updated, just updated when I hit enter.)
(The ^_^ is based on the previous command's return code. It shows ;_; in red when there's a nonzero exit status.)
This is in fact possible without resorting to strange hacks. I've got this in my .zshrc
RPROMPT='[%D{%L:%M:%S %p}]'
TMOUT=1
TRAPALRM() {
zle reset-prompt
}
The TRAPALRM function gets called every TMOUT seconds (in this case 1), and here it performs a prompt refresh, and does so until a command starts execution (and it doesn't interfere with anything you type on the prompt before hitting enter). I know you don't need it constantly refreshed but it still gets the job done without needing a line for itself!
Source: http://www.zsh.org/mla/users/2007/msg00944.html (It's from 2007!)
I had a struggle to make this:
It displays the date on the right side when the command has been executed.
It does not overwrite the command shown.
Warning: it may overwrite the current RPROMPT.
strlen () {
FOO=$1
local zero='%([BSUbfksu]|([FB]|){*})'
LEN=${#${(S%%)FOO//$~zero/}}
echo $LEN
}
# show right prompt with date ONLY when command is executed
preexec () {
DATE=$( date +"[%H:%M:%S]" )
local len_right=$( strlen "$DATE" )
len_right=$(( $len_right+1 ))
local right_start=$(($COLUMNS - $len_right))
local len_cmd=$( strlen "$#" )
local len_prompt=$(strlen "$PROMPT" )
local len_left=$(($len_cmd+$len_prompt))
RDATE="\033[${right_start}C ${DATE}"
if [ $len_left -lt $right_start ]; then
# command does not overwrite right prompt
# ok to move up one line
echo -e "\033[1A${RDATE}"
else
echo -e "${RDATE}"
fi
}
Sources:
http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
https://stackoverflow.com/a/10564427/238913
You can remap the Return key to reset the prompt before accepting the line:
reset-prompt-and-accept-line() {
zle reset-prompt
zle accept-line
}
zle -N reset-prompt-and-accept-line
bindkey '^m' reset-prompt-and-accept-line
zsh will run the preexec function just before executing a line. It would be simple to have that output the current time, a simple version would be just:
preexec() { date }
Modifying an existing prompt would be much more challenging.
Building off #vitaŭt-bajaryn's cool ZSH style answer:
I think overriding the accept-line function is probably the most idiomatic zsh solution:
function _reset-prompt-and-accept-line {
zle reset-prompt
zle .accept-line # Note the . meaning the built-in accept-line.
}
zle -N accept-line _reset-prompt-and-accept-line
You can use ANSI escape sequences to write over the previous line, like this:
preexec () {
DATE=`date +"%H:%M:%S on %Y-%m-%d"`
C=$(($COLUMNS-24))
echo -e "\033[1A\033[${C}C ${DATE} "
}
Is it possible to configure zsh to expand global aliases during tab completion? For example, I have the common aliases:
alias -g '...'='../..'
alias -g '....'='../../..'
but when I type, for example, cd .../some<tab> it won't expand to cd .../something or cd ../../something. Consequently, I frequently won't use these handy aliases because they are incompatible with tab completion.
I'm a user of Mikael Magnusson's rationalise-dot. From my zshrc:
# This was written entirely by Mikael Magnusson (Mikachu)
# Basically type '...' to get '../..' with successive .'s adding /..
function rationalise-dot {
local MATCH # keep the regex match from leaking to the environment
if [[ $LBUFFER =~ '(^|/| | |'$'\n''|\||;|&)\.\.$' ]]; then
LBUFFER+=/
zle self-insert
zle self-insert
else
zle self-insert
fi
}
zle -N rationalise-dot
bindkey . rationalise-dot
# without this, typing a . aborts incremental history search
bindkey -M isearch . self-insert
Try looking up zsh abbreviations. They allow you to enter an "abbreviation" which automatically gets replaced with its full form when you hit a magic key such as space. So you can create one which changes ...<SPACE> to ../...
For example, this is what you need in your profile:
typeset -A abbrevs
abbrevs=(
"..." "../.."
"...." "../../.."
)
#create aliases for the abbrevs too
for abbr in ${(k)abbrevs}; do
alias -g $abbr="${abbrevs[$abbr]}"
done
my-expand-abbrev() {
local MATCH
LBUFFER=${LBUFFER%%(#m)[_a-zA-Z0-9]#}
LBUFFER+=${abbrevs[$MATCH]:-$MATCH}
zle self-insert
}
bindkey " " my-expand-abbrev