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.

How can I toggle between two sorting states?

0 votes
asked Dec 4, 2015 in vifm by anonymous
edited Dec 4, 2015 by xaizek

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
answered Dec 4, 2015 by xaizek
edited Jan 12, 2016 by xaizek

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>
commented Dec 4, 2015 by anonymous

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

commented Dec 4, 2015 by xaizek

Added second version to the answer.

commented Jan 12, 2016 by anonymous

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.

commented Jan 12, 2016 by xaizek

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 won't be considered as spam, but this is not a perfect place for it.
...