profile picture

Michael Stapelberg

zsh: tab-completing recent files (2012)

published 2012-11-16, last modified 2018-03-18
Edit Icon

This short article describes the most important productivity enhancement for working in a shell (zsh) in the last few months for me: A shortcut for completing the most recent files.

In my daily work, it often happens that I save an email attachment (or download a file from the internet) and then do something with it in a shell, like unpacking it, applying a patch to source code, and so on.

While I do have an alias to execute ls -hltr which shows me the most recently modified files, it is much more convenient to just tab-complete the most recent file.

Based on Feh’s zsh configuration, I added the following statements to my zshrc:

# 'ctrl-x r' will complete the 12 last modified (mtime) files/directories
zle -C newest-files complete-word _generic
bindkey '^Xr' newest-files
zstyle ':completion:newest-files:*' completer _files
zstyle ':completion:newest-files:*' file-patterns '*~.*(omN[1,12])'
zstyle ':completion:newest-files:*' menu select yes
zstyle ':completion:newest-files:*' sort false
zstyle ':completion:newest-files:*' matcher-list 'b:=*' # important

Now, being in /tmp and launching vi, when I press Control+x followed by r, this is what I get:

zsh: tab-completing recent files

I run a blog since 2005, spreading knowledge and experience for almost 20 years! :)

If you want to support my work, you can buy me a coffee.

Thank you for your support! ❤️