How can I configure Bash, Zsh or Tmux to complete the last matching identifier on the screen? Consider this common scenario:
$ git fetch
remote: Counting objects: 16, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 9 (delta 4), reused 0 (delta 0)
Unpacking objects: 100% (9/9), done.
From /opt/git/thunder
* [new branch] issue540 -> origin/issue547314
e9204cf..4d42c3f v2.1 -> origin/v2.1
How can I get Bash / Zsh/ Tmux to complete issue547
on the CLI easily? When I press Tab after $ git checkout is
Git helpfully completes to issue
, but I must complete the digits by myself because all the previous digits do in fact match existent Git branches.
In VIM, pressing Ctrl+P for omnicomplete will complete as the previous match, so in this case issue547314
will in fact be completed. How can I get this behaviour in Bash, in Zsh or in Tmux?
I'm currently using Bash 4.2 and Tmux 1.10 on Ubuntu Server (usually 12.04 LTS). I can update to the latest Ubuntu Server LTS (14.04) if needed.
EDIT: I would not mind any solution that uses Bash, Zsh, or Tmux as long as it is not difficult to use. So Tab or Ctrl+P or some other such shortcut would be fine, but not Alt+Meta+Super+Shift+א.
Best Answer
I think that feature that OP is looking for is called dabbrev-expand in Emacs world:
xterm
also hasdabbrev-expand
feature but it's a bit less smart than Emacs counterpart but it's very useful to me and one was one of the reasons for which I switched toxterm
. Insidexterm
window one can use a custom keybinding specified in~/.Xresources
to invokedabbrev-expand
on a given string. For example, I have the following entry in my~/.Xresources
(I useuxterm
, an Unicode version of xterm):Inside
xterm
window I can useM-/
(ALT + /
) to invokedabbrev-expand
.xterm
will look for all strings visible on the screen that start with letters I typed. Example:If I pressed
M-/
nowxterm
would expanda_v
toa_very_long_string
. Unfortunately, as I saidxterm
is not so smart and itsdabbrev-expand
feature will only work on full strings. So, in your caseis
would be expanded toissue540
and notissue547314
becauseissue547314
is a part oforigin/issue547314
(think about it as\b
in regular expressions, it's a bit similar although most regular expressions engines would catch both occurrences ofissue
strings in\bissue.+\b
). But, you can typeor
and then presM-/
.xterm
will first expandor
toorigin/v2.1
, this is not what we want so pressM-/
again andxterm
will expand it toorigin/issue547314
. Now, if you use Bash you can doM-b
,C-w
andC-e
to removeorigin/
part. To sum up,dabbrev-expand
insidexterm
is not as good as in Emacs (and Vim I guess) but it's still faster than rewriting long strings by hand and less typo-prone. And in most cases it will expand directly to the desired string without need to remove redundant parts. You just need to get used to it - look at string you want to have at cursor and see if it's not preceded by something else, and if it is type a preceding part and remove it after expansion.Note that
xterm
is not compiled withdabbrev-expand
feature by default and you have to enable it explicitly. However, version ofxterm
in Ubuntu repositories is compiled withdabbrev-expand
and you can use it right away.