21

In our C++ code base we keep 99 column lines but 79-some-odd column multiline comments. Is there a good strategy to do this automagically? I assume the modes are already known because of smart comment line-joining and leading * insertion.

cdleary
  • 69,512
  • 53
  • 163
  • 191
  • You can set a custom `formatexpr` or `formatprg`, however this will require writing a vim plugin or an external formatter programm, see help for details. I do not know any plugins that will do this for you, but I am not sure that they does not exist. – ZyX Aug 13 '10 at 18:58
  • Would you mind either posting sample code or providing a link some code that you want formatted? No promises, but I might be able to write a simple formatting program. – Christopher Bottoms Aug 18 '10 at 12:47
  • @molecules: I'm not looking for a formatting program, just something that automatically knows my textwidth is 79 instead of the normal 99 when I'm typing within a multiline comment. – cdleary Aug 18 '10 at 22:34

2 Answers2

17

Apparently both code and comments use the same textwidth option. As far as I can see, the only trick is to set this option dynamically:

 :autocmd CursorMoved,CursorMovedI * :if match(getline(.), '^\s*\*') == 0 | :setlocal textwidth=79 | :else | :setlocal textwidth=99 | :endif

Here the critical part is detecting when we are in a comment. If you only format comments this way:

/*
 * my comment
 */

my regex should work... unless you have lines in the code starting with * (which I guess can happen in C, less frequently in C++). If you use comments like this:

// comment line 1
// comment line 2

the regex is even simpler to write. If you want to cover all possible situations, including corner cases, well... I guess the best thing would be to define a separate detection function and call that from the :autocmd instead of match().

UncleZeiv
  • 18,272
  • 7
  • 49
  • 77
  • +1, I would have posted a similar proposition but it would've been identical to this one. – mike3996 Aug 19 '10 at 15:39
  • What kind of impact does this have on performance? If I am SSHed into a server, might this noticeably increase my latency? – Keith Pinson Feb 28 '13 at 00:03
  • It won't @Kazark, all the logic is performed inside Vim (i.e. server side). Having said that, it will affect the performance the same way it's affected if you were using it locally (i.e. not through SSH) – Juan Campa Feb 04 '14 at 23:10
1

I came across this same problem and think that I have found a suitable solution.

What I wanted my comments to word wrap so that when I'm typing I don't have to worry about formating text. This works well with comment text. But I wasn't comfortable with having vim format my code. So I wanted vim to highlight every thing in red after x column.

To do this with only cpp code you would add the following to your ~/.vim/ftdetect/cpp.vim file.

set textwidth=79
match ErrorMsg '\%>99v.\+'

note: You may have to create the file and folders if they don't exist.

If you have problems with this make sure that you have formatoptions set to:

formatoptions=croql

You can see this by running :set formatoptions inside of vim.

  • Sorry, but no -- this is the same solution I got to this question that I asked: http://stackoverflow.com/questions/235439/ Highlighting junk all over the place is much less finesse than I'm hoping for! – cdleary Aug 18 '10 at 08:14