neolateral

programming, drawing, photograpy etc.

Problem

I've been a Vim user for decades. While writing text or a program, I like to keep my text restricted to 80 columns such that it is visible on most terminals and text display programs. I've got a nice little vertical column displaying the end of the 80-column width in my Vim (see the last section if you would like to set it up). And then I would edit my text and try to manually format my text withing these coloumns.

I was doing this whole reformatting for a large paragraph again today and then I thought maybe I'm missing something - maybe someone has found a better way to do this! 💭. And I found this stackoverflow post vim command to restructure/force text to 80 columns!

Solution Discovered

I feel so stupid 😝now that I've discovered a way to format my text automatically within 80 columns in Vim that I felt I had to write it down and post in on my blog for posterity. Here is the sequence of magic incantations which has made my weekend so much better 😁.

  1. Set text width for document: Tell Vim that you would like to set the width of the text in this document to 80 columns - :set textwidth=80. Or even shorter :set tw=80.

  2. Use format command with movement commands: Now one can use the format command gq followed by a <vim movement command> to format the text covered in the movement.

    • For example to format the entire file - goto the beginning of the file with gg, followed by the command to format till the end of the file gqG.

    • To format just the current paragraph gq}.

Extra: Vertical Column at 80

Here's the magic incantation in Vimscript to show a vertical column after the 80th character. The text should be to the left of the column to ensure it is within the 80 column limit. Of course you can change the colours to your taste.

" 80 characters line
set colorcolumn=81
" execute "set colorcolumn=" . join(range(81,335), ',')
highlight ColorColumn ctermbg=Gray ctermfg=DarkRed