I recently discovered the textwidth setting, which lets me configure vim to insert a "smart" whitespace-delimited carriage return as I'm typing when my line of text exceeds my specified limit.
Is there a way I can have this feature only apply when I'm typing in a non-commented block of text?
E.g.:
:set tw=20
:set formatoptions+=t
:set cino=(0
Now as I typed the below, vim auto-inserted the carriage-return and indentation between int and b, which is nice:
1 int foo( int a, int
2 b, int c )
Is it possible to configure vim to naturally allow lines 4 and 5 below?
1 int foo( int a, int
2 b, int c )
3 {
4 /* Can vim be made to allow this line even when textwidth is set to 20? */
5 int i = 5; // Can vim also be made to allow this line even when textwidth is set to 20?
6 return i;
7 }
I am specifically wondering whether the above "exceptions" to line-breaking can be done "as I type" as opposed to applying a formatting after text has been typed.
UPDATE: What I have tried:
I tried adapting an answer to this question by adding the following to my .vimrc:
augroup filetypes
autocmd!
autocmd FileType c call s:fubar()
augroup end
" Set up ft=c
fun! s:fubar()
augroup ft_c
autocmd!
autocmd CursorMoved,CursorMovedI *
\ if index(["cCommentGroup"], synIDattr(synID(line('.'), col('.'), 1), 'name')) >= 0
\| setlocal textwidth=500
\| else
\| setlocal textwidth=80
\|endif
augroup end
endfun
This did not affect runtime editing behavior in any way that I could tell. Following is a .cpp file I created after sourcing my updated .vimrc. textwidth is 80 in this output:
1 #include <iostream>
2
3 int main( int argc, char* argv[], char* envz[] )
4 {
5 std::cout << "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6 << std::endl;
7 // This is a test of some funky new autocmd stuff in my .vimrc that makes use
8 // of syntax information...
9
10 /* This is a test of some funky new autocmd stuff in my .vimrc that makes use
11 * of syntax information...
12 */
13 }
The code was hard-wrapped desirably, but both C and C++ comments were also hard-wrapped.
elsecondition besetlocal textwidth=80, would it be possible to have it set to whatever attribute was set when I do:set tw=80? I.e. use the user-specified value in theelsecondition instead of a hardcoded one? – StoneThrow Mar 08 '17 at 21:18.vimrc. Can you tell if there's an error in what I added to.vimrc? I foundcCommentGroupin/usr/share/vim/vim74/syntax/c.vim, which is what I think I need. I've never used autogroup or anything else in your solution, so I'm unclear if I have a syntax or any other kind of error.source'ing the modified.vimrcdidn't report errors. Thanks if you can help any more. – StoneThrow Mar 08 '17 at 23:55