Recently edited files

Here’s an Idea how one can implement recent files in Kakoune:

hook global BufOpenFile .* recentf-add-file
hook global BufWritePost .* recentf-add-file

declare-option str recentf_file "%val{config}/.recentf"
declare-option int max_recentf_files 45

define-command -hidden recentf-add-file %{ nop %sh{
    if ! grep -q "${kak_buffile}" "${kak_opt_recentf_file}"; then
        printf "%s\n%s\n" "${kak_buffile}" "$(cat ${kak_opt_recentf_file})" > ${kak_opt_recentf_file}
        printf "%s\n" "$(head -${kak_opt_max_recentf_files} ${kak_opt_recentf_file})" > ${kak_opt_recentf_file}
    fi
}}

define-command recentf -params 1 -shell-script-candidates %{ cat ${kak_opt_recentf_file} } %{ edit -existing %arg{1} }

The only thing I don’t really like about this implementation is that -shell-script-candidates sorts candidates and most recent files are no longer at the top of the list. I could use -shell-script-completion as it doesn’t do sorting, but it doesn’t allow fuzzy matching.

2 Likes

Since kakoune doesn’t seem to support fuzzy matching with non-alphabetic-sorting, what about using an external tool?

I found a snippet for using rofi on open buffers at https://github.com/mawww/kakoune/wiki/Fuzzy-finder#rofi:

define-command rofi-buffers \
-docstring 'Select an open buffer using Rofi' %{ evaluate-commands %sh{
    BUFFER=$(printf "%s\n" "${kak_buflist}" | tr " " "\n" | rofi -dmenu | tr -d \')
    if [ -n "$BUFFER" ]; then
        printf "%s\n" "buffer ${BUFFER}"
    fi
} }

After doing some testing, rofi does seem to display things in the order they’re passed in, rather than alphabetic order, and it can kind of support fuzzy matching.

1 Like

I’m using fzf for that

1 Like