← All Posts

Vim for writers

vim |

Saturday morning gig : Try to make vim better for writing blog posts. I've already written about vim for coding in Switching to vim. It's not that great for writing though. Moving in lines is hard when they get longer as there's no automatic new line handling, navigations gets tricky with hjkl, wb of ft commands in text that contains long lines (such as blog posts). On 16:9 screens, the absence of margin makes it a strain to write long lines that span across the whole screen length. Also the cursor can be hard to find when not blinking (I know many terminals are configured that way). But make no mistake, vim gets a lot more powerful when editing a document. And that's basically it's phylosophy. It's not the topic today though.

The goal today is to edit my .vimrc file to support a blog post mode I can switch to when editing markdown as to make it a little more comfortable. The .vimrc file is available on my Github page.

The changes I want to make this morning :

  • A touch of WYSIWYG
  • Margins
  • Blinking cursor
  • Wordwrapping

WYSIWIG

This will be very opinionated, I want nothing more than code coloring for that. And any more than that is quite hard to implement in vim anyway. I'll stick with tpope/vim-markdown for the moment. I find tpopes' plugins easy to use and elegant with no big extra features. They integrate very well into vim. Other plugins like plasticboy/vim-markdown have way to many functionnalities such as automatic folding, conceiling, etc. so if it is something you might be interested in, check it out !

Margins

This one is more difficult. You'll need to learn about how vim buffers handle line wrapping and breaks. This post is a great way to start. I first set up vim to show line breaks with set list and set showbreak=> associated with line numbers, it shows soft and hard wrapping lines.

From there, two options :

  • Change column width and let the soft wrapping mechanism do it's job
  • Change textwidth and enable automatic hard wrapping for lines.

Soft wrapping

Using soft wrapping is the easiest for a better editing experience simply change :

  • set column=150

I didn't choose this option as it made long lines still hard to edit. In fact, moving in long lines that span across multiples buffer lines require using 2 key strokes, gh, gj, gk and gl. Not great.

Hard wrapping

Hard line wrapping is what I chose as it automatically creates hard new lines and makes navigation way easier. No more g keystroke, just simple vim navigation. Automatically creating new lines from while editing is not automatic, but there's a command for that. Also when setting the new textdith, you need to reformat the document. Reformating a document is done with gq, so for a whole document :

  • set textwidth=150 sets the new width that'll make lines hard break.
  • gggqG reformats the whole document after textwidth changes
  • set formatoptions+=a automatically reformats a paragraph as you insert new characters

Blinking cursor

The cursor is often hard to find in large buffers that contain a lot of characters. This one can easily be set in your Preferences panes of your terminal emulator of your choice. Mine is iTerm2.

  • In iTerm2 : preference

Wordwrapping

Vim by default does not support linebreaks. It means that when your word reaches the end of the soft line, it is cut in the middle without a - to separate it. It makes reading while editing somewhat painful. A simple option to change this is to set this parameter in your .vimrc :

  • set linebreaks

Automate

In order to automatically set textwidth and formatoptions, simply add an autocmd to your .vimrc :

  • autocmd FileType markdown setlocal textwidth=90 formatoptions+=a linebreak

Voilà, that's how I managed to configure vim for a better writing experience, it still has issues, automatic hard line creation can make reading from other text editors different. Shrinking becomes not intuitive when the buffer column length is smaller than textwidth. Multiples hard wrapped lines start to appear in the middle of the screen. That's it for today vim tips, hope you enjoyed !

← All Posts