11

On MacOS Big Sur 11.3, here is my .zshrc. I would like to get the latest modified or create files and directories near to the prompt (sorted from the most recent up to the oldest ones). Here my current config in ~/.zshrc :

# ZSH completion
autoload -Uz compinit
compinit
# Colorize completions using default `ls` colors.
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"

# Zsh reverse auto-completion
zmodload zsh/complist
bindkey '^[[Z' reverse-menu-complete
# To get new binaries into PATH
zstyle ':completion:*' rehash true

zstyle ':completion:*:complete:(ls):*' file-sort date reverse
zstyle ':completion:*:complete:(cd):*' file-sort date reverse
zstyle ':completion:*:complete:(cp):*' file-sort date reverse
zstyle ':completion:*:complete:(mv):*' file-sort date reverse

# Variables not very well known
# Disable prompt disappearing on multi-lines
export COMPLETION_WAITING_DOTS="false"

The issue is that when I press TAB after a "l" which is actually the alias:

alias l='grc -es --colour=auto ls --color -Gh -C -lrt'

grc is a tool to colorify the files.

Indeed, I have not as the first result the most recent modified or created file or directory which are suggested.

Which option could I add in zsh completion to get as first results after pressing TAB these last recent (modification or creation) files or directories?

The first command applied is "l" which corresponds to the alias:

alias l='grc -es --colour=auto ls --color -Gh -C -lrt'
  1. Once I type "l", I want, when I touch the TAB (auto)-completion , the most recent modified files as suggestions near to the prompt from which I perform the "l" + TAB completion..

As an example, here the below figure when typing a simple "l" command (seee alias above) :

example of alias "l" alias

Main goal : the most important goal of this post : if I type "l+TAB+TAB", I would like that the most recent file/directory appears firstly as suggestion : in my case, the first suggestion after this commmand would be filename2, after a second "TAB" the suggestion dir_1 and the third suggestion dir_8, etc (see the order of simple command "l").

  1. Now, if I type "l+TAB", I get :

example of typing "l+TAB"

In option,I would like to avoid this last result (under the form of a menu but I would like rather a list) when I perform a "l+TAB" but I don't know which line to add or modify in ~/.zshrc. This is not the priority.

UPDATE 1: I have almost found the solution for typing twice on TAB key after a "l" alias which can be assimilated to a ls -lrt. Here the peudo magic command :

bindkey '\t' reverse-menu-complete

But the problem is that with this option, when I press on a first time on TAB, a suggestion is automatically done with the most recent file or dir.

Example : I if do : $ l +TAB, I get on my following above capture :

early suggestion

What I would like to get is to have the most recent file suggestion when I type a second time on TAB and not as soon as I have typed a second time.

UPDATE 2: I am close the final wanted behavior. I set :

zstyle ':completion:*:complete:(ls|cd|cp|mv|vim|cat|more|tail|head|open):*' file-sort date reverse

bindkey '^\t' reverse-menu-complete
bindkey '^[[Z' menu-complete

If I do a first l + TAB, I have the correct most recent file automatically added first, and a second TAB pushing suggests from the most recent to oldest file (reverse ordering).

It is missing just a modification to have l + TAB which has to not add suggestion file, just list all the files from oldest to most recent and after a second TAB, suggest firstly the most recent files from older with ^[[Z' menu-complete.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 3
    There are multiple questions scattered through this, and there seems to be different versions of your `completion` string (some using `modification`, some using `date`). The final question seems to be "How to calculate the size of the terminal emulation window and compare it to the number of lines required to list all files in the current directory"; you might want to ask that in a separate question? The answer to your _title_ is already given: `zstyle ':completion:*' file-sort date`, then type `l` followed by `TAB`, and the first suggestion is the newest file. – simont Dec 01 '21 at 23:38
  • I _think_ you're really trying to alter the behaviour of the completion menu (not the options within it). Check the `menu` section of the [compsys `standard style`](https://linux.die.net/man/1/zshcompsys) documentation; try `zstyle ':completion:*' menu yes=long select` to show the menu when there's too many options to fit on the screen. Alternatively always try setting `AUTO_MENU` and `MENU_COMPLETE` options (when testing it seems I need both, not either-or, but check the man pages). If none of that's helpful, can you clarify in the question so it's clear what you want :) – simont Dec 02 '21 at 00:21
  • For coloring the menu, check the [zsh guide, chapter 6.5.2](https://zsh.sourceforge.io/Guide/zshguide06.html), for `list-colors` which I _think_ might do what you want. As to why three of the items seem to be selected in your screenshot, that'd be a different question entirely :) – simont Dec 02 '21 at 11:52
  • Can you provide better explanation or illustrations of what you mean by "near to the prompt whatever I am on the top on the terminal or at the bottom"? For both question `3.2` and `3.3` the behavior's exactly as specified: you've got `reverse` in your `zstyle` (see the `file-sort` section of [the Completion System's Standard Styles](https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Standard-Styles) documentation). Why does removing `reverse` from *both* lines of your `zstyle` not fix this? – simont Dec 21 '21 at 00:08
  • 1
    Further, your question lists *two different zstyles* and would be greatly improved if you clarified or stripped out any unnecessary edits here, as it's difficult to actually replicate your system. – simont Dec 21 '21 at 00:09

1 Answers1

1

To have files/directories completion for any command (unless overridden with a different completion), sorted by modification date (recent to old), as a detailed list -

  1. Add the following to ~/.zshrc :
autoload -Uz compinit
compinit
zmodload zsh/complist

zstyle ':completion:*' file-list all
zstyle ':completion:*' file-sort date
  1. Reload zsh profile - run source ~/.zshrc on an existing terminal session or open a new terminal session for changes to take effect

  2. Trigger detailed list completion - Before hitting TAB - hit space after command. For example l<space><TAB>.

  3. Keep on hitting TAB to select

Eladio
  • 369
  • 1
  • 5
  • 1
    Thanks for your answer. Unfortunately, this doesn't work. I invite you to look at my **UPDATE** where I am near to the solution with a bindkey but suggestion appears before I have listed all the files. I would like to get the most recent file suggestion when I type a second time. Best regards –  Apr 19 '22 at 23:26
  • 1
    @Eladio : did you understand my last issue in **UPDATE** regarding the suggestion which is badly done (it is suggested the most recent as soon as the first `l +TAB` whereas I want the most recent suggestion after the second `TAB` pushing) ? Regards –  Apr 21 '22 at 20:11