One thing that I frequently do is edit the most recently modified files, so instead of typing "ls -lr" and then "vim lastfile", I thought I would make some shortcuts in my ~/.bash_profile file:
alias via="vim `ls -rt | tail -1`"
alias vib="vim `ls -rt | tail -2 | head -1`"
alias vic="vim `ls -rt | tail -3 | head -1`"
alias vid="vim `ls -rt | tail -4 | head -1`"
alias vie="vim `ls -rt | tail -5 | head -1`"
The problem is that, weirdly enough, these commands don't work. They open some file that isn't one of the last, or even a file was deleted from the current directory (I wonder if there's some kind of file cache updating issue in the directory. This occurs on both my local machine and the cluster I work on).
However, if I type vim `ls -rt | tail -1`
directly, without using the alias, it works every time.
Best Answer
The problem is you need to quote the backticks in your alias definition. Double quotes (
"
) do not quote command substitution. You will need single quotes ('
). UseThough you'd actually want:
That is:
$(...)
) while we are at it.--
to mark the end of options forvim
(otherwise, it wouldn't work for filenames starting with-
or+
).ls -t | head
instead ofls -rt | tail
to get the result sooner.Do not use
If you do that the command substitution happens when you define the alias, not when you run it. Try typing
alias via
to see that the output is not actuallyalias via='vim `ls -rt | tail -1`'
but ratheralias via='vim <prematurely expanded output>'
.