Completion of words on the screen as in VIM (Bash or Tmux)

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+א.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I think that feature that OP is looking for is called dabbrev-expand in Emacs world:

Expand the word in the buffer before point as a dynamic abbrev, by
searching in the buffer for words starting with that abbreviation
(v-expand).

xterm also has dabbrev-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 to xterm. Inside xterm window one can use a custom keybinding specified in ~/.Xresources to invoke dabbrev-expand on a given string. For example, I have the following entry in my ~/.Xresources (I use uxterm, an Unicode version of xterm):

UXTerm*VT100.Translations: #override n
     Meta <Key>/:dabbrev-expand() n

Inside xterm window I can use M-/ (ALT + /) to invoke dabbrev-expand. xterm will look for all strings visible on the screen that start with letters I typed. Example:

$ echo a_very_long_string bye by
$ a_v

If I pressed M-/ now xterm would expand a_v to a_very_long_string. Unfortunately, as I said xterm is not so smart and its dabbrev-expand feature will only work on full strings. So, in your case is would be expanded to issue540 and not issue547314 because issue547314 is a part of origin/issue547314 (think about it as b in regular expressions, it’s a bit similar although most regular expressions engines would catch both occurrences of issue strings in bissue.+b). But, you can type or and then pres M-/. xterm will first expand or to origin/v2.1, this is not what we want so press M-/ again and xterm will expand it to origin/issue547314. Now, if you use Bash you can do M-b, C-w and C-e to remove origin/ part. To sum up, dabbrev-expand inside xterm 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 with dabbrev-expand feature by default and you have to enable it explicitly. However, version of xterm in Ubuntu repositories is compiled with dabbrev-expand and you can use it right away.

Method 2

Bash as a similar facility called bash completetion. You can make use of it by hitting the Tab key when providing arguments to commands when you’re typing them.

Example

If I start by typing ls and then hitting Tab twice you’ll get Bash to provide a list of matching files (and command line switches) to whatever command you’re attempting to use:

$ ls <---- hit Tab x 2
Display all 232 possibilities? (y or n)

And here’s the example with switches:

$ ls -- <---- hit Tab x 2
--all                 --format=                                  --quote-name
--almost-all          --group-directories-first
....
....

Setup?

My installation already included a rule for git:

/etc/bash_completion.d/git

The installation of this file was done when I installed the git package. I’m on Fedora 19, but other distros should be doing this as well.

$ rpm -qf /etc/bash_completion.d/git
git-1.8.3.1-1.fc19.x86_64

Looking at the rules file it explains that it will facilitate auto-completion in the following situations:

# The contained completion routines provide support for completing:
#
#    *) local and remote branch names
#    *) local and remote tag names
#    *) .git/remotes file names
#    *) git 'subcommands'
#    *) tree paths within 'ref:path/to/file' expressions
#    *) file paths within current working directory and index
#    *) common --long-options
#

So this should “just work” for you.

Method 3

You can do something like this with extrakto:

  • press tmux prefix + tab to start extrakto
  • fuzzy find the text/path/url
  • press
    • tab to insert it to the current pane,
    • enter to copy it to the clipboard,
    • ctrl-o to open the path/url or
    • ctrl-e to edit with $EDITOR


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x