Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically slash the backslashes in vim

Tags:

regex

vim

csh

Can we write functions/subroutines in csh or vim?

Basically, my question is how to slash the backslashes inside a string automatically which we use for search in vim.

Lets say:

Contents of file file_a is:

abcd
a/b/c/d

Now, if I search 'abcd' inside vim with "/abcd" in command mode, it will match abcd(first line). And If I search for 'a/b/c/d', it will not match whole of 'a/b/c/d'. It will match only 'a' from 'a/b/c/d'.

To match whole of 'a/b/c/d', I would need to search for a\/b\/c\/d. Slashing backslashes is a pain every time you want to search for strings having backslashes inside it. :)

Have anyone of you solved this earlier?

like image 396
Hemant Bhargava Avatar asked Apr 20 '16 05:04

Hemant Bhargava


People also ask

How do I insert a backslash in Vim?

Go to the end of the line, add space + backslash, go to the next line, add space + backslash, ... repeat.

How do I escape a character in Vim?

To escape a special character, precede it with a backslash ( \ ). For example, to search for the string “anything?” type /anything\? and press Return. You can use these special characters as commands to the search function.

How do you go back one word in Vim?

Press w (“word”) to move the cursor to the right one word at a time. Press b (“back”) to move the cursor to the left one word at a time. Press W or B to move the cursor past the adjacent punctuation to the next or previous blank space.


1 Answers

In Vim:

You can search backwards, where the separator is ? instead of /, so / does not need to be escaped: ?a/b/c/d; to move to the next match downwards, use N.

Or you can set the search pattern using :let @/="a/b/c/d" (this will not move the cursor), then use n to go the next match.

You can also define your own command:

function! FindSlashed(arg)
  let @/=a:arg
  norm n
endfunction
command! -nargs=1 S call FindSlashed(<q-args>)

which you can use like this:

:S a/b/c/d

EDIT: let, not set.

like image 182
Amadan Avatar answered Oct 04 '22 14:10

Amadan