Welcome to Vifm Q&A, where you can ask questions about Vifm usage. Registration is optional, anonymous posts are moderated. GitHub or Google logins are enabled.
0 votes
in vifm by
edited by

I would like s to toggle between two sorting states. This is what I have so far:

nnoremap s :set sort=-size<cr>
qnoremap s q:set sort=+name<cr>

1 Answer

0 votes
edited by

Didn't get why one of your mappings is for normal mode and another one for quick view, but here is a way to do the switch in normal mode:

nnoremap s :if &sort == '+name'
         \ |    set sort=-size
         \ |    echo 'Sorted by size'
         \ |else
         \ |    set sort=+name
         \ |    echo 'Sorted by name'
         \ |endif<cr>

Version with modification date:

command! togglesort
         \ :if &sort == '-size,+name'
         \ |    set sort=+mtime
         \ |    echo 'Sorted by modification date'
         \ |else
         \ |    if &sort == '+name'
         \ |        set sort=-size
         \ |        echo 'Sorted by size'
         \ |    else
         \ |        set sort=+name
         \ |        echo 'Sorted by name'
         \ |    endif
         \ |endif
nnoremap s :togglesort<cr>

Nice! How would one go about adding Time Modified as a third sorting option?


Added second version to the answer.


Would it be possible to ask vifm to return its current sorting state instead of using a $sort_type variable? The problem with the current approach is that because $sort_type is not defined at first, typing s only defines the variable when start vifm sorted by size, and you have to press s again to sort by date. It well when you start vifm sorted by mtime, though.


Updated the answer. I had the same problem, but worked around it by setting $sort_type in vifmrc, but it still had an issue that one variable is shared by both panes. Now it should work fine.

If you would like to make a bug report or feature request consider using GitHub, SourceForge or e-mail. Posting such things here is acceptable, but this is not a perfect place for them.