Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Simple journal with vim

So how would you use your text editor to make a daily notes?

For vim I end up using simple approach:

  • have a ~/docs/journal/ directory where I keep yearly journals in asciidoc format (one can use markdown of course).

  • have a mapping to quickly open a journal ready to enter you thoughts, notes or a todos for a day.

Define a mapping
" edit global journal file
nnoremap <silent> <leader>ej :call journal#new()<CR>
Create ~/.vim/autoload/journal.vim
" Personal jounaling using vim and asciidoctor
" Example mapping: nnoremap <silent> <leader>ej :call journal#new()<CR>
"
" * Opens ~/docs/journal/YEAR.adoc file, where YEAR is a current year, like 2020
"
" * Position cursor 2 lines after current date heading
"     == 2020-08-14 Heading
"
"     CURSOR IS HERE
"
" * If there is no current date heading, create it and position cursor 2 lines after
"     == 2020-08-14
"
"     CURSOR IS HERE


func! journal#new() abort
    let jfilename = strftime("%Y") . '.adoc'
    let jfullname = printf('~/docs/journal/%s', jfilename)

    call s:new_entry(jfullname)
endfunc


func! s:new_entry(jfilename) abort
    if bufexists(a:jfilename)
        exe "b " . a:jfilename
    else
        exe "e " . a:jfilename
    endif

    normal! gg

    let journal_heading = '= ' . strftime("%Y")
    if getline(1) !~ '^' . journal_heading
        call append(0, [journal_heading, ""])
    endif

    let heading_date = '== ' . strftime("%Y-%m-%d")
    if search('^'.heading_date, 'cw')
        normal! 2j
        return
    endif

    if search('^==\s*\S\+', 'cw')
        call append(line('.')-1, heading_date)
        normal! 4O
        normal! 2k
    else
        call append(line('$'), heading_date)
        normal! G
        normal! 2o
    endif
endfunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment