LInux vi editor,how to replace special character? - linux

I wrote some text in vi editor,such as
xxx
xxx.g/gasd/ga
gasdj\gqweg/fsda
fg-cgdohd
now I want to replace the "/g" to "abc"
how to escape it
I wrote
:%s/'/g'/abc/g
in command line,it's no working,how to do it?

You can use any character, not just a slash, in your search & replace:
%s:/g:abc:g

Related

How do I create files with special characters in Linux?

I am using the touch command to try and create a file with the name "\?$*'KwaMe'*$?\" (quotation marks included as part of the file name). However when I type touch "\?$*'KwaMe'*$?\" in the Terminal, it doesn't give me the result I am expecting. How can I create this file?
You need to escape special characters with the backslash symbol (\).
This command will create a file named "\?$*'KwaMe'*$?\":
touch \"\\\?\$\*\'KwaMe\'\*\$\?\\\"
Explanation
Double your \, like this: \\, so that your shell does not interpret the backslashes from your filename as escape characters.
Escape " and ', like this: \", \', so that your shell interprets the double quotes as part of the filename.
Escape $, like this: \$, otherwise your shell will think you're using a variable.
Escape ? and *, like this: \?, \*, to prevent filename expansion.

How do you replace a line with forward slashes in it in EX?

I'm running a script for vim EX mode I've tried every escape character and word identifier I can find.
it needs to find the string "/etc/walker" and replace it with "/etc/runner"
% s/\</etc/walker\>/\</etc/runner\>/g
wq
same issue with a script to append at the end of the file. It doesn't do anything. I'm trying to append "/etc/walker"
$
a
\</etc/walker\>
.
wq
what I've tried on regex editors seems to work there but not in EX
Thanks for your help
Try this:
:s#/etc/walker#/etc/runner#
Notice the use of # as a delimiter, that way you don't have to add back slashes.
You could also use:
:s#/etc/walker#/etc/runner#
For appending at the end of the line:
:s#$#/etc/walker#
In EX mode just remove the : at the beginning.

How to substitute text which has implicit meaning in vi

I have a C code in vi from which I want to replace the text "\n" with something else. Use of %s:\n:(anything else) isn't working. Is there any way to bypass this?
For example,a line in the code is: printf("Hello world\n");
In vi, I need to replace "\n" in the printf line with a full stop. Use of substitue command (%s:\n:.) will not work for "\n"
Please let me know what is the command to replace 'text' which also has some implicit meaning in vi (here \n is newline is vi as well).
Thanks in advance!
You need to escape the backslash in \n, like this:
%s:\\n:.

Searching for \[ in VIM

I have a TeX file that contains lines like
\[ De = 0 \]
Now my boss wants equation numbers so I want them to be like
\begin{equation}De = 0\end{equation}
I tried the following command by escaping the backslash with a backslash.
:%s#\\[#\\begin{equation}#gc
I think this should work but I am getting pattern not found error.
I have looked at
How to include forward slash in vi search & replace
and
http://vim.wikia.com/wiki/Search_and_replace
Can somebody tell what am I doing wrong?
You have to escape both characters: :%s_\\\[ _\\begin{equation}_g.
EDIT: Since you also asked for an explanation:
Why escape the \?
Vim supports different pattern matching styles (see :help /magic), but escape characters have to be escaped in all of them.
Why escape the [?
[] in patterns are used as collections. Thus you have to escape the [ to match it literally.

Merge the current line with the line above in Vim

I have a pretty long list, like so:
itemOne
itemTwo
,itemThree
itemFour
itemFive
,itemSix
,itemSeven
Each line that starts with a comma needs to be merged with the line above, like so:
itemOne
itemTwo,itemThree
itemFour
itemFive,itemSix,itemSeven
How can I do this?
An efficient way is to use the following :global command:
:g/^,/-j!
You could do it with:
:%s/\n,/,/g
Try this substitution:
:%s#\n\ze,##
Explanation:
%s# begin a substitution on all lines.
\n match the newline character.
\ze, set the end of the match before the comma so the comma will not be replaced.
## replace with nothing (to remove the newline character).

Resources