Vi search and replace with slashes - linux

I am trying to run this command in vi
:s/href="\//href="http:\/\/website.com\/folder\/subfolder\//g
but got this error E486: Pattern not found: href="\/
What am i doing wrong?

That error means pretty much what it says. vi didn't find any pattern href="/ (ignoring escapes) in your file.
Sometimes it's easier to use something besides / for the search delimiter if your search has a lot of slashes, so you don't need to escape them all. Try replacing the / delimiter with # instead, like this:
s#href="/#href="http://website.com/folder/subfolder/#g
Then maybe you can more easily see what's wrong with your pattern:

becouse there are many '/' chars, try use another delimiter, ex ',':
:s,some/pattern/with/slashes,new/string,g

On another note. That substition worked for me. Just copied and pasted. Are you on the same line that you are trying to perform the substitution on? the 'g' is meant globally on the line you are on. If you need to perform the search and replace on the file the use :%s/

Related

How to replace one character with mutliple characters using search and replace in vim

In vim, I find myself searching for a character and in command mode doing x followed by 10p or 30p or something like that fairly often. I was wondering if there is a regular expression version that I could use in the :s/.../.../ command. \{m} work for matching m counts of of characters in the seach, but its equivalent s/X/X\{10}/gc replaces with the literal stringX{10}.
I've been searching the documentation and haven't found out how to do it yet, if anyone knows of a way I'd appreciate knowing about it.
You can use :h sub-replace-expression like this
%s/X/\=repeat(submatch(0), 10)/gc

E486: Pattern not found

I need to repoint a mount from /IsilonAsperaVOD/data to /Isilon/asperavod/data. Using vim I do:
:s/IsilonAsperaVOD/Isilon\\/asperavod/g
which returns the error. I can type out the path or copy/past with same effect...? how do I do this
You don't need :global (as in your own answer), prefixing a :% range for the entire file will suffice (without that, the :s only applies to the current line).
The problem with your original attempt is that you've escaped twice; there needs to be a single backslash in front of the /:
:%s/IsilonAsperaVOD/Isilon\/asperavod/g
But, it's better to avoid the escaping altogether by using a different separator character:
:%s#IsilonAsperaVOD#Isilon/asperavod#g
this worked
:g/IsilonAsperaVOD/s//IsilonDenver\/asperavod/g
so, single escape for directory slash, and g (global) and then :s, instead of :s (replace) even with %

vim how to search for URL

How would you search for the following string in vim?
http://my.url.com/a/b/c
I've tried (a la Very No Magic)
:/\Vhttp://my.url.com/a/b/c
But it gives me:
E492 not an editor command: /\Vhttp://my.url.com/a/b/c
You would think there'd be a simple way to search a string literally... I'm not too interested in slash escaping every slash, or writing a complicated search, because I have to rapidly search different URLs in a text file.
I'm not sure why you get not an editor command since I don't. The simplest way to search without having to escape slashes is to use ? instead, e.g.
:?http://my.url.com/a/b/c
" or since the : is not necessary
?http://my.url.com/a/b/c
This does search in the other direction, so just keep that in mind
another way to search forward (from the position of your cursor) without escaping is use :s command.
you could do:
:%s#http://my.url.com/a/b/c##n
then press n to navigate matched text forward, N backwards
If you want to know how many matches in the buffer, use gn instead of n
note that, I said "without escaping", I was talking about the slash, if you want to do search precisely, you have to escape the period. .. since in regex, . means any char.
Can also set the search register directly.
:let #/='\Vhttp://my.url.com/a/b/c'
Then you can use n and N like normal.
Use MacVim (or GVim). Open the non-regex GUI search using ⌘f (or ctrlf on Windows). This is the recommended way to do a non-regex search in Vim. GUI Vim has many improvements over terminal vim like this one and I would highly suggest using it full time if you aren't already.
Searching in vim is just /, not :/. You can search for that string escaping only the slashes: /http:\/\/my.url.com\/a\/b\/c

Trouble searching in VIM with file having lots of slashes and dots

I've got a bunch of really big files that contain lines like:
convert ./1776/07/04/T915002.tif ./1776/07/04/T915002.png
rm ./1776/07/04/T915002.tif
convert ./1776/07/04/T918012.tif ./1776/07/04/T918012.png
rm ./1776/07/04/T918012.tif
What I want to do is search for say /1861/ meaning a line for the year 1861
But vim seems to interpret that 'go to line 1861, which is not at all what I want.
Searching for the ./1861/ fails too. I assume the leading dot tried to match any character. Trying to backslash quote it fails too. Clearly I'm missing a fundamental idea or two.
Thanks
You need a backslash to escape the dot or the slash:
/\.\/1861\/
Use ? to search backwards. This means you don't need to escape the /s.
?/1861/

vim does not find and replace simple phrase that is clearly present

I have a simple vim problem that Google hasn't managed to help me with. Any thoughts are appreciated.
I do the following search and replace:
:s/numnodes/numnodes1/g
On a file containing the following text:
numprocs=0
numnodes=0
I get
E486: Pattern not found
The position of the green square which indicates where I'd start typing is clearly above the pattern. I tried searching for other short phrases not involving regex, which are also present, which also fail. A simple /numnodes highlights matches as expected. Does anyone have any idea what might be the matter with vim?
Try :%s/searchphrase/replacephase/g
Without the % symbol Vim only matches and replaces on the current line.
try using this:
:%s/numnodes/numnodes1/g

Resources