Is it possible to surround a selected part of a line with /* and */?

start COMMENT ME end
start /*COMMENT ME*/ end
  • 1
    Kind of related question – statox Dec 5 '18 at 14:13
  • 1
    Though a dupe, tpope’s commentary and surround plugins solve cases like this very effectively – D. Ben Knoble Dec 5 '18 at 18:31
  • I don't want comment an entire row, i would comment only a part of that with /**/ – Marco Andreolli Dec 6 '18 at 9:45

There is a plugin name NerdCommenter provide many ways of commenting, includes /* */: https://github.com/scrooloose/nerdcommenter

If you just need a quick solution, here is the macro which does the trick:

Update: let @j="viws/*<esc>pa */<esc>"

Let save it to your .vimrc file then using by moving the cursor at the word then @j.

  • I would comment only "foo bar" 123 foo barws/* */<80>kl<80>kl<80>kl^[p 123 – Marco Andreolli Dec 6 '18 at 12:26
  • @MarcoAndreolli checkout the update, I'm using <esc> instead of weird character, it works now. – Finn Dec 7 '18 at 8:17

The simple (redoable version, that messes * register) is

xnoremap µ s/* <c-r>" */<esc>

PS: this is quite certainly a duplicate


You can use either of the following sequences of keystrokes from visual mode to comment out the currently selected text:



(Note that the second of these is essentially the same as in Luc Hermitte's mapping. See :help i_CTRL-R if you're not sure how it works.)

Not the answer you're looking for? Browse other questions tagged or ask your own question.