I have this function which works great in zsh, but I want to convert it to fish shell and I can't get it working.
function ogf () {
echo "Cloning, your editor will open when clone has completed..."
source <(TARGET_DIRECTORY=~/students EDITOR=$EDITOR clone_git_file -ts "$1")
}
First of all, since fish's syntax differs from zsh, you also have to change the output of clone_git_file to source it.
For example, if clone_git_file is something like:
#!/bin/bash
echo "FOO=$TARGET_DIRECTORY"
echo "BAR=$2"
you have to change it to fish syntax.
#!/bin/bash
echo "set -gx FOO $TARGET_DIRECTORY"
echo "set -gx BAR $2"
Now here's the ogf() function, and sample code for fish:
function ogf
echo "Cloning, your editor will open when clone has completed..."
source (env TARGET_DIRECTORY=~/students EDITOR=$EDITOR clone_git_file -ts $argv[1] | psub)
end
ogf MY_ARGUMENT
echo "FOO is $FOO"
echo "BAR is $BAR"
Running this code with fish, the output is:
FOO is /home/MY_USER/students
BAR is MY_ARGUMENT
Related
When printing all available aliases on stdout via command alias a multiline alias in bash will printed with the line breaks but it does not in zsh.
Example with the identical multiline alias definition for bash .bash_aliases and zsh .zsh_aliases:
alias hello='
echo "Hello"
echo "beautiful"
echo "world"'
When the alias hello is executed the result is the same on both shells.
But when comparing the printout of the definiton via alias hello on stdout...
in bash the output is:
alias hello='
echo "Hello"
echo "beautiful"
echo "world"'
whereas in zsh the output looks like this:
hello=$'\n echo "Hello"\n echo "beautiful"\n echo "world"'
Why is the \n in zsh not printed as new line and the tabulator \t not respected like in bash`s stdout?
I tried several escaping but without success.
You can use the print built-in to display the text with newlines:
print "$(alias)"
e.g.
> print "$(alias hello)"
hello=$'
echo "Hello"
echo "beautiful"
echo "world"'
It's usually preferable to use a function for anything with multiple lines rather than an alias. With a function you have fewer worries about quoting and a generally easier-to-follow syntax:
hello2() {
print Hello
print beautiful
print world
}
$ echo ${(t)prompt}
scalar-special
$ echo ${(t)PROMPT}
scalar-special
Zsh shows that they are special, but does not show that they are tied like:
$ echo ${(t)manpath}
array-tied-special
$ echo ${(t)MANPATH}
scalar-tied-export-special
From the man page:
PROMPT <S> <Z>
Same as PS1 ...
prompt <S> <Z>
Same as PS1.
This means that if you modify one of PROMPT, prompt or PS1, and then look at one of the other two variables, you will see that your modifications are there as well.
PROMPT=foo
echo $PS1 # outputs foo
echo $prompt # outputs foo
I want to configure zsh to append the time each command started next to the line command was executed on. For example:
# before I press ENTER
$ ./script
# after I press enter
$ ./script [15:55:58]
Running script...
I came up with the following config (which also colors the timestamp yellow):
preexec () {
TIME=`date +"[%H:%M:%S] "`
echo -e "$1 %{$fg[yellow]%}\033[1A\033[1C${TIME}$reset_color"
}
But it breaks and prints { and % characters on basic commands such as cat and echo. It also breaks on password prompts (macOS terminal). For example with echo:
$ echo "hello" [15:55:58]
hello"hello" %{%}
How can I fix this config?
Thank you.
You inspired me and based on your script I wrote mine. I have tested this on zsh 5.4.smth.
preexec () {
local TIME=`date +"[%H:%M:%S] "`
local zero='%([BSUbfksu]|([FK]|){*})'
local PROMPTLEN=${#${(S%%)PROMPT//$~zero/}}
echo "\033[1A\033[$(($(echo -n $1 | wc -m)+$PROMPTLEN))C $fg[blue]${TIME}$reset_color"
}
In your ~/.zshrc file, put:
function preexec() {
timer=${timer:-$SECONDS}
}
function precmd() {
if [ $timer ]; then
timer_show=$(($SECONDS - $timer))
export RPROMPT="%F{cyan}${timer_show}s %F{$black%}"
unset timer
fi
}
And that should give you something like this:
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 have a script that has a string in a file name like so:
filename_with_spaces="a file with spaces"
echo test > "$filename_with_spaces"
test_expect_success "test1: filename with spaces" "
run cat \"$filename_with_spaces\"
run grep test \"$filename_with_spaces\"
"
test_expect_success is defined as:
test_expect_success () {
echo "expecting success: $1"
eval "$2"
}
and run is defined as:
#!/bin/zsh
# make nice filename removing special characters, replace space with _
filename=`echo $# | tr ' ' _ | tr -cd 'a-zA-Z0-9_.'`.run
echo "#!/bin/zsh" > $filename
print "$#" >> $filename
chmod +x $filename
./$filename
But when I run the toplevel script test_expect_success... I get cat_a_file_with_spaces.run with:
#!/bin/zsh
cat a file with spaces
The problem is the quotes around a file with spaces in cat_a_file_with_spaces.run is missing. How do you get Z shell to keep the correct quoting?
Thanks
Try
run cat ${(q)filename_with_spaces}
. It is what (q) modifier was written for. Same for run script:
echo -E ${(q)#} >> $filename
. And it is not bash, you don't need to put quotes around variables: unless you specify some option (don't remember which exactly)
command $var
always passes exactly one argument to command no matter what is in $var. To ensure that some zsh option will not alter the behavior, put
emulate -L zsh
at the top of every script.
Note that initial variant (run cat \"$filename_with_spaces\") is not a correct quoting: filename may contain any character except NULL and / used for separating directories. ${(q)} takes care about it.
Update: I would have written test_expect_success function in the following fashion:
function test_expect_success()
{
emulate -L zsh
echo "Expecting success: $1" ; shift
$#
}
Usage:
test_expect_success "Message" run cat $filename_with_spaces