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?


: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 FileType c call s:fubar()
augroup end

" Set up ft=c
fun! s:fubar()
  augroup ft_c
    autocmd CursorMoved,CursorMovedI *
          \ if index(["cCommentGroup"], synIDattr(synID(line('.'), col('.'), 1), 'name')) >= 0
          \| setlocal textwidth=500
          \| else 
            \| setlocal textwidth=80
  augroup end

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>                                                                                                                                                                                                                                                         
  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...                                                                                                                                                                                                                                               
 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.

  • 1
  • This looks promising. What would a regex be to check "I am currently within a C or C++ comment" though? I'm not great at regex. – StoneThrow Mar 8 '17 at 21:15
  • I wouldn't use a regexp at all, but rather check the syntax group. I think this will work (see my answer on "Line number specific text-width setting"). – Martin Tournoij Mar 8 '17 at 21:17
  • One more question: instead of having the else condition be setlocal 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 the else condition instead of a hardcoded one? – StoneThrow Mar 8 '17 at 21:18
  • @Carpetsmoker - In case you are still able to help, I tried adapting your solution, but it didn't affect vim behavior in any way I could tell. I updated the post with what I added to my .vimrc. Can you tell if there's an error in what I added to .vimrc? I found cCommentGroup in /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 .vimrc didn't report errors. Thanks if you can help any more. – StoneThrow Mar 8 '17 at 23:55

Your Answer

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

Browse other questions tagged or ask your own question.