Autocomplete newest file

You can easily configure this in zsh, e.g. with something like this:

zstyle ':completion:*' file-sort date

(you can also change the line such that this style is only used for certain file name patterns)

zsh is very similar to bash, you could probably call it a superset of bash - feature-/usage-wise.

But perhaps bash has a similar feature.


Just remove the vim from the alias. Do something like this:

alias latest='ls -tr | tail -n 1'

You can then use any program to open the latest files:

emacs `latest`
ls `latest`
mv `latest` ../

etc.

However, this will break if your file names have spaces or weird characters which is why you should never parse ls. A better way would be something like this (add this to your .bashrc) :

function latest(){
  $1 "$(find . -type f -printf "%C@ %p\n" | sort | tail -n 1 | cut -d " " -f 2-)"
}

This function will execute whatever command you give it as an argument and pass the result of the find call (the latest file) to that command. So, you can do things like:

latest emacs
latest less

If you need to be able to do things like mv $(latest) foo/ try this one instead:

function latest(){
   A=(${@})
   prog=$1;
   lat=$(find . -type f -printf "%C@ %p\n" | sort | tail -n 1 | cut -d " " -f 2-)
   if [ ! -z $2 ] ; then 
     args=${A[@]:1}; 
     $prog "$lat" "${A[@]:1}"
   else
     $prog "$lat"
   fi
}

Then, to copy the latest file to bar/, you would do

latest cp bar

And you can still do latest emacs in the same way as before.


Since the newest file is also sorted last, you could use menu-complete-backward. menu-complete and menu-complete-backward cycle through completions or insert the first or last completion. I have bound them to option-tab and shift-tab in ~/.inputrc:

"\e\t": menu-complete
"\e[Z": menu-complete-backward

Your terminal emulator might not insert \e[Z when you press shift-tab. Use C-v or cat -v to see what text is inserted when you press a key combination.