I would like to turn on
set -u
only inside the body of one zsh function. At the end of the function, it should be restored to its previous state.
I reckon that I have to do a
setopt localoptions ????
inside the function, but what comes at the place of the ???? ? From the zsh manpage, I found an explanation for the option UNSET, but this corresponds to set +u, not set -u.
UPDATE:
Based on the answer given by chepner, I also tried:
set -u
foo() {
setopt NO_UNSET LOCAL_OPTIONS
echo $xx
}
bar() {
setopt localoptions nounset
echo $xx
}
But both functions, when invoked, abort with xx: parameter not set
set -u is equivalent to setopt no_unset and set +u is equivalent to setopt unset. You've inverted the meaning.
set -u
foo() {
setopt local_options unset
echo $xx
}
foo # prints an empty line
echo $xx # errors out
Documentation references:
Long option documentation: UNSET is equivalent to +u (not to -u)
Single letter option documentation: -u is equivalent to NO_UNSET (not to UNSET)
The inverse of the UNSET option is NO_UNSET
foo () {
setopt NO_UNSET LOCAL_OPTIONS
...
}
Related
As the document says, the zle variable CURSOR can only be in range [0, $#BUFFER].
Test code (put it into .zshrc, the ^[OP is F1):
testCursor() {
echo "\nOriginal C: $CURSOR"
BUFFER="a"
echo "Change Buffer: $CURSOR"
CURSOR=$((CURSOR+10))
echo "Force edit: $CURSOR"
CURSOR=100
echo "Force assign: $CURSOR"
}
zle -N testCursor
bindkey '^[OP' testCursor
The CURSOR satisfied it's range definition in runtime, how did the zsh-zle implements it?
The CURSOR value is handled in Zsh's source code, which is implemented in the C programming language: https://github.com/zsh-users/zsh/blob/3c93497eb701d8f220bc32d38e1f12bfb534c390/Src/Zle/zle_params.c#L266
There is no way for you to declare a similarly constrained variable in Zsh shell code.
However, you can write a math function for it instead:
# Declare a global integer.
typeset -gi TEST=0
# -H makes these hidden, that is, not listed automatically.
typeset -gHi _TEST_MIN=0 _TEST_MAX=10
# Load `min` and `max` functions.
autoload -Uz zmathfunc && zmathfunc
set_test() {
(( TEST = min(max($1,$_TEST_MIN),$_TEST_MAX) ))
}
get_test() {
return $(( min(max($TEST,$_TEST_MIN),$_TEST_MAX) ))
}
# Declare `set_test` as a math function accepting exactly one numeric argument.
functions -M set_test 1
# Declare `get_test` as a math function accepting exactly zero arguments.
functions -M get_test 0
You can then use these in arithmetic statements, with this syntax:
❯ print $(( get_test() ))
0
❯ (( set_test(100) ))
❯ print $(( get_test() ))
10
But also in other contexts, with this syntax:
❯ set_test -1
❯ get_test; print $?
0
How do you pass arguments to custom zsh functions?
For instance:
function kill_port_proc(port) {
lsof -i tcp:<port interpolated here>| grep LISTEN | awk '{print $2}'
}
I'm seeing so many examples online with ZSH functions, but there barely anything on passing arguments and interpolating them.
When defining a function, you cannot specify required arguments. That's why using both the function keyword and parens () seems useless to me.
To get the passed arguments, use positional parameters.
The positional parameters provide access to the command-line arguments of a shell function, shell script, or the shell itself; [...]
The parameter n, where n is a number, is the nth positional parameter. The parameter $0 is a special case [...]
About the $0 positional parameter:
The name used to invoke the current shell, or as set by the -c command line option upon invocation.
If the FUNCTION_ARGZERO option is set, $0 is set upon entry to a shell function to the name of the function, and upon entry to a sourced script to the name of the script, and reset to its previous value when the function or script returns.
Using your example:
function kill_port_proc {
lsof -i tcp:"$1" | grep LISTEN | awk '{print $2}'
}
Personaly, I like to document the function by, at least, adding the function signature prior to the definition.
Then, I declare local parameters for each arguments and readonly parameters when I want to protect them from unexpected modification.
If the argument is mandatory, I use a special parameter expansion form:
${name?word}
${name:?word}
In the first form, if name is set, or in the second form if name is both set and non-null, then substitute its value;
otherwise, print word and exit from the shell. Interactive shells instead return to the prompt.
If word is omitted, then a standard message is printed.
How I would write your example:
# kill_port_proc <port>
function kill_port_proc {
readonly port=${1:?"The port must be specified."}
lsof -i tcp:"$port" | grep LISTEN | awk '{print $2}'
}
my_function() {
if [ $# -lt 2 ]
then
echo "Usage: $funcstack[1] <first-argument> <second-argument>"
return
fi
echo "First argument: $1"
echo "Second argument: $2"
}
Usage
$ my_function
Usage: my_function <first-argument> <second-argument>
$ my_function foo
Usage: my_function <first-argument> <second-argument>
$ my_function foo bar
First argument: foo
Second argument: bar
I would like my prompt to show a cross (✘) when the previous command fails. I use the following code:
export PROMPT=$'%(?..✘\n)\n› '
This gives me the following output:
› echo Hello
Hello
› asjdfiasdf
zsh: command not found: asjdfiasdf
✘
›
✘
I would like to modify the prompt so that it does not repeat the cross when the prompt is redrawn after Enter (the third case in the example above).
Is it possible?
I think I got it. Let me know if you find a bug...
preexec() {
preexec_called=1
}
precmd() {
if [ "$?" != 0 ] && [ "$preexec_called" = 1 ]
then echo ✘; unset preexec_called; fi
}
PROMPT=$'\n› '
Result:
› ofaoisfsaoifoisafas
zsh: command not found: ofaoisfsaoifoisafas
✘
›
› echo $? # (not overwritten)
127
I do this in my zsh, though with colors rather than unicode characters. It's the same principle.
First, I set up my colors, ensuring that they are only used when they are supported:
case $TERM in
( rxvt* | vt100* | xterm* | linux | dtterm* | screen )
function PSC() { echo -n "%{\e[${*}m%}"; } # insert color-specifying chars
ERR="%(0?,`PSC '0;32'`,`PSC '1;31'`)" # if last cmd!=err, hash=green, else red
;;
( * )
function PSC() { true; } # no color support? no problem!
ERR=
;;
esac
Next, I set up a magic enter function (thanks to this post about an empty command (ignore the question, see how I adapt it here):
function magic-enter() { # from https://superuser.com/a/625663
if [[ -n $BUFFER ]]
then unset Z_EMPTY_CMD # Enter was pressed on an empty line
else Z_EMPTY_CMD=1 # The line was NOT empty when Enter was pressed
fi
zle accept-line # still perform the standard binding for Enter
}
zle -N magic-enter # define magic-enter as a widget
bindkey "^M" magic-enter # Backup: use ^J
Now it's time to interpret capture the command and use its return code to set the prompt color:
setopt prompt_subst # allow variable substitution
function preexec() { # just after cmd has been read, right before execution
Z_LAST_CMD="$1" # since $_ is unreliable in the prompt
#Z_LAST_CMD="${1[(wr)^(*=*|sudo|-*)]}" # avoid sudo prefix & options
Z_LAST_CMD_START="$(print -Pn '%D{%s.%.}')"
Z_LAST_CMD_START="${Z_LAST_CMD_START%.}" # zsh <= 5.1.1 makes %. a literal dot
Z_LAST_CMD_START="${Z_LAST_CMD_START%[%]}" # zsh <= 4.3.11 makes %. literal
}
function precmd() { # just before the prompt is rendered
local Z_LAST_RETVAL=$? # $? only works on the first line here
Z_PROMPT_EPOCH="$(print -Pn '%D{%s.%.}')" # nanoseconds, like date +%s.%N
Z_PROMPT_EPOCH="${Z_PROMPT_EPOCH%.}" # zsh <= 5.1.1 makes %. a literal dot
Z_PROMPT_EPOCH="${Z_PROMPT_EPOCH%[%]}" # zsh <= 4.3.11 makes %. a literal %.
if [ -n "$Z_LAST_CMD_START" ]; then
Z_LAST_CMD_ELAPSED="$(( $Z_PROMPT_EPOCH - $Z_LAST_CMD_START ))"
Z_LAST_CMD_ELAPSED="$(printf %.3f "$Z_LAST_CMD_ELAPSED")s"
else
Z_LAST_CMD_ELAPSED="unknown time"
fi
# full line for error if we JUST got one (not after hitting <enter>)
if [ -z "$Z_EMPTY_CMD" ] && [ $Z_LAST_RETVAL != 0 ]; then
N=$'\n' # set $N to a literal line break
LERR="$N$(PSC '1;0')[$(PSC '1;31')%D{%Y/%m/%d %T}$(PSC '1;0')]"
LERR="$LERR$(PSC '0;0') code $(PSC '1;31')$Z_LAST_RETVAL"
LERR="$LERR$(PSC '0;0') returned by last command"
LERR="$LERR (run in \$Z_LAST_CMD_ELAPSED):$N"
LERR="$LERR$(PSC '1;31')\$Z_LAST_CMD$(PSC '0;0')$N$N"
print -PR "$LERR"
fi
}
Finally, set the prompt:
PROMPT="$(PSC '0;33')[$(PSC '0;32')%n#%m$(PSC '0;33') %~$PR]$ERR%#$(PSC '0;0') "
Here's how it looks:
A more direct answer to the question, adapted from the above:
function magic-enter() { # from https://superuser.com/a/625663
if [[ -n $BUFFER ]]
then unset Z_EMPTY_CMD # Enter was pressed on an empty line
else Z_EMPTY_CMD=1 # The line was NOT empty when Enter was pressed
fi
zle accept-line # still perform the standard binding for Enter
}
zle -N magic-enter # define magic-enter as a widget
bindkey "^M" magic-enter # Backup: use ^J
function precmd() { # just before the prompt is rendered
local Z_LAST_RETVAL=$? # $? only works on the first line here
# full line for error if we JUST got one (not after hitting <enter>)
if [ -z "$Z_EMPTY_CMD" ] && [ $Z_LAST_RETVAL != 0 ]; then
echo '✘'
fi
}
PROMPT=$'\n› '
With screen shot:
Use the prexec and precmd hooks:
The preexec hook is called before any command executes. It isn't called when no command is executed. For example, if you press enter at an empty prompt, or a prompt that is only whitespace, it won't be called. A call to this hook signals that a command has been run.
The precmd hook is called before the prompt will be displayed to collect the next command. Before printing the prompt you can print out the exit status. In here we can check if a command was just executed, and if there's a status code we want to display.
This is very similar to the solution suggested by #sneep, which is also a great solution. It's worth using the hooks though so that if you've got anything else registering for these hooks they can do so too.
# print exit code once after last command output
function track-exec-command() {
zsh_exec_command=1
}
function print-exit-code() {
local -i code=$?
(( code == 0 )) && return
(( zsh_exec_command != 1 )) && return
unset zsh_exec_command
print -rC1 -- ''${(%):-"%F{160}✘ exit status $code%f"}''
}
autoload -Uz add-zsh-hook
add-zsh-hook preexec track-exec-command
add-zsh-hook precmd print-exit-code
Thanks to everyone for their answers. Four years later, I would like to illustrate a variation on sneep's answer for those looking for the error code and an alert without a symbol. This is a minimalist prompt but when an error occurs it displays the error code and > in red following the top level directory.
preexec() {
preexec_called=1
}
precmd() {
if [ "$?" != 0 ] && [ "$preexec_called" = 1 ]; then
unset preexec_called
PROMPT='%B%F{blue}%1~%f%b%F{red} $? > %F{black}'
else
PROMPT='%B%F{blue}%1~%f%b%F{blue} > %F{black}'
fi
}
I'm wrote a function called test_status that I am trying to incorporate in my tmux status bar. To give some background, my tests will output to a file called .guard_result with either success or failure and the test_status function reads from that file and echoes a 💚 if my tests are passing and a ❤️ if they are failing.
The good news is running test_status works just fine, I'm just having trouble getting it to work with tmux. What am I missing here?
# ~/.oh-my-zsh/custom/aliases.zsh
function test_status {
if [ ! -f "./.guard_result" ]; then
echo "?"
return 1
fi
result="$(cat ./.guard_result)"
if [[ $result == *"success"* ]]
then
echo "💚";
elif [[ $result == *"fail"* ]]
then
echo "❤️";
fi
}
This function works... Here is Tmux configuration (which doesn't show result):
# ~/.tmux.conf
set -g status-right "#(test_status) #[fg=colour245]%d %b %Y #[fg=white]:: #[fg=colour245]%l:%M %p"
I know I must be missing something simple... Thanks for your help!
tmux passes shell commands to /bin/sh not zsh. And even if tmux would use zsh, the function would not be available in that context as ~/.zshrc, which loads oh-my-zsh, is only read for interactive shells.
In order to get the the output of test_status into tmux, I would suggest to put the function into a zsh script and call that.
You can either source ~/.oh-my-zsh/custom/aliases.zsh from within the script and then call test_status:
#!/usr/bin/zsh
# ^ make sure this reflects the path to zsh (`type zsh`)
source ~/.oh-my-zsh/custom/aliases.zsh
test_status
Or you can just put the entire function into the script, so as to not clutter alias.zsh:
#!/usr/bin/zsh
function test_status {
if [ ! -f "./.guard_result" ]; then
echo "?"
return 1
fi
result="$(cat ./.guard_result)"
if [[ $result == *"success"* ]]
then
echo "💚";
elif [[ $result == *"fail"* ]]
then
echo "❤️";
fi
}
Safe the script somewhere (e.g. /path/to/test_status.zsh), make it executable (chmod a+x /path/to/test_status.zsh) and call it by path in the tmux configuration.
I'm trying to get customize my zsh prompt and want to evaluation a function with git commands every time my prompt is generated. I'm using setopt PROMPT_SUBST, but it doesn't seem to be working. This is my zshrc:
setopt PROMPT_SUBST
autoload -U colors && colors # Enable colors
# Show Git branch/tag, or name-rev if on detached head
parse_git_branch() {
echo "PARSING GIT BRANCH"
(git symbolic-ref -q HEAD || git name-rev --name-only --no-undefined --always HEAD) 2> /dev/null
}
prompt() {
echo -n "%/"
echo "$(git status)"
}
PS1="$(prompt)"
And this is my output of setopt:
interactive
login
monitor
nonomatch
promptsubst
shinstdin
zle
You need to delay calling prompt until the prompt is displayed; do that by using single quotes:
PS1='$(prompt)'
A better idea, though, is to define a function that sets PS1, then add that function to the precmd_functions array so that it is executed prior to displaying each prompt.
prompt () {
PS1="%/$(git status)"
}
precmd_functions+=(prompt)