The documentation of fnameescape
contains an example command that
moves the cursor to the most recently modified file, but that file could
also be a directory.
So to exactly address your request, put the fragment below in
~/.config/vifm/vifmrc
, restart Vifm and now you can use
,f
to jump to the most recent non-directory file*.
"Cursor to most recently modified file
noremap ,r :exe 'goto' fnameescape(system('ls -At | head -n 1'))<CR>
"Cursor to least recently modified file
noremap ,o :exe 'goto' fnameescape(system('ls -Art | head -n 1'))<CR>
"Cursor to most recently modified non-directory file
noremap ,f :exe 'goto' fnameescape(system('ls -Atp | grep -vm1 /'))<CR>
"Cursor to least recently modified non-directory file
noremap ,l :exe 'goto' fnameescape(system('ls -Atrp | grep -vm1 /'))<CR>
As you can see, I also provide additional related mappings for your convenience.
*Assuming the file-name does not contain a newline character.
How does it work?
goto
is the Vifm function that moves the cursor to a given file.
The desired file is the first non-directory file listed by ls -Atp
, i.e., the
first line that does not contain a slash. grep -v /
filters out lines that
contain a slash, and the -m1
option tells it to only return the first match.
Although -m
is available in BSD and GNU/Linux systems, it is not required by POSIX,
so if your Grep lacks it, just use
grep -v / | head -n 1
or sed '\:/:d;q'
instead.
{
and}
to jump directly to last/first of the files/directories. However,{
can make you land in a directory, so you will still need aj
to go down one row and reach the file. – Quasímodo Feb 11 '21 at 14:04{
takes you to the last directory, just above the files. That isn't exactly what I asked for - I guess that is why you didn't give it as an answer - but it is still very helpful. I don't have the reputation points to upvote your response, so thanks! – Shawn Feb 11 '21 at 18:02noremap { {j
that will do that directly. – Quasímodo Feb 12 '21 at 11:41