How to italicize prompt for zsh (oh-my-zsh, iTerm2)? - zsh

I want to italicize my prompt (specifically the right prompt/RPROMPT) for zsh using oh-my-zsh, in iTerm2, and so far have had problems doing so. I have checked that the terminal can output and view italicized fonts with echo -e "\e[3mitalic\e[0m".
Things I have tried so far :
RPROMPT = '\e[3m Hello \e[0m' : the output is a literal quote \e[3m Hello \e[0m
from here, I tried
HELLO = Hello
RPROMPT = '\e[3m $Hello \e[0m'`
and the output still has the \e[3m and \e[0m parts
from an example, I tried using \x1b[3m rather than \e[3m : still outputs \x1b[3m and \x1b[0m
I found this page but I don't understand what I'm looking at/what I'm supposed to do.
I would like to get the italic format working, so any help is greatly appreciated.

You can use RPROMPT=$'string with special escaping' (See bash manual)
It may have a few issues such as the left prompt getting edited.

After further testing based on GrapeApple's answer, I found a solution that does not mess with the left prompt. Per zsh documentation, you need to use a string quote %{ ... %} to wrap around the escape sequence formatting, e.g. :
RPROMPT=$'%{\x1b[34;1;3m%} hello %{\x1b[0m%}'

For better compatibility (in case you run your zsh one day with a different kind of TERM), I would not hard-code the escape sequences, but use something like
RPROMPT="$(tput sitm) Hello $(tput sgr0)"
See man terminfo for the meaning of sitm and sgr0.

Related

How can I determine why this `test` does not match for tmux's session_name?

I'm trying to give my tmux panes individual titles. Since there is nothing built into tmux to assign titles, I'm using a function that will receive various properties of the pane and then lookup the title that I want based on those properties, and echo it out.
However, the test inside the function is not working as expected. Even when the session_name "portal" is passed in, it does not match the string "portal", even though the output is always exactly "portal".
I've removed all irrelevant code from the function to show just exactly the failing match:
tmux_pane_title() {
local session_name=$1
# ...
if [[ "$session_name" = "portal" ]] && echo ".${session_name}." || echo "-${session_name}-"
# ...
}
tmux set pane-border-format "#P: `tmux_pane_title \"#{session_name}\" \"#{pane_current_command}\" \"#{pane_current_path}\"` "
It always echos out "-portal-", showing the $1 is in fact "portal", but it does not match "portal" in the test.
I have tried using sed to remove newlines, but it made no difference.
However if I hard-code "portal" into the tmux format for pane-border-format it will suddenly work, suggesting there's some weird control character hidden in the name preventing it from working when I pass in session_name
tmux set pane-border-format "#P: `tmux_pane_title \"portal\" \"#{pane_current_command}\" \"#{pane_current_path}\"` "
If that's the case, how can I find and eliminate the control character? (And why would it be there? I did not enter anything weird into my tmuxinator.yml file for the session name.)
I've already tried removing control characters like this:
local session_name=$(echo $1 | tr -d "[:cntrl:]")
If that's not the case, how can I figure out what is breaking this function?
P.S. I'm on tmux 3.1b.
This is NOT an answer to the question, but it is a step in the direction of a solution to the problem that led me to ask the question. If you have the same problem, this may be helpful.
Although I'm still interested in solving the mystery related to the failing test, I found a way to set my pane titles more easily1.
There is a pane_title property that can be used in your pane-border-format:
tmux set pane-border-format "#{pane_title}"
If you set this format, then you can set the title with printf and escape sequences:
printf '\033]2;%s\033\\' 'your desired title'
(I had read about the printf technique elsewhere, both on and off SO, but it fails if you don't have the proper format including pane_title. No where else did I see these two things mentioned together. Without the combination, it fails. Assuming that the default format is set is not a safe assumption.)
A complete answer is still useful, so I could do things like this:
tmux set pane-border-format " #P: #{?pane_title,#{pane_title},`tmux_pane_title \"#{session_name}\" \"#{pane_current_command}\" \"#{pane_current_path}\"`} "
That would choose an explicit title if one were set, and default to the function to choose one. So please don't close this question as a duplicate of others that relate to setting tmux pane titles. IT IS DIFFERENT.
1In other words, I solved the problem but I didn't answer the question. Many people think SO is a problem-solving site, but it describes itself as a question & answer site. I doubt if many people have given this distinction much thought, but they are very different things.

ZSH avoid adding empty commands to history?

In zsh (with oh-my-zsh, is that matters) when I enter empty commands (e.g. just press enter) I see empty lines added to my ~/.zsh_history:
: 1508496422:0;ls
: 1508496422:0;vim
: 1508496482:0;
: 1508496482:0;
: 1508496482:0;
: 1508496482:0;
: 1508496490:0;
: 1508496490:0;
: 1508496490:0;
: 1508496490:0;
: 1508496494:0;ls
I'm wondering if it's possible to avoid adding these lines. I checked http://zsh.sourceforge.net/Doc/Release/Options.html but no luck. The reason why I'm trying to avoid adding empty lines is I'm using fzf and fzf lists these empty commands when I search in last commands in a directory.
If this is not possible in zsh side I'll try to search for a solution in fzf side.
There are a few Zsh settings to control what goes into your history
(though I'm surprised emtpies end up there; I can't reproduce that
despite also using fzf and hitting blank RETs a lot).
The man page for zshoptions(1) describes:
HIST_IGNORE_[ALL_]DUPS — This should at least reduce your
consecutive multiple empties down to one.
HIST_IGNORE_SPACE — Your empties might be treated as whitespace
and thus be eliminated. I like this feature anyway for intentionall
discarding commands by starting them with a space.
There is also the HISTORY_IGNORE option (not to be confused with
Bash's HISTIGNORE) — described in zshparam(1) with an example —
which lets you remove a set of patterns. An empty pattern may fix
your case. It also has a zshaddhistory hook that you could use to
more finely control exactly what goes into history.

buggy ANSI escape sequences in R prompt

When R is run interactively in a terminal which supports colors, it is possible to use ANSI escape sequences in order to put colors in the prompt, such as
options(prompt = "\033[0;31mThis is red\033[0m> ")
Unfortunately, something goes wrong because for long command lines, the line continuation override the prompt instead of being written in the next line.
The problem gets worse when using several colors, because somehow each escape sequence "takes up some space" in the command line, up to the point that the end of the prompt might overwrite the beginning. On my configuration this happens with for instance
options(prompt = paste("\033[0;31m With \033[0;32m multiple",
"\033[0;33m colors \033[0;34m this",
"\033[0;35m gets \033[0;36m really",
"\033[0;37m wrong! \033[0m"))
Why is it so? Is there a workaround?
PS: This rather old post seems related http://r.789695.n4.nabble.com/Xterm-escape-sequences-in-Prompt-td906375.html
update: with R version 3.6.0 and readline 8.0 (don't know which matters here), most of the above described problem disappeared, but some strange behaviors remain. Accepted answer below resolves everything.
you need to surround each «invisible» color code with special «marks»: \001 and \002:
options(prompt = "\001\033[0;31m\002This is red\001\033[0m\002> ")
for explanation see $ info readline (or this short answer).
Gábor Csárdi on the r-devel mailing list says that I cannot easily change this behavior (http://r.789695.n4.nabble.com/buggy-ANSI-escape-sequences-in-R-prompt-td4728671.html). The workaround he proposes is to use a two lines prompt, which suits me well enough.

Zsh + tmux + oh-my-zsh: Autocomplete produces remnant characters

I recently installed Zsh in hope of a better life and brighter mornings. However, I quickly realized Zsh introduces various issues in conjunction with tmux.
The first issue was some weird stuff happening at the end of the prompt, before my commands, but this was resolved by supplying tmux with the -u flag for unicode-support. However, I am stuck with one final issue that needs resolution before I can use Zsh with tmux:
Usecase: Autocomplete a command which contains multiple suggestions
Issue: Autocompletion shifts suggestion one character to the right, while leaving the original character behind (visual bug, it is not included in the command)
Example 1.
Then I hit TAB..
Example 2.
Then I hit TAB..
Note 1: This does NOT occur when using the Bash-shell.
Note 2: I am using "oh-my-zsh". This issue only occurs when using the provided themes. This narrows it down to an "oh-my-zsh"-theme issue, not native zsh/tmux.
In case some people still get a similar issue, see also the top-voted answer of Remnant characters when tab completing with ZSH. The plugin you were using may have had the same non-printable characters issue, that would explain the symptoms.

zsh autocorrect, edit option. Possible to bring cursor to the wrong command?

This is only a tiny, tiny gripe I just experienced right now.
zsh will try to autocorrect car someTextFile.txt. Unfortunately zsh suggests xar someTextFile.txt when I really want cat.
When I choose the edit option (eat the [nyae] prompt), zsh presents me with this:
car someTextFile.txt| (the |is the cursor position). What I would really like is for zsh to place the cursor at the misspelled command. Like this: car| someTextFile.txt – is that possible?
Just a small annoyance :-)
I don't know how to accomplish what you desire. However, for commands that you commonly mistype you can add an alias to your .zshrc:
alias car='cat'
That way, when you mistype, 'car' will be run 'cat'.

Resources