Add debates from previous command to zsh conclusion

In zsh (along with bash) you can make use of some background word developments to represent debates from previous commands.

This instance reveals obtaining the 2nd parameter from the previous command in background with !:# development:

% echo foo bar baz
foo bar baz
% echo !:2
echo bar

I usually neglect specifically what # parameter a certain argument is and also keying !:# isn't constantly that fast when I do bear in mind which arg it is. I find out about meta-. to change the last argument, yet occasionally it isn't the last arg that I desire.

I would certainly such as to add the debates from the previous command as pointers to finish any kind of command that I'm keying in zsh.

I had the ability to identify just how to create a shell function that can create an array of debates (0. N) from the last command and also bind it to a certain command.

_last_command_args() {
    last_command_array=("${(s/ /)last_command}") 
    _sep_parts last_command_array

# trying to get last_command_args to be suggested for any command, this just works for foo
compdef _last_command_args foo

This is what it resembles for finishing simply foo where I struck the tab key at <TAB>:

% echo bar baz qux
bar baz qux
% foo <TAB>
bar   baz   echo  qux 

This functions wonderful for finishing the command "foo", yet I would certainly such as these to be alternatives on any kind of zsh development that I do. I assume it is obtained something to do with the zstyle completer things, yet after a couple of hrs of hacking around I understood I'm out of my deepness.

Just how can I get the debates from my previous command as recommended conclusions for any kind of command in zsh?

I've obtained my complete zshrc compinstall file shared out on bitbucket if that aids. Great deals of it is cribbed from a selection of resources and also several of it I've hacked with each other myself.


@Julien Nicoulaud is solution obtained me close, I'm noting it as approved as it obtained me where I required to get.

With my certain config, making use of the recommended:

zstyle ':completion:*' completer _last_command_args _complete

Didn't fairly benefit me as it was creating tab conclusion to just present the checklist of debates from the last command (though it 'd in fact finish with filenames too, simply not present them). Scuffing to _complete _last_command_args did the opposite. It would certainly present the regular filenames, yet not last_command_args

I'm presuming this has something to do with the means completer functions. I assume it just presents the result from the first method that returns efficiently, yet I'm having problem analyzing the zsh resource to recognize totally what is taking place. I had the ability to fine-tune my method to include a phone call to _complete to make sure that it revealed both last argument regulates along with the normal autocomplete things. Not fairly as apart, yet functions well adequate for me.

Below is the complete function I made use of in addition to the various other zstyle things I have:

# adds the arguments from the last commadn to the autocomplete list
# I wasn't able to get this to work standalone and still print out both regular
# completion plus the last args, but this works well enough.
_complete_plus_last_command_args() {
    last_command_array=("${(s/ /)last_command}") 
    _sep_parts last_command_array

_force_rehash() {
  (( CURRENT == 1 )) && rehash
  return 1  # Because we didn't really complete anything

zstyle ':completion:::::' completer _force_rehash _complete_plus_last_command_args _approximate 

Other zstyle lines I have, not essential for this to function, yet could influence why this benefits me:

zstyle -e ':completion:*:approximate:*' max-errors 'reply=( $(( ($#PREFIX + $#SUFFIX) / 3 )) )'
zstyle ':completion:*:descriptions' format "- %d -"
zstyle ':completion:*:corrections' format "- %d - (errors %e})"
zstyle ':completion:*:default' list-prompt '%S%M matches%s'
zstyle ':completion:*' group-name ''
zstyle ':completion:*:manuals' separate-sections true
zstyle ':completion:*' menu select
zstyle ':completion:*' verbose yes

Now, if I'm in a directory site with file1.txt and also file2.txt, and also my last command was echo foo bar baz, I get this for autocomplete which is simply what I desired:

% ls
bar   baz   echo  foo 
- files -
file1.txt   file2.txt 
2019-05-13 05:00:45
Source Share
Answers: 1

You can add your completer to the list of completers made use of by default:

zstyle ':completion:*' completer _last_command_args _complete
2019-05-21 07:23:49