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