I want to substitute all cygdrive/e with cygdrive/d using vim. But I can't get the matching pattern correctly. Here is my command:
s/cygdrive\/e/cygdrive\/d/g
it doesn't work. Can anybody show me what is wrong?
Thanks,
vim allows you to specify the delimiter.. (First character after s is the delimiter)
s/cygdrive\/e/cygdrive\/d/g
using line range argument .. and # as delimiter
ESC:
:1,$ s#/cygdrive/e#/cygdrive/d#g
Your search pattern and replacement string look fine.
Make sure you are in ex mode when you try it.
So press ESC, then : and then
%s/cygdrive\/e/cygdrive\/d/g
But if you want all he replacements in just the current line you can do:
s/cygdrive\/e/cygdrive\/d/g
You have to escape the special character
like this
s/cygdrive\/e/cygdrive\/d/g
Related
I have a strange character in a text, how can I find all of its occurrences
and replace it with another character?
I know that if you need to do the same thing with a word you can use * and then :%s//replacement/g to replace all occurrences,
is there something similar for characters?
Yank the character into a register (a in this case): "ayl
Start your substitute command :%s/
Use <C-r> a to paste the register into the command line.
Finish your command and execute: :%s/<char>/<new-char>/g
Do you know how to type out the character? If so you can do :%s/<char-to-replace>/<char-to-replace-with/g
Use the command:
%s/a/A/g
which replaces a with A. For some more examples of the %s command take a look here.
I tried to replace '&=' with '=&' in Vim, but the command :%s/&=/=&/g
replaced '&=' with '=&='.
Why did that happen ?
What command should I use ?
Just escape the ampersand.
:%s/&=/=\&/g
You need to escape it because & is a special character in the replacement that will be replaced with the entire matched pattern.
I need to use find new lines (\n) in a .txt file and replace them with a backspace (\b). Using this in the Find and Replace feature of Notepad++ successfully finds the new lines, (\n), but replaces them with the characters "\b" instead of applying a backspace. How can this be done correctly?
\r\n should work.
replace them with empty field and you will get your backspace(\b) equivalent
A simple way would be Ctrl + J or Edit->Line Operations->Join Lines
I was also trying to replace \n with backspace, but the above solutions seems neat. Maybe it helps someone.
(I tried the \r\n solution and it seems both works the same way :) )
In notepad++,
Go to Edit menu, line operations and then click on "Remove Empty Lines"
You get what you are trying with regular expression.
Removing empty lines
Use regular expressions, in Find what:, enter \r\n and in Replace with: leave that blank.
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/
When I do a search and replace on a yanked line that has characters that require escaping it's really annoying to have to go back through the command line string and escape all the special characters.
Is there a way to use a literal (raw) string modifer like in python: str = r'\unescaped\'?
Use the \V modifier:
:%s/\V$foo//g
See also :help /magic
I just thought of doing this instead: :%s;\foo\;bar;g. This solves the escape the backslash problem Manni points out.