Vi and Vim Asked by Simon Alford on December 20, 2020
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?
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.
Correct answer by VladislavThePoker69 on December 20, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP