10

In editors like vim and emacs, one can use hotkeys like C-p (vim) or M-/ (emacs) to perform auto-completion on the current word using other words in the same buffer.

Is it possible to achieve the same functionality within a shell? If I see some word in output from a previous command and I'd like to quickly auto-complete it when typing out another command.

Just to be clear, an example:

$ ls
Desktop/  Mail/  music/  osx@  something_with_a_pretty_long_name

$ someth

From here, I'd like to be able to hit a hotkey and have it automatically complete what I'm typing to something_with_a_pretty_long_name.

EDIT: Shoot. Using a filename was a poor example for what I'm after. Consider the following, second example:

$ cat /var/log/something.log
[19:30] Service started
[19:35] Something else happened

$ happ

Where I could auto-complete 'happ' to 'happened' merely because it appeared in my terminal buffer. I hope this clarifies what I'm after.

3
  • Non-answer (because it's a touch cheeky): how about running the shell in emacs (M-X shell)? M-/ works as always, and <kbd>Tab</kbd> works for just filenames/commands.
    – Alexios
    Jun 29, 2012 at 17:38
  • I indeed sometimes do do this! That's actually where I got the urge to have this functionality in any terminal. =)
    – noffle
    Jun 29, 2012 at 21:05
  • I feel your pain. I like the extra features in the emacs shell, but it tends to annoy me too much to use for protracted lengths of time. Emacs of course implements this at the terminal level, and that's probably where you'd want to look for it. And I fear you'd need a patch. I haven't seen a mainstream (or niche) terminal provide this functionality.
    – Alexios
    Jun 30, 2012 at 10:02

4 Answers 4

4

Short answer: No, not with classical shells like zsh or bash as typically your shell and terminal are different entities.

Long answer: Maybe. What typical happens is you enter a command and press Enter - now your shell forks and executes the specified command. Then the shell has no idea what is happening, the shell does not know what will be written to the terminal or what the executed program did, if it started other programs, wrote to your terminal or anything.

There are two possible solutions to your problem:

  1. Capture all the output from your executed commands and use it somehow for completion purpose (can be done but probably shouldn't be done)
  2. Use a different Terminal supporting 'completing' from your "shell buffer".

A solution for 2 is for example using emacs and multi-term and just using M-/ like you normal would to complete things from the terminal. Other terminals may offer a similar solution.

But do you really need this kind of completion? shell completion nowadays can complete man pages, commands, pids, sockets, command options, file paths from remote systems, and many more. This may be already sufficient for your use case or could be enhanced for your specific problem.

1
  • Although disappointing, this was pretty much the answer I was expecting. Thank you for the explanation you gave regarding the disconnect between the shell executing a command and the stuff that goes on in the background unbeknownst to it.
    – noffle
    Jun 29, 2012 at 16:06
2

http://www.iterm2.com/#/section/features/autocomplete

iterm2 has exactly what you need. Bad thing it's for mac.

But since it's terminal you can ssh to linux/unix and it still works.

1
  • Thanks for the recommendation; I wasn't aware iTerm2 could do this.
    – noffle
    Jul 9, 2012 at 19:40
2

You can do this with extrakto which uses tmux to capture the output and let's you fuzzy select with fzf:

extrakto

0

This is probably not the answer you'd expect, but it may serve some of your needs.

You can use TAB to auto-complete the command which is in the $PATH (or internal commands), so if you filename is executable file and it's in the $PATH it will be auto-completed.

If you want to autocomplete on filename which is not executable you can type "." -dot, then space (or whatever in place of dot) then the part of filename and TAB. This way you will trick the shell to believe you are executing a command and giving the filename as argument to the command. Pressing the TAB will autocomplete the filename, but you will have to remove the dot+space in front perhaps.

Another possibility is to use the BASH completion feature. This can auto-complete more than just filenames.

1
  • Thank you for taking the time to try to assist, but this is not what I'm looking for. I am already aware of (and frequently use) tab completion in my shell. What I'm after is a way of getting completion on arbitrary terminal output. I've updated my post to better describe this.
    – noffle
    Jun 29, 2012 at 2:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .