I'm editing a shell to be used as a here document. I want to replace the $ dollar signs with \$. How can I do this when vim uses $ as a line ending.
cat > goodband.txt <<EOF
function bestband
{
local JON=$1
local BON=$2
}
EOF
I want to replace with
cat > goodband.txt <<EOF
function bestband
{
local JON=\$1
local BON=\$2
}
EOF
I went into visual mode, highlighted the block and tried :s/$/\$\g. But I highlighted and replaced the line endings.
Basic Find and Replace In Vim, you can find and replace text using the :substitute ( :s ) command. To run commands in Vim, you must be in normal mode, the default mode when starting the editor. To go back to normal mode from any other mode, just press the 'Esc' key.
By pressing ctrl + r in visual mode, you will be prompted to enter text to replace with. Press enter and then confirm each change you agree with y or decline with n .
When you want to search for a string of text and replace it with another string of text, you can use the syntax :[range]s/search/replace/. The range is optional; if you just run :s/search/replace/, it will search only the current line and match only the first occurrence of a term.
Open ~/.vimrc and check its contents. If you see a line like this: set list. It means, it will display $ in every line to mark the end of line. Either remove it or use :set nolist command in the vi editor.
$
is a special marker for the end of line. If you want to replace literal $
characters, you need to escape them, such as moving to the start of the JON
line and entering:
:.,.+1s/\$/\\$/g
(current line and next).
For affecting the visual selection in the case where it's not so easy to work out the line range, you can just use the '<
and '>
markers:
:'<,'>s/\$/\\$/g
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With