Vim escape backslash paste - vim

I am having the following text in vim:
Hello, \/Good\/Bye.
Now I want to change it to:
Hello, -Good-Bye.
So I use: Visual Select, Yank the Line. then
%s/<Ctrl+R>+"/-/gc
When I push CTRL+R+" The line clones into the search, but It \/ will not work. I need to change it to \\/ . Is there a quick function or keystroke to automatically escape the chars? Please let me know. Thank you!

As phd wrote in comments, you can use \V and escape() to do it. For example, write the following function:
function! Regesc(text)
return '\V' . escape(a:text, '/\')
endf
Then after yanking some text, use it like this:
:%s/Ctrl+R=Regesc(##)CR/-/gcCR
This will display and run the following command, depending of what you yanked:
:%s/\VHello, \\\/Good\\\/Bye./-/gc

You could use another delimiter after your visual selection
:'<,'>s,\%V\\/\%V,-,gc
\\ ......... scaped backslash
\%V ........ visual selection area
So you have to put your pattern \\/ inside a visual delimiter

Related

Surround Visual Text with more than 1 character

How would you surround the following text with 3 backticks ``` using tpope's Vim Surround.
All I can is 1 backtick using S` in visual mode:
This is not what you asked but this can be done without surround:
(from visual mode)
c
```
<C-r>"
```
<Esc>
See :help ctrl-r.
Define a custom surround:
(Insert following in your .vimrc or file specific config ~/.vim/after/ftplugin/markdown.vim )
" Custom surrounds
let b:surround_{char2nr('c')} = "```\r```"
now visual select and Sc will give you desired surround.
Or use a snippet solution; for example using Ultisnips define a snippet like so:
snippet code
\`\`\`${1}
${0:${VISUAL}}
\`\`\`
endsnippet
now visual select your desired lines then hit snippet expansion key ( mine is Tab ) type code and hit Tab again. that's it.
Here another ultisnips solution.
snippet code "add backtics codes" w
`!v repeat(nr2char(96),3)` ${1:markdown}
${0:${VISUAL:type here}}
`!v repeat(nr2char(96),3)`
endsnippet
If you do not want "markdown" after the first line just get rid of it. I am showing this solution only to show how to avoid backslashing so much.

I try to substitute some text in highlighted text in Vim

I try to select some text in a sentence and substitute an character in the range of the highlighted text.
e.g.
This is my masterpiece document $which$ has many dollar signs on it. $For example$
I only highlight $For example$ and try to substitute two dollars signs with '|'
I have tried the following command:
:'<,>'s/\$/\|/gc <CR>
I use 'v' to highlight in normal
But above command replaces all the dollar signs in the sentence with '|' instread
This is what I got after above command:
This is my masterpiece document |which| has many signs on it. |For example|
Does anyone have any idea what is wrong with my substitute command?
With the selection made, type
s/\%Vpattern/replace
So in your case
:'<,>'s/\%V\$/\|/gc <CR>
Note if you are doing a very magic search (\v at the beginning of the search), omit the backslash and just type %V
:h \%V explains it pretty well:
Match inside the Visual area.
Vim is not very user friendly in general and things you expect to work, like replacing inside a selection, are not automatic.
If the main thing you do with selected text is a replacement, this mapping might be useful, which always prefixes the search with \%V when you type : when visual mode is active.
" Turn on "match inside visual selection" by default when pressing
" : with text highlighted
vnoremap : :\%V

Vim: converting matching parenthesis to matching curly braces

I saw a screencast where someone had highlighted a set of parenthesis and instantly converted them to curly braces. Is this a macro or a Vim thing?
you can do it with surround.vim plugin. https://github.com/tpope/vim-surround
e.g.
when your cursor (*) at : ( fo*o), press cs({ will change your text into { fo*o}
They use vim surround plugin.
Quoting their Hello World.
It's easiest to explain with examples. Press cs"' inside
"Hello world!"
to change it to
'Hello world!'

How to substitute for matching delimiters in vi?

I have some text which has matched delimiters (in this case, curly braces, and the text happens to be LaTeX, which is only incidental):
\nb{\vec{n},\vec{y}} \in \vec{z}
What I'd like to do is globally replace \nb{...} with (...), while respecting the nesting of delimiters. I.e., the result should be
(\vec{n},\vec{y}) \in \vec{z}
and not
(\vec{n},\vec{y}} \in \vec{z)
which is what would be produced by :%s/\\nb{\(.*\)}/(\1)/g. Standard regular expressions can't handle matched delimiters, so I wasn't expecting this way to work. Is there some vi-specific trick I can use to do this?
If you have surround.vim installed then the following should do the trick
:set nowrapscan
:let #q="/\\m\\\\nb{/e+1\<cr>cs{)dF\\#q"
gg#q
If you do not:
:set nowrapscan
let #q="/\\m\\\\nb{<cr>dt{yi{\"_ca{()\<esc>\"0P#q"
gg#q
Overview
Create a recursive macro that searches for \nb{, positions the cursor just inside the {, replace the }{'s with ()'s.
Glory of Details
:set nowrapscan this prevents searches from looping back around the file.
:let #q="..." store our macro inside the q register
/\m\nb{/e+1 searches for \nb{ and positions the cursor after the {
cs{) the surround version will just change the surrounding { with )
#q run the macro again
Used " so must escape a few things so they work correctly.
gg#q go to the top of the file and execute the macro in register q
The non surround version varies a bit here
yi{ copy the text inside {'s
"_ca{()<esc> change the text inside and including the {'s and replace with ()
"0P paste what we just copied inside the ()
I would use the following :global command.
:g/\\nb{/norm!/^M%r)[{r(dF\\
Type ^M as Ctrl+V, Enter.

How can I a put a line like "==========" quickly in vim

I am editing restructuredtext files. I often need to put some charactors like "=-`~" in one line, and I want the length of the line match the previous line. How should I do this in vim?
a long long title
=================
Thanks!
Another that will work:
yypv$r=
How about yyp:s/./=/g ?
You can map that to a key, e.g.
:map <F5> yyp:s/./=/g<CR>
I would use yypver= to avoid searching & shift button as much as possible. This could of course also be mapped to a key.
If your line starts without any trailing whitespace:
Hello World
Normal Mode:
YpVr=
Gives:
Hello World
===========
Explanation
Y -> Yank entire line, like yy
p -> paste the line
V -> select whole line in visual line mode
r -> replace all of select with next character
= -> the character to replace the others
If you line has leading whitespace, eg:
Hello World
Use:
Ypv$r=
Giving:
Hello World
===========
We use v$ visual selection to the end of the line, rather than using V to select everything on the line.
If you had trailing whitespace you can use the g_ movement to get to the last non whitespace character.
When the cursor is placed on a long long line you could use something like
:s/\(.*\)/\=submatch(1) . nr2char(13) . repeat('=', strlen(submatch(1)))/
In order to make it more easy to do the substitution, I'd then use a map:
nmap __ :s/\(.*\)/\=submatch(1) . nr2char(13) . repeat('=', strlen(submatch(1)))/
So, you can underline the line where the cursor is on with typing __.

Resources