Skip to content
Furkan Uzumcu edited this page Aug 28, 2022 · 8 revisions

Welcome to the zettelkasten.nvim wiki!

Since some built-in Vim functionality is not that commonly known this wiki will include life hacks that will increase your understanding of Vim built-ins and also make things easier for you to navigate your notes using zettelkasten.nvim.

:help ltag

Jump to tag [name] and add the matching tags to a location list for the current window. [tagsrch.txt]

You can use this command to list notes that use a particular tag.

ltag

:help zettelkasten-[I

You can use this mapping to list all the notes that refer to the note ID under the cursor.

I

:help keywordprg

Program to use for the |K| command. [options.txt]

This option is used along side :help :ZkHover command. zettelkasten.nvim automatically sets keywordprg option for the markdown buffer if it has not been set before. Once it's set, you can press K over a note ID and can see its title. If you want to preview the note in the :help preview-window, then you can customize this option to setlocal keywordprg=:ZkHover\ -preview.

zlhover

Browse Notes

By default, zettelkasten.nvim uses a date based ID system. This allows you to maintain the same ID while changing the title of your note as you please. Since the file names are just IDs, it's hard to see what they are about. This is where the browser comes in. Run :ZkBrowse or edit zk://browser and you'll see your notes along with their titles. In here, you can use K to also preview the notes or use tags to jump to them or list other tags.

zkbrowse

Preview Images on macOS

Put this in your markdown.lua file to preview an image file on macOS.

if vim.fn.executable("qlmanage") == 1 then
    vim.api.nvim_buf_set_keymap(
        0,
        "n",
        "<leader>p",
        ':call jobstart(["qlmanage", "-p", expand("<cfile>")], {"cwd": expand("%:h")})<CR>',
        { noremap = true, silent = true, nowait = true }
    )
end

preview

Insert Table of Contents to a Note

This snippet is directly taken from the documentation. Add the following code to your markdown.lua file:

vim.api.nvim_buf_add_user_command(0, "ZkInsertTOC", function(opts)
    vim.api.nvim_buf_set_lines(
        vim.api.nvim_get_current_buf(),
        opts.line1,
        opts.line2,
        true,
        require("zettelkasten").get_toc(opts.args)
    )
end, {
    nargs = 1,
    range = true,
})

zkinserttoc

Generate Tags Using Universal Tags

Credit goes to Edwin Wenink.

if vim.fn.executable("ctags") == 1 then
    vim.api.nvim_buf_add_user_command(
        0,
        "MdZkUpdateTags",
        "!ctags -R --langdef=markdowntags --languages=markdowntags --langmap=markdowntags:.md --kinddef-markdowntags=t,tag,tags --mline-regex-markdowntags='/(^|[[:space:]])\\#(\\w\\S*)/\\2/t/{mgroup=1}' .",
        {
            range = false,
        }
    )
end

View Note Outline Using TagBar

Install TagBar and while viewing your note, run :TagBar to view the outline of your note.

Screen Shot 2022-05-21 at 16 05 45

Sorting Notes in :ZkBrowse

You can use the usual buffer manipulation commands (e.g :sort, :g) to change the contents of the browser. In order to refresh, you just refresh the buffer with :e!.

If you use the default :sort command, you can sort files in ascending or descending date order. For example, running :sort! will sort the lines in a date descending order, making the first line your most recent note. If you use :sort, then the first line will be the oldest note.

You can also use your custom sort algorithm to sort the notes in a most recently modified way. You can see an example of this from my dotfiles. Here's the full snippet:

" custom_sort.vim
" Compare Functions {{{

function <SID>compare_modified_time(f1, f2)
    let l:mtime1 = getftime(a:f1)
    let l:mtime2 = getftime(a:f2)
    if l:mtime1 < l:mtime2
        return 1
    endif

    if l:mtime1 > l:mtime2
        return -1
    endif

    return 0
endfunction

function <SID>compare_modified_time_dirvish(f1, f2)
    return <SID>compare_modified_time(a:f1, a:f2)
endfunction

function <SID>compare_modified_time_zkbrowser(f1, f2)
    return <SID>compare_modified_time(a:f1[:21], a:f2[:21])
endfunction

function s:compare_length(a, b)
    let x = strlen(a:a)
    let y = strlen(a:b)
    return (x == y) ? 0 : (x < y) ? -1 : 1
endfunc

" }}}

function custom_sort#sort_command_completion(A,L,P)
    return ["-modified", "-length", "-uniq", "-folder-first"]
endfunction

function custom_sort#sort(mode, start_line, end_line)
    if a:mode == "-folder-first" && a:start_line == a:end_line
        execute "sort ,^.*[\/],"
        return
    elseif a:mode == "-folder-first"
        execute "'<,'>sort ,^.*[\/],"
        return
    end

    if a:start_line == a:end_line
        let l:lines = getline(0, line('$'))
        let l:start_index = 1
    else
        let l:lines = getline(a:start_line, a:end_line)
        let l:start_index = a:start_line
    endif

    if a:mode == "-modified"
        if &filetype != "dirvish" && &filetype != "zkbrowser"
            echohl Error
            echo "[custom-sort] This mode is only available in dirvish/zkbrowser buffers: " . a:mode
            echohl Normal
            return
        endif

        if &filetype == "dirvish"
            call sort(l:lines, expand("<SID>") . "compare_modified_time_dirvish")
        else
            call sort(l:lines, expand("<SID>") . "compare_modified_time_zkbrowser")
        endif

        call setline(l:start_index, l:lines)
    elseif a:mode == "-length"
        call sort(l:lines, expand("<SID>") . "compare_length")
        call setline(l:start_index, l:lines)
    elseif a:mode == "-uniq"
        call uniq(l:lines)
        call setline(l:start_index, l:lines)
    else
        echohl Error
        echo "[custom-sort] Unsupported mode: " . a:mode
        echohl Normal
    endif
endfunction

After this, you can just run :Sort -modified while in :ZkBrowse and the last modified file will show up at the top.