Skip to content

Instantly share code, notes, and snippets.

@ovelny
Created July 31, 2021 09:58
Show Gist options
  • Save ovelny/72659e841c1dbcee173eb244c8609252 to your computer and use it in GitHub Desktop.
Save ovelny/72659e841c1dbcee173eb244c8609252 to your computer and use it in GitHub Desktop.
Script to make vimwiki + various plugins work like Joplin, with support for note encryption (https://ovelny.sh/blog/switching-from-joplin-to-vimwiki/)
" Plugins will be downloaded under the specified directory.
call plug#begin('~/.vim/plugged')
" Declare the list of plugins.
Plug 'plasticboy/vim-markdown', { 'for': 'markdown' }
Plug 'ferrine/md-img-paste.vim', { 'for': 'markdown' }
Plug 'vimwiki/vimwiki'
Plug 'iamcco/markdown-preview.nvim', { 'do': { -> mkdp#util#install() }, 'for': ['markdown', 'vim-plug']}
" Optional: goyo
" Plug 'junegunn/goyo.vim'
" List ends here. Plugins become visible to Vim after this call.
call plug#end()
" autocapitalisation tweaked from the following sources:
" https://davidxmoody.com/2014/vim-auto-capitalisation/
" https://github.com/davidxmoody/dotfiles/blob/master/dotfiles/config/nvim/init.vim
func! AutoCapitalisation()
augroup SENTENCES
au!
autocmd InsertCharPre * if search('\v(%^|[.!?]\_s+|\_^\-\s|\_^\*\s|\_^#+\s|\n\n)%#', 'bcnw') != 0 | let v:char = toupper(v:char) | endif
augroup END
endfu
autocmd FileType markdown call AutoCapitalisation()
" encryption with markdown for vimwiki
" you need to have gpg installed with an existing keypair
au BufEnter *.md.gpg setlocal filetype=markdown
" Optional: always run vimwiki files with Goyo toggled on
" au VimEnter *.md.gpg execute ':Goyo'
" Vimwiki opens many buffers, which can be tedious to handle
" seamlessly with the gpg augroup below. Even though this is
" a hack, it is way easier to wrap your head around this if
" the vimwiki session relies entirely on its autosaving, and
" is also quit entirely with no buffers left unclosed. To
" achieve this, we remap :wq and :q to :wqa and :qa,
" and disable :w and :wa.
au VimEnter *.md.gpg execute ":cabbrev wq wqa"
au VimEnter *.md.gpg execute ":cabbrev q qa"
au VimEnter *.md.gpg execute ":cabbrev w <Nop>"
au VimEnter *.md.gpg execute ":cabbrev wa <Nop>"
" First make sure nothing is written to ~/.viminfo or backups while editing
" an encrypted file.
set backupskip+=*.gpg
set viminfo=
augroup encrypted
au!
" Disable swap file, undo file and backups, and set binary file format
" before reading the file
autocmd BufReadPre,FileReadPre *.gpg
\ setlocal noswapfile noundofile nobackup bin
" Decrypt contents after reading the file, reset binary file format
" and run any BufReadPost autocmds matching the filename without .gpg
autocmd BufReadPost,FileReadPost *.gpg
\ execute "%!gpg --decrypt --default-recipient-self 2>/dev/null" |
\ setlocal nobin |
\ execute "redraw!" |
\ execute "doautocmd BufReadPost " . expand("%:r")
" Set binary file format and encrypt contents when leaving vim
autocmd Vimleave *.gpg
\ setlocal bin |
\ bufdo execute "%!gpg --encrypt --armor --default-recipient-self"
" Contrary to the original scripts, we are not handling an undo command
" to revert encryption in buffer: vimwiki auto-saves contents when quitting,
" so we rather leave it at that and disable :w and :wa for .md.gpg files.
" If you don't like auto-saving, sorry!
augroup END
" vimwiki settings
set nocompatible
filetypelugin on
syntax on
let g:vimwiki_list = [{'path': '~/wiki/',
\ 'syntax': 'markdown', 'ext': '.md.gpg'}]
" Automatically insert a header when creating a new link
let g:vimwiki_auto_header = 1
" Use underscores to replace spaces in the file names
let g:vimwiki_links_space_char = '_'
" Disable all concealing
let g:vimwiki_conceal_onechar_markers = 0
" Disable URL shortening
let g:vimwiki_url_maxsave = 0
" Don't load vimwiki for markdown files located somewhere else
let g:vimwiki_global_ext = 0
" Bold headers
hi VimwikiHeader1 cterm=bold gui=bold
hi VimwikiHeader2 cterm=bold gui=bold
hi VimwikiHeader3 cterm=bold gui=bold
hi VimwikiHeader4 cterm=bold gui=bold
hi VimwikiHeader5 cterm=bold gui=bold
hi VimwikiHeader6 cterm=bold gui=bold
" md-img-paste settings for vimwiki and markdown files
autocmd FileType markdown nmap <buffer><silent> <leader>, :call mdip#MarkdownClipboardImage()<CR>
let g:mdip_imgdir = 'images'
let g:mdip_imgname = 'image'
" Markdown preview
autocmd FileType markdown nmap <leader>n :MarkdownPreviewToggle<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment