Easily comment (C++) code in vim - c++

I have looked at the following question:
How to comment out a block of Python code in Vim
But that does not seem to work for me. How do I comment code easily without resorting to plugins/scripts?

Use ctrl-V to do a block selection and then hit I followed by //[ESC].
Alternatively, use shift-V to do a line-based select and then type :s:^://[Enter]. The latter part could easily go into a mapping. eg:
:vmap // :s:^://<CR>
Then you just shift-V, select the range, and type // (or whatever you bind it to).

You can add this to your .vimrc file
map <C-c> :s/^/\/\//<Enter>
Then when you need to comment a section just select all lines (Shift-V + movement) and then press CtrlC.
To un-comment you can define in a similar way
map <C-u> :s/^\/\///<Enter>
that removes a // at begin of line from the selected range when pressing CtrlU.

You can use the NERD commenter plugin for vim, which has support for a whole bunch of languages (I'm sure C++ is one of them). With this installed, to comment/uncomment any line, use <Leader>ci. To do the same for a block of text, select text by entering the visual mode and use the same command as above.
There are other features in this such as comment n lines by supplying a count before the command, yank before comment with <Leader>cy, comment to end of line with <Leader>c$, and many others, which you can read about in the link. I've found this plugin to be extremely useful and is one of my 'must have' plugins.

There's always #ifdef CHECK_THIS_LATER ... #endif which has the advantage of not causing problems with nested C-style comments (if you use them) and is easy to find and either uncomment or remove completely later.

Related

Comment/Uncomment in Vim using Regex

I'm trying to use :'<,'>s!^!// ! to comment out highlighted code in visual mode. Is it possible to edit the regex so that it uncomments the code if it is already commented?
Some of the other answers mention plugins. I currently use NERD commenter, which provides a ci (comment invert) command that does what you want.
For starters, here are a couple of ways to avoid commenting out lines that are already commented. For simplicity, I will assume that the comment characters are all in the first column. Either one accepts a range, such as the Visual range '<,'> in your question.
:s#^\(// \)\#!#// #
:v#^// #s!^!// !
OK, if you really want a single command that toggles whether a line is commented out, then you have to capture whether the line is initially commented and then replace with an expression:
:%s!^\(// \|\)!\=strpart('// ', strlen(submatch(1)))
:help /\(
:help /\#!
:help :g
:help sub-replace-expression
A dirty but very versatile trick I use is to exploit the :normal command, that will emulate keystroke sequence you give on the given range.
For instance, to comment, select your whole lines in visual mode (using V) and enter the command :
:normal i//
This is quiet self-explanatory : i open edit mode, and // writes it (because you entered edit mode); the command will be applied to every selected line, with the cursor positioned to the beginning of the line (because we selected whole lines with V and not v; it gets tricky otherwise). With the same idea, you can remove the commenting on a visual selection using :
:normal xx
(This does exactly what it says : deletes the first two characters, as you would type on your keyboard).
There is several pros for this approach, including :
If you use several different languages with different comment signs ({ , /*, //, #, %) , it's just a matter of changing a single strokes.
You can really get creative with the normal command, since you can exploit marks for the command's range, and literally do anything :
:normal A //Hello world
Again, it feels very natural because you are simply feeding a command with the keystroke you would have used on your selection. You can even switch between modes using this trick.
well, I doubt you can do it using only one regex, and that's why Tim Pope has written an extension exactly for that:
https://github.com/tpope/vim-commentary
then all you have to do is gc to comment a line or a visual selection, and gc to uncomment it!
There are several plugins that work to comment and uncomment code across multiple languages. That being said, you would probably want to use two separate regexes in this case. For example, say I have some code...
var x = 5;
// Does stuff 5 times
for ( var i = 0; i < x; i++ ) {
// stuff
}
When I run said regex on this I don't want the already commented lines to be uncommented. So it's better to do something similar, but in reverse (e.g. :'<,'>s!^// !!).
Also, the \ze and \zs operators can be useful here to preserve whitespace. And :* is a shortcut for :'<,'>. For instance I could do
:*s!^\s*\zs// !!
This will remove only the characters // from lines that start with them or with whitespace before them.

Why does CodeBlocks 12.11, on windows, underline my comments with red zigzag lines, and how do I turn this feature off?

How do I turn off the "spell-checker like" feature in CodeBlocks on windows?
I also just now realized that if I add a "\" (back-slash) to the end of my comment, the next line if code is also commented. Has this always been standard for c++?
Mine was underlining all my comments and strings, too. Turns out when I downloaded codeblocks, the language wasn't set to English. If you look in the bottom right corner of the codeblocks window, there is a little flag. You can right click it and select the correct language. Hope this helps!
Open Code::Blocks.
Go to plugins -> Manage Plugins
Select Spell Checker and disable it.
Has this always been standard for c++?
Well, rather for the C preprocessor (which C++ uses exhaustively). Yes, it's a documented feature: the backslash-newline sequence acts as a line continuation marker (i. e., the backslash "invalidates", escapes the newline, effectively making the preprocessor treat the consecutive lines separated by backslashes as one line).
The falsely underlined words, might be caused by not having a dictionary selected. This is how I fixed it.
Click Settings->Editor->Spell Checker(on left of dialog) then under Language select a dictionary in the drop down.

Highlighting Javascript Inline Block Comments in Vim

Background
I use JScript (Microsoft's ECMAScript implementation) for a lot of Windows system administration needs. This means I use a lot of ActiveX (Automated COM) objects. The methods of these objects often expect Number or Boolean arguments. For example:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();
(CreateTextFile Method on MSDN)
On the second line you see that the second argument is one that I'm talking about. A Boolean of "true" doesn't really describe how the method's behavior will change. This isn't a problem for me, but my automation-shy coworkers are easily spooked. Not knowing what an argument does spooks them. Unfortunately a long list of constants (not real constants, of course, since current JScript versions don't support them) will also spook them. So I've taken to documenting some of these basic function calls with inline block comments. The second line in the above example would be written as such:
var a = fso.CreateTextFile("c:\\testfile.txt", /*overwrite*/ true, /*unicode*/ false);
That ends up with a small syntax highlighting dilemma for me, though. I like my comments highlighted vibrantly; both block and line comments. These tiny inline block comments mean little to me, personally, however. I'd like to highlight those particular comments in a more muted fashion (light gray on white, for example). Which brings me to my dilemma.
Dilemma
I'd like to override the default syntax highlighting for block comments when both the beginning and end marks are on the same line. Ideally this is done solely in my vimrc file, and not in a superseding personal copy of the javascript.vim syntax. My initial attempt is pathetic:
hi inlineComment guifg=#bbbbbb
match inlineComment "\/\*.*\*\/"
Straight away you can see the first problem with this regular expression pattern is that it's a greedy search. It's going to match from the first "/*" to the last "*/" on the line, meaning everything between two inline block comments will get this highlight style as well. I can fix that, but I'm really not sure how to deal with my second concern.
Comments can't be defined inside of String literals in ECMAScript. So this syntax highlighting will override String highlighting as well. I've never had a problem with this in system administration scripts, but it does often bite me when I'm examining the source of many javascript libraries intended for browsers (less.js for example).
What regex pattern, syntax definition, or other solution would the amazing StackOverflow community recommend to restore my vimrc zen?
I'm not sure, but from your description it sounds like you don't need a new syntax definition. Vim syntax files usually let you override a particular syntax item with your own choice of highlighting. In this case, the item you want is called javaScriptComment, so a command like this will set its highlighting:-
hi javaScriptComment guifg=#bbbbbb
but you have to do this in your .vimrc file (or somewhere that's sourced from there), so it's evaluated before the syntax file. The syntax file uses the highlight default command, so the syntax file's choice of highlighting only affects syntax items with no highlighting set. See :help :hi-default for more details on that. BTW, it only works on Vim 5.8 and later.
The above command will change all inline /* */ comments, and leave // line comments with their default setting, because line comments are a different syntax item (javaScriptLineComment). You can find the names of all these groups by looking at the javascript.vim file. (The easiest way to do this is :e $VIMRUNTIME/syntax/javascript.vim .)
If you only want to change some inline comments, it's a little more complicated, but still easy to see what to do by looking at javascript.vim . If you do that, you can see that block comments are defined like this:-
syn region javaScriptComment start="/\*" end="\*/" contains=#Spell,javaScriptCommentTodo
See that you can use separate regexes for begin and end markers: you don't need to worry about matching the stuff in between with non-greedy quantifiers, or anything like that. To have a syntax item that works similarly but only on one line, try adding the oneline option (:h :syn-oneline for more details):-
syn region myOnelineComment start="/\*" end="\*/" oneline
I've removed the two contains groups because (1) if you're only using it for parameter names, you probably don't want spell-checking turned on inside these comments, and (2) contained sections that aren't oneline override the oneline in the container region, so you would still match all TODO comments with this region.
You can define this new kind of comment region in your .vimrc, and set the highlighting how you like: it looks like you already know how to do that, so I won't go into more details on that. I haven't tried out this particular example, so you may still need a bit of fiddling to make it work. Give it a try and let me know how it goes.
Why don't you simply add a comment line above the call?
I think that
// fso.CreateTextFile(filename:String, overwrite:Boolean, unicode:Boolean)
var a = fso.CreateTextFile("c:\\testfile.txt", true, false);
is a lot more readable and informative than
var a = fso.CreateTextFile("c:\\testfile.txt", /*overwrite*/ true, /*unicode*/ false);

Vim different textwidth for multiline C comments?

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

To comment out matches in Vim - independent on comment leader?

I want to comment out lines in some code I have. I have different kinds of codes, and they use different comment leaders. E.g. in latex: '%', in Fortran 90: '!' and in python: '#'. I want to do a substitute command that looks something like this:
:g/<search-string>/s/^/<add-comment-leader-here>/
If this is possible, I could also make a command in Vim that automatically commented out the selected text. Something like this:
vmap <z> :'<,'>s/^/<add-comment-leader-here>/
Any ideas are welcome! :)
If you haven't seen it already, you may be interested in the NERD Commenter Vim plugin.
Check out Enhanced Commentify: I think this does what you want: it determines the comment leader based on the file type.
If you want to do it yourself, the easiest way would be to define a mapping that uses exec to build a command and include a variable that is set in your ~/.vim/after/ftplugin/c.vim and other ftplugin files. Alternatively, just add the same mapping (with a different leader) to each ftplugin file.