3

set tw=80 wraps lines after they reach 80 characters. Sometimes when I'm coding, writing notes, etc, I want some--but not all---lines to be longer than 80, so after it wraps, I join the line back with J. Later, I come back to this 85-character line, and add some more to it. The line wraps. I join it back.

This annoying workflow would be solved if Vim only wrapped lines when crossing the 80-character threshold, instead of every time I'm in insert mode typing. It seems like there should be a way to do this. Is there?

3

You're looking for something like the t or l flag in 'formatoptions'.

t   Auto-wrap text using textwidth
l   Long lines are not broken in insert mode: When a line was longer than
    'textwidth' when the insert command started, Vim does not
    automatically format it.

The default setting is formatoptions=tcq. To test it out run :set formatoption to see if it has a t or l flag. Then run :set formatioptions-=t and you should be able to clack away well past your tw=80.

But I think the l flag is really the best option, so If the l flag is not present (the default) run :set formatioptions+=l. If the t flag is still set the line will wrap after passing tw=80, but when adding to a line already longer than 80 characters, the line will not keep wrapping automatically.

The catch to removing the t is vim will no longer automatically formats the other text and you have to run something like gqip (format inside paragraph) on the lines you want to be less than 80. There is a c flag that instead looks at comments specifically. It will wrap the comment and automatically add the comment leader for you. In conjunction with the j flag, when you go and hit J to combine the lines it will do it cleanly, meaning it will remove the comment leader from the line being appended.

run :help fo-table and :help formatoptions to find the flags and other useful info. If l didn't do the trick and removing t is the best option, you can also add something like...

highlight rightMargin term=bold ctermfg=blue guifg=orange
nnoremap <localleader>h :match rightMargin /.\%>81v/
nnoremap <localleader>H :match none<cr>

to your vimrc. Using this I can toggle on and off highlighting text in the 81st column onward, giving a nice unobtrusive way of knowing which lines are too long.

Edit: Changed the answer so the solution (in my last edit) was more front and center. the l flag was the right answer here.

| improve this answer | |
  • 1
    Thanks so much, that works perfectly! Might want to edit the answer so that the solution set formatoptions+=l is front and center – Simon Alford Jul 24 at 0:38
  • 1
    Glad it worked for you! I added the answer to the core post while trying to not completely change what I said originally. – VladislavThePoker69 Jul 25 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.