How to remove/add quotes? - vim

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.

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

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.

In vim, is there a plugin to use % to match the corresponding double quote (")?

The % key is one of the best features of vim: it lets you jump from { to }, [ to ], and so on.
However, it does not work by default with quotes: Either " or ', probably because the opening and closing quote are the same character, making implementation more difficult.
Thinking a bit more about the problem, I'm convinced that it should be implemented, by counting if the number of preceding quotes is odd or even and jumping to the previous or next quote, accordingly.
Before I try to implement it myself, I'd just like to know if someone already has?
Depending on your reason for needing this, there may be a better way to accomplish what you're looking for. For example, if you have the following code:
foo(bar, "baz quux")
^
and your cursor happens to be at the ^, and you want to replace everything inside the quotes with something else, use ci". This uses the Vim "text objects" to change (c) everything inside (i) the quotes (") and puts you in insert mode like this:
foo(bar, "")
^
Then you can start typing the replacement text. There are many other text objects that are really useful for this kind of shortcut. Learn (and use) one new Vim command per week, and you'll be an expert in no time!
Greg's answer was very useful but i also like the 'f' and 'F' commands that move the cursor forward and backward to the character you press after the command.
So press f" to move to the next " character and F" to move to the previous one.
I have found this technique very useful for going to the start/end of a very long quoted string.
when cursor is inside the string, visually select the whole string using vi" or vi'
go to start/end of the string by pressing o
press escape to exit visual select mode
this actually takes the cursor next to the start/end quote character, but still feels pretty helpful.
Edit
Adding Stefan's excellent comment here which is a better option for anyone who may miss the comment.
If you use va" (and va') then it will actually visually select the quotes itself as well.
– Stefan van den Akker
I'd like to expand on Greg's answer, and introduce the surround.vim plugin.
Suppose that rather than editing the contents of your quotes, you want to modify the " characters themselves. Lets say you want to change from double-quotes to single-quotes.
foo(bar, "baz quux")
^
The surround plugin allows you to change this to
foo(bar, 'baz quux')
^
just by executing the following: cs"' (which reads: "change the surrounding double-quotes to single-quotes").
You could also delete the quote marks simply by running: ds" (which reads: "delete the surrounding double-quotes).
There is a good introduction to the surround plugin here.
I know this question is old but here is a plugin to use % to match the corresponding double quote:
https://github.com/airblade/vim-matchquote

Resources