6

I have a following line in one script:

cp "$uploaddir"/"$file" "$cvsdir"/"$device"

I would like to search if this exact line is present elsewhere in the script. Usually I would do yy and then /Ctrlr0. However, this time the line contains forward slashes. I also tried to specify "very nomagic"(\V) after the forward slash, but this didn't help. I could obviously do /cp "$uploaddir"\/"$file" "$cvsdir"\/"$device", but this is bit clunky. Is there a better way to search line from text which contains forward slashes without escaping each forward slash?

3 Answers 3

11

Use backward search :h ?. This way you type ? instead of / to start search, but this also allows to use / in search pattern.

1
  • This is so simple it's brilliant!
    – Randall
    Commented May 8, 2018 at 19:29
1

You need to escape the forward slashes, too, not just the backslashes that are the only regular expression pieces requiring escaping when \V is used. This can be done with the escape() function, and inserting the register via the expression register:

y$/<C-r>='\V' . escape(@@, '/\')<CR><CR>

There's still an issue with newlines (that's why I used y$ instead of yy), and the expression already is too long to type regularly. And it clobbers a register.

That's why I would recommend to use one of several available plugins that implement such a literal search of the visual selection. The visualstar plugin does this, and my SearchHighlighting plugin includes such functionality as well. (The plugin page has links to additional alternative plugins.)

1

You can use grep -F to search for literal fixed strings. If you run grep from vim, the matching lines are placed in the quickfix list. You can use :cnext and :cprevious to navigate the results.

:gr -F 'cp "$uploaddir"/"$file" "$cvsdir"/"$device"' %

Your Answer

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

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