Vim: converting matching parenthesis to matching curly braces - vim

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!'

Related

Enclosing curly braces in parenthesis with Vim?

How to enclose curly braces in parenthesis in vim?
Initial string:
{a: b}
Final string:
({a: b})
The string possibly span multilines:
{
a: b
}
Assuming you are in normal mode and on any curly bracket character (opening or closing).
The manual/vanilla version (without any bracketing plugin) would be
c%(^R")
With:
^R meaning CTRL+R
the default register (") being filled with the content of the dictionary.
ca{ that should be used instead of c% if you're anywhere within the dictionary.
With my lh-brackets plugin, I would use v%( or vi{( -- unlike the vanilla version, will leave the default register unmodified.
With the popular surround plugin, I guess (I may be wrong as I've been using my plugin for decades) it would be something like ys%( or ysa{(.
PS: the fact your dictionary spans on several lines doesn't make any difference here.
With the vim-surround plugin you can visually select the text first e.g. va{, then surround with parentheses using S). I find it easier to remember this visual surround sequence v{motion}S<char> than the other options

Vim escape backslash paste

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

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

How to remove/add quotes?

I would like a command to remove/add quotes
"This is a text" -> This is a text
This is a text -> "This is a text"
Is there some?
You can use the popular surround.vim plugin:
yss" surrounds a line with quotes.
ds" deletes the closest surrounding quotes.
See also this tutorial for more examples of what you can do with this plugin.
Yes, go to command mode, then type :
:%s/\(^"\|"$\)//g
To quote lines :
:%s/\(^\|$\)/"/g
This performs substitution on all lines, adapt it to your needs if needed.
For deleting quotes you can simulate a new object o" meaning "outside quotes":
nnoremap do" di"viwp
Type do" in a quoted text and you'll get the text without quotes.
For adding quotes you could do (though this only works linewise):
nnoremap yo" I"<end>"<esc>
If you use surround.vim you can use yss" to quote an entire line without having to map a new command for it.

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.

Resources