1---
2title: "Vim as a Markdown Editor"
3description: "Configuring Vim to act as a first-class markdown editor for various purposes"
4cover: /assets/pngs/editor.png
5date: 2020-04-30T23:06:59-04:00
6categories:
7 - Technology
8tags:
9 - Vim
10 - Markdown
11 - Zettelkasten
12 - 100 Days To Offload
13toc: true
14---
15I've recently decided to attempt to keep all of my notes and everything I've learned in a [Zettelkasten](https://zettelkasten.de/). After reading [Daryl Sun's blog post](https://write.privacytools.io/darylsun/100-days-to-offload-day-4), I started looking more into the method and found it *incredibly* intriguing. I've tried the "Evernote way" of throwing everything I come across in a single place but it inevitable gets lost. I don't remember what it was called but I tried another app that actually tags your files and organises them in a nice manner. This worked well for the most part but the graphical client was badly optimised Electron and *very* heavy. I've also tried keeping notes in books but I was never really able to keep up with any of it. The thing that is especially compelling about a Zettelkasten is that I put *everything* I learn in a single text file but link around to as many different ideas as I can, drawing my *own* connections for me to rediscover later on.
16
17Because it's all in a simple text file, I'm also able to create a keybinding in [Sway](https://github.com/swaywm/sway/) that will open it in Vim, jump to the bottom, and have a nice markdown environment ready for me to write in. It did take a bit of configuration and looking around for different plugins but I'm very happy with what I have so far.
18
19The first thing is telling Vim to treat all `.md` files as Markdown
20```vim
21" Treat all .md files as markdown
22autocmd BufNewFile,BufRead *.md set filetype=markdown
23```
24
25## Visuals
26In a long text file with a great many lines, it can be useful to find your cursor quickly without having to search around the screen for it.
27```vim
28" Highlight the line the cursor is on
29autocmd FileType markdown set cursorline
30```
31
32It can also be nice to not see a ton of \[links](https\://example.com) and \*\*bold** or \*italic* text everywhere. Sure, my eye has gotten used to it but still. I'd rather have my terminal actually render bold text as bold.
33```vim
34" Hide and format markdown elements like **bold**
35autocmd FileType markdown set conceallevel=2
36```
37If you use the `vim-markdown` plugin mentioned further on, I recommend using its option for concealing rather than Vim's.
38
39## Spell check
40One of the things every good editor needs is spell check and Vim is no exception. This line enables spell check with British English for all markdown files.
41```vim
42" Set spell check to British English
43autocmd FileType markdown setlocal spell spelllang=en_gb
44```
45
46Here's a short crash course in Vim spelling commands:
47- `[s` to search for misspelled words above the cursor
48- `]s` to search for misspelled words below the cursor
49- `z=` to see replacement suggestions
50- `zg` to add the word to your dictionary
51
52## Goyo
53The very first component is something I use across *all* markdown files. [Goyo](https://github.com/junegunn/goyo.vim) is one of the first plugins I install on any machine I'll be writing with. It enables a "distraction-free writing environment" and I absolutely love it. It disables pretty much all visual elements in Vim except for what mode you're in: visual, command, insert, etc. I have a keybinding set to quickly open/close Goyo because there is an odd issue when I switch workspaces to and away from Vim. With two taps of `Ctrl+g`, it's back to normal.
54
55```vim
56nnoremap <C-g> :Goyo<CR>
57```
58
59Another line in my Vim config automatically opens Goyo for all markdown files:
60```vim
61autocmd FileType markdown Goyo
62```
63
64## vim-markdown
65That latest plugin I installed is [vim-markdown](https://github.com/plasticboy/vim-markdown) and it is *wonderful*. I really recommend reading about all of the options but here's what I have set.
66```vim
67" Configuration for vim-markdown
68let g:vim_markdown_conceal = 2
69let g:vim_markdown_conceal_code_blocks = 0
70let g:vim_markdown_math = 1
71let g:vim_markdown_toml_frontmatter = 1
72let g:vim_markdown_frontmatter = 1
73let g:vim_markdown_strikethrough = 1
74let g:vim_markdown_autowrite = 1
75let g:vim_markdown_edit_url_in = 'tab'
76let g:vim_markdown_follow_anchor = 1
77```
78
79In addition to the rest of the awesome features, the main one I wanted is the last: `follow_anchor`. With this, I can create internal links within the same markdown document and jump between them with `ge`. It also lets me open both files and URLs from within Vim and without ever having to reach for the mouse.
80
81## General Vim things
82Other, more general Vim settings that I use globally but might also be nice for editing markdown
83```vim
84" Have lines wrap instead of continue off-screen
85set linebreak
86
87" Gives Vim access to a broader range of colours
88set termguicolors
89
90" Converts tabs to spaces
91set expandtab
92
93" Use two spaces instead of tabs
94set tabstop=2
95
96" The same but for indents
97set shiftwidth=2
98
99" Keep cursor in approximately the middle of the screen
100set scrolloff=12
101
102" Disable mouse support
103set mouse=
104```
105
106---
107
108In all, I'm hoping that the work I've done today for improving my markdown workflow will help me create a more effective Zettelkasten. The *big* thing was really being able to follow internal links around because that's the main thing with keeping a Zettelkasten: following your ideas to see where they lead and discovering what connections you can make to form entirely new ideas. Mine will be stored in [Gitea](https://git.nixnet.xyz/Amolith/zettelkasten) for now but I'm thinking about putting it here at some point. It would be cool to have a map of my own mind very easily accessible from anywhere.
109
110
111
112## Edit
113### Time stamps
114```vim
115" Insert timestamp at the end of the line in this format: 20200527T113245
116nnoremap <C-t><C-s> m'A<C-R>=strftime('%Y%m%dT%H%M%S')<CR>
117```
118
119### Portable `autocmd`s
120Put all the `autocmd` lines in the `if` statement so they don't throw errors when the config is added to a version of vim without `autocmd` support
121```vim
122" Only enable autocommands when Vim supports them
123if has("autocmd")
124 ""
125 " Markdown Configuration
126 ""
127 " Spellcheck in British English
128 autocmd FileType markdown setlocal spell spelllang=en_gb
129 " Automatically open Goyo
130 autocmd FileType markdown Goyo
131 " Hide plaintext formatting and use color instead
132 autocmd FileType markdown set conceallevel=3
133 " Disable cursor line and column highlight
134 autocmd FileType markdown set nocursorline
135 autocmd FileType markdown set nocursorcolumn
136endif
137```
138
139I won't keep editing this post to provide updates on my config. Instead, I recommend looking at my ["production" version on Gitea](https://git.nixnet.xyz/Amolith/dotfiles/src/branch/master/dotfiles/pc/.config/nvim/init.vim).
140
141---
142
143This was posted as part of [#100DaysToOffload](https://100daystooffload.com/), an [awesome idea](https://fosstodon.org/@kev/104053977554016690) from [Kev Quirk](https://kevq.uk/). If you want to participate, just write something every day for 100 days and post a link on social media with the hashtag!