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