Re: Commenting a selection/visual block in ViEmu

Hi, I want to be able to comment a block of XML (App.config) in ViEmu but can't figure out how to do it in vi(m) commands. I tried using the existing <CTRL>K <CTRL>C on a selection but it didn't work in NORMAL or INSERT mode. Anybody else have this problem? I have no other add-ins installed, Visual Studio 2003.


Re: Commenting a selection/visual block in ViEmu

Hello - I will check it out and get back to you. It may have to do with it being XML (different language services are used for different file types), or it may be a problem in ViEmu.

Thanks for the feedback,



Re: Commenting a selection/visual block in ViEmu

I came up with a basic search & replace for XML comments that I can use with visual mode:


which I could map to a key. However, it would be preferable to comment xml with <!-- at the start and --> at the end. Is there a regular expression for the beginning and ending of a visual block selection?


Re: Commenting a selection/visual block in ViEmu

There is no specific regex for a visual selection. You can only issue ex commands in normal mode, but the '< and '> marks refer to the latest visual selection beginning and end. But ex commands mostly work only linewise (ex ranges are linewise, etc...).

A VS macro should help in fixing it, but, in any case, I will have a look at the VS commands and try to fix it - the problem may also break perfectly good VS macros.




Re: Commenting a selection/visual block in ViEmu


I've checked it, and it turns out it's a limitation of the XML language service in VS.NET 2003 itself. Even with ViEmu disabled, the command doesn't work (while it works with other file types).

The only solution I can think of is a bit involved: it requires a ViEmu key mapping and a Visual Studio macro. I'm not a macro wizard, but I guess it's pretty easy to write a macro to comment the current selection. Let's say the macro is called "CommentXML" and is stored in MyMacros.Module1. The following mapping should make Control+Shift+C do what you want:

  :vnoremap <c-s-c> gS:macro MyMacros.Module1.CommentXML<cr>

vnoremap makes the mapping only work in visual mode, 'gS' is the command to turn the current visual range in the Visual Studio selection and :macro calls a macro.

Ok, I bit the bullet, and here is my attempt at a CommentXML macro:

    Sub CommentXML()
        Dim sel As TextSelection = DTE.ActiveDocument.Selection
        Dim Block As String = sel.Text()
        Dim cmt As String

        cmt = "<!--" + Block + "-->"
        sel.Text() = cmt
    End Sub

It works, but it's not great (multiple undo steps, it deletes and re-inserts the text, etc...). I'm not familiar with the macro model, and I wasn't able to find a better solution - hopefully someone else can.

HTH and best regards,


PS: Of course, you can just map the macro directly to a keypress using VS, but if you want to use ViEmu's vim-like selection model, a mapping will work best as it will take care of converting the visual range into the selection


Re: Commenting a selection/visual block in ViEmu

Should I use this for un/commenting c++ code?
or this solution is just for the mentioned file type?

BTW I used to get this feature from visual assist before I installed viEmu, but it stopped working
after that.

BTW2 you might be able to use/bind the visual assist un/commenting feature instead of the macro.
that is of course if you have visual assist. (which is a must IMHO)


Re: Commenting a selection/visual block in ViEmu

Hi kroiz, no, this is meant just for XML. Commenting C++ should work fine using VS's built-in support.

I've tested it on VS.NET 2003 without Visual Assist, and it works fine. If you are in visual mode, you usually want to use 'gS' to turn the current visual range into VS's selection before issuing the command, else just the whole current line is commented.

Can you verify whether using the menu to issue the command (Edit->Advanced) works? If the keypress doesn't work, but the menu works, it's a keybinding problem. If not even the menu item works, it may be something else.




Re: Commenting a selection/visual block in ViEmu

I did not know that VS could comment and uncomment. (it works)
but what I wanted, but I figured that it is not possible is visual assist feature that let you comment uncomment a selection by pressing / or *.
but I guess these keys has different use in vi so it is not possible.
I will just use what VS gives me.
cause Ive seen some crazy stuff:


Re: Commenting a selection/visual block in ViEmu

Hi kroiz,

The macros there use




that is, two keypress sequences for commenting the current line/selection. They wouldn't work now in ViEmu because I haven't yet implemented keychord mapping (assigning a mapping to a multiple-keystroke sequence). I plan to implement that in a future version.

The ones which use vim's scripting language are farther out of reach, and I'm not sure it would make sense to implement 'viml'. But VS's macro system can be used from ViEmu, and a possible fix would be to allow external access to ViEmu's internal variables (registers, marks, etc...)

Thanks for the feedback and the pointer,



Re: Commenting a selection/visual block in ViEmu

no no no, Thank YOU
I am already very satisfied with the current implementation. (-:


Re: Commenting a selection/visual block in ViEmu

Great. In any case, I plan to implement that support as it is very useful for specific commands, without requiring weird key combinations. And also for completeness-sake.