0

Let's say I have

// that is just a comment
// that is just a comment
// that is just a comment
// that is just a comment
// that is just a comment

And I want to type * at // to use the command cgn and then typing just . instead of classical n . getting this result:

/* that is just a comment
/* that is just a comment
/* that is just a comment
/* that is just a comment
/* that is just a comment

My solutin was setting search register manually:

:let @/='//'

Is there a faster solution? I know visual block changing but my question is just for better understanding of vim.

2 Answers 2

2

The reason that this isn't working for you is that forward slash / (ASCII 47), isn't included in your 'iskeyword' setting, which in Vim defaults to @,48-57,_,192-255.1

You could fix it by adding the forward slash to your 'iskeyword' setting but I wouldn't recommend doing this:

:setlocal iskeyword+=/

The 'iskeyword' setting is used in many places in Vim, and there's a fairly decent chance that by adding forward slash to it you will break things. e.g. Do you like being able to step through the components of a path with the w motion?

Slightly quicker than setting the search register with a :let command is just searching:

/\/\/

If you find the construction of regular expressions to be slow, you could also take advantage of the method the * uses for selecting what to search for:

  1. the keyword under the cursor |'iskeyword'|
  2. the first keyword after the cursor, in the current line
  3. the non-blank word under the cursor
  4. [...]

In order that steps 1 & 2 don't find anything, construct a line which contains no keywords, search for it, and then remove it again, by typing:

o//<Esc>*u

You can then carry on with your cgn replacements.

For more details on the 'iskeyword' setting, see :help 'iskeyword' and :help 'isfname' (the latter just for the description of the format of the former), and for more details of how * works see :help *.

1: Except on Windows, where it's slightly different: @,48-57,_,128-167,224-235

3
  • 1
    interestingly, if // is the only thing on the line, * 'works' (presumably the same is true with any symbols)
    – Mass
    Feb 26, 2018 at 23:00
  • @Mass: Yup. See the edit I was writing while you wrote your comment :)
    – Rich
    Feb 26, 2018 at 23:05
  • As I thought we ended up learning a lot more about vim with this question. We have a nice video about cgn here. Feb 26, 2018 at 23:28
1

You could use ?//<cr> which is quite fast to type. Since n would now work backwards, you'd have to do cgN if you cared about order. Or you could type /<cr> to forward-ize the search.

1
  • Searching backwards is super clever! (Might want to explain why it helps, though? I don't think it's obvious.)
    – Rich
    Feb 26, 2018 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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