vim-as-a-markdown-editor.md

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