1

I have not been able to find a VIFM motion that moves directly to the most recently edited file when I am elsewhere in the directory. Is there such a motion? If not, is it possible to define one?

I have tried using the window sorting for Time Modified, then jumping to the top with gg. However, that sorting puts directories at the top of the window, so gg takes me there. It is close, but because this is something I often want to do, I am hoping to minimize the effort to get to it. Thanks!

Shawn
  • 11
  • You can use { and } to jump directly to last/first of the files/directories. However, { can make you land in a directory, so you will still need a j to go down one row and reach the file. – Quasímodo Feb 11 '21 at 14:04
  • OK, I tested this. What wasn't clear to me from your comment was that { 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:02
  • Yes, that is why I don't give it as an answer. I'm glad it was helpful. Do note you can also make a map noremap { {j that will do that directly. – Quasímodo Feb 12 '21 at 11:41
  • 1
    That does exactly what I was looking for! I still can't upvote, so thanks! – Shawn Feb 15 '21 at 17:47

1 Answers1

0

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.

Quasímodo
  • 18,865
  • 4
  • 36
  • 73