Never complete beyond ambiguous choices - zsh

I'm trying to figure out how to customize zsh completion so that it never completes beyond an ambiguous result by just hitting tab. Here's an example.
$ emacs f<TAB>
food fool
(completes into)
$ emacs foo
food fool
Importantly, if I press TAB after 'emacs foo' I would like zsh to beep instead of complete into the menu.
Thanks.

Descending into the completion menu is controlled via the AUTO_MENU option. This is a default option which can be turned off by setting in your .zshrc the 'no' prefixed option.
setopt noautomenu
ZSH Documentation: 16 Options

Related

zsh <tab> <tab> autocompletion only if there is unique match

I recently switched from bash to zsh and I am trying to figure out how to change the double tab autocomplete behavior. I'd like it to autocomplete only if there is a unique match.
This can be controlled by shell options. You can try it in interactive shell first, then add your preferred options to ~/.zshrc.
To make zsh auto complete behave more like bash, you can start with this:
setopt NO_AUTOLIST BASH_AUTOLIST NO_MENUCOMPLETE
Also check official zsh documentation, section 6 and especially subsection 6.2. Currently it can be found here: https://zsh.sourceforge.io/Guide/zshguide06.html#l147

How can I get backward-delete-word to add to the current kill-ring in zsh?

I use zsh and I would like backward-kill-word in Emacs mode to behave like Emacs (and bash, fwiw). The behaviour that I have failed to reproduce is that when I press multiple backward-kill-word Emacs adds the killed text to the cut buffer (the first item in the killring) making it possible for me to yank everything with one yank command.
How can I configure zsh to behave like this aspect of Emacs editors?
Actually, by default, Zsh's cut buffer works exactly the same as in Emacs. Just use zsh -f to start Zsh without config files and try it.
However, are you perhaps using zsh-autosuggestions or zsh-syntax-highlighting? There are bugs in these plugins that break this feature:
https://github.com/zsh-users/zsh-autosuggestions/issues/363
https://github.com/zsh-users/zsh-syntax-highlighting/issues/150#issuecomment-658381485
Fixes have been submitted, but for zsh-autosuggestions, none have yet been merged, and for zsh-syntax-highlighting, the fix won't work until Zsh 5.9 has been released.
In the meantime, though, zsh-autocomplete contains a workaround that fixes the problem. If you add that plugin, your cut buffer will start functioning like normal again.

What does terminal / hyperterm display in the tab title? Is it possible to customize this?

I'm confused what data is being shown in my terminal tabs. The first part, epzio is obviously my username. But the second part, #C02S60BBG8WP, I have no idea.
Note: I use zsh combined with oh-my-zsh for my shell.
Is it possible to customize what is shown in the tab titles? I'd like to remove the epzio#C02S60BBG8WP part altogether and just display the current working directory.
Also, why do my tab titles turn blue for no apparent reason? I thought this was only supposed to happen when you had a specific process such as node running. As you can see in the screenshot below, the tic-tac-toe tab has turned blue even though nothing is happening in that tab.
Update: It seems that the solution might involve making changes to ~/.oh-my-zsh/lib/termsupport.zsh, but I'm not sure if these would get overridden when oh-my-zsh updates itself.
C02S60BBG8WP is probably your hostname; check by typing hostname.
You can change the terminal title by printing an escape sequence like this:
echo -en "\033]0;New terminal title\a"
So this should change the title to your current working directory, $PWD, substituted by a single ~ if you're in $HOME:
echo -en "\033]0;${PWD/#$HOME/~}\007"
If that doesn't work, it's probably being overridden immediately afterwards by a command that is automatically invoked by your shell. In bash, this would be PROMPT_COMMAND which on my system looks like this:
$ echo $PROMPT_COMMAND
__vte_prompt_command; printf "\033]0;%s#%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"
The zsh equivalent seems to be to define a precmd hook:
precmd() { echo -en "\033]0;${PWD/#$HOME/~}\007" }
To make that permanent, you can just put it your .zshrc.

Select from zsh completion menu by number

I discovered this little navigation trick the other day, which allows me to trigger menu completions by number when I enter 'cd -'
~ cd -
0 -- ~/home
1 -- ~/home/stuff
2 -- ~/downloads
3 -- ~/wallpaper
Shell scripting syntax still reads like a foreign language to me, but to get this functionality my directory stack history is piped into the function below.
DIRSTACKSIZE=9
DIRSTACKFILE=~/.zdirs
if [[ -f $DIRSTACKFILE ]] && [[ $#dirstack -eq 0 ]];
then dirstack=( ${(f)"$(< $DIRSTACKFILE)"} )
[[ -d $dirstack[1] ]] && cd $dirstack[1] && cd $OLDPWD
fi
chpwd() {
print -l $PWD ${(u)dirstack} >$DIRSTACKFILE
}
The magical part is being able to choose from the list by number, but I have come to learn that this is probably
because the functionality for navigation by number is baked in to the 'cd -' command. Still, I'd like to use this everywhere.
Any tips writing a wrapper function (or something like that, I guess) for the completion menu that pipes in completions from the menu when it is triggered
and displays them in a numbered list where those numbers select the corresponding element?
I've gotten started reading the manual and what not, but everything remains rather opaque. Thanks!
First off, the code snippet you show has nothing to do with completion. Instead, what it does is to record the directory stack to a file in order to preserve it between zsh sessions. (Personally, I'm not even sure this is a good idea.)
A good place to start investigating zsh completions is the _complete_help ZLE widget. This is bound by default to ^Xh in zsh's viins (vi insert) keyboard map, but is unbound by default in the emacs keymap. If you want to use it in the emacs keymap (the default for many people), you have to bind it:
bindkey -M emacs "^Xh" _complete_help
Now you can type cd - (or cd +) and follow it by CTRL-Xh instead of TAB. You should see the following output:
tags in context :completion::complete:cd::
directory-stack (_directory_stack _cd)
(At this point I'll admit we're getting close to the limits of my knowledge of the zsh completion system.)
Now you can see the completer functions for the directory-stack tag in this particular context. The one you're probably interested in is _directory_stack, and you can see the content of that function with:
functions _directory_stack
…which is where those leading numbers are actually generated.
Arguably it's possible to write similar completion functions for other completion contexts, and apply the using zstyle. However, this is non-trivial completion magic, and beyond anything I have attempted

tmux man-page search highlighting

When I search in, for example, man ls while in a tmux session, the search strings don't appear highlighted - the page jumps down so that the search string is on the top line of the buffer, as expected, but it's not highlighted.
Doing the same thing in the same shell while not in a tmux session results in highlighted search strings.
I have no idea where to start looking to solve this. Any hints are appreciated.
Based on Less Colors For Man Pages by Gen2ly, here is my man page and how to do it:
Preview
This is a shell, not a web page !
How to
(optional) I'm using Tomorrow theme for Konsole/Yakuake ;
Edit your ~/.bashrc ~/.zshrc, etc. to add :
# Colored man pages: http://linuxtidbits.wordpress.com/2009/03/23/less-colors-for-man-pages/
# Less Colors for Man Pages
export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
export LESS_TERMCAP_md=$'\E[01;38;5;74m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # end mode
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_so=$'\E[38;5;016m\E[48;5;220m' # begin standout-mode - info box
export LESS_TERMCAP_ue=$'\E[0m' # end underline
export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline
Reload your config and try a man page search :
. ~/.bashrc && man ls
Fixed it. The problem is to do with the way that the screen $TERM handles italics. From the tmux FAQ:
vim displays reverse video instead of italics, while less displays italics
(or just regular text) instead of reverse. What's wrong?
This matches my problem exactly. The $PAGER used by man is less by default - basically, man uses less to show the contents of the manual pages. In my case, less wasn't highlighting text, just showing regular text.
The reason for this happening:
Screen's terminfo description lacks italics mode and has standout mode in its
place, but using the same escape sequence that urxvt uses for italics. This
means applications (like vim) looking for italics will not find it and might
turn to reverse in its place, while applications (like less) asking for
standout will end up with italics instead of reverse.
The solution is to make a new terminfo file for tmux, which lets it know that italics are supported. The solution's outlined in the (at time of writing) very, very bottom of the tmux FAQ.
After creating the new terminfo file, in tmux: C-b :source-file /absolute/path/to/.tmux.conf (from this SuperUser question) - this should make tmux reload the .tmux.conf file. However, this didn't work for me, and the changes only applied after restarting the tmux server (close all tmux sessions, then re-open them).
This thread is a few years old but is still the one that comes up as the best search result, so I'm answering with what finally worked for me. This is based off of tmux FAQ.
...but the instructions aren't completely clear on when or where to substitute the -256color string. I use gnome-terminal (v 3.16.2) with tmux, and this worked for me:
$ mkdir $HOME/.terminfo/
$ screen_terminfo="screen-256color"
$ infocmp "$screen_terminfo" | sed \
-e 's/^screen[^|]*|[^,]*,/screen-256color|screen with italics support,/' \
-e 's/%?%p1%t;3%/%?%p1%t;7%/' \
-e 's/smso=[^,]*,/smso=\\E[7m,/' \
-e 's/rmso=[^,]*,/rmso=\\E[27m,/' \
-e '$s/$/ sitm=\\E[3m, ritm=\\E[23m,/' > /tmp/screen.terminfo
$ tic /tmp/screen.terminfo
And tell tmux to use it in ~/.tmux.conf:
set -g default-terminal "screen-256color"
Note: I tried it once without the -256color and since that didn't work (still seeing italics instead of highlighting), I had to delete everything under the .terminfo dir (another dir called 's') before the infocmp would work.

Resources