22

Say I have the following

printf(text goes here);

and I select "text goes here" in visual mode...
How do I quickly change it to

printf("text goes here");
2

9 Answers 9

19

This is intended to answer the specific question that you asked. You state that you have visually selected some text and want to surround it with quotes. To do that, run:

:s/\%V\(.*\)\%V/"\1"/

To break that into parts:

  • : allows you to enter ex commands.

  • s/old/new/ is the usual substitute command.

  • \%V is an under-documented atom to mark the beginning of the selected text

  • \(.*\) selects everything and save it into selection 1.

  • The second \%V signifies the end of the selected text.

  • The replacement text is everyting that was selected, which is stored in \1, surrounded by quotes: "\1".

This command applies line by line. So, you may get unwanted results if the selected text extends over multiple lines.

7
  • 1
    I think there's a bug. printf(text goes here); becomes printf("text goes her"e);. Jul 12, 2014 at 22:50
  • @CristianCiupitu It is not clear to me if that is a vim bug or not. In either case, a simple workaround is to highlight one extra character.
    – John1024
    Jul 12, 2014 at 23:35
  • For the record, I used with vim-enhanced-7.4.179-1.fc20.x86_64. Jul 12, 2014 at 23:41
  • 1
    @CristianCiupitu @John1024 %V just matches the selection up to before the cursor, therefor if the e is the last character in the selection, then the second %V is not inside the selection and ignores the last e. Oct 14, 2015 at 20:11
  • 2
    @John1024 the real work around would be to use the more appropriate '<,'> or in a pinch include a . after the second \%V Oct 14, 2015 at 20:15
27

You should research more. What Vim command(s) can be used to quote/unquote words?

Quoting:

surround.vim is going to be your easiest answer. If you are truly set against using it, here are some examples for what you can do. Not necessarily the most efficient, but that's why surround.vim was written.

  • Quote a word, using single quotes ciw'Ctrl+r"'
    • ciw - Delete the word the cursor is on, and end up in insert mode.
    • ' - add the first quote.
    • Ctrl+r" - Insert the contents of the " register, aka the last yank/delete.
    • ' - add the closing quote.

  • Unquote a word that's enclosed in single quotes di'hPl2x
    • di' - Delete the word enclosed by single quotes.
    • hP - Move the cursor left one place (on top of the opening quote) and put the just deleted text before the quote.
    • l - Move the cursor right one place (on top of the opening quote).
    • 2x - Delete the two quotes.

  • Change single quotes to double quotes va':s/\%V'\%V/"/g
    • va' - Visually select the quoted word and the quotes.
    • :s/ - Start a replacement.
    • \%V'\%V - Only match single quotes that are within the visually selected region.
    • /"/g - Replace them all with double quotes.
3
  • 15
    tl;dr Install surround.vim, visually select the text then press S". Jul 12, 2014 at 22:45
  • 5
    @CristianCiupitu ysi)" without visual mode.
    – FDinoff
    Jul 12, 2014 at 23:13
  • that's a different question
    – CervEd
    Feb 9, 2023 at 8:46
23

The command sequence cw""<ESC>P is the close thing that can do a surrounding of text and works by cutting the word (string of alphanumeric characters: a-z, A-Z, 0-9, including the _ [underscore]), then inserts the text "", and hitting the escape key (<ESC> to escape out of insert mode one then is able to paste before the cursor. Thus the command breaks down to:

c - cut into register

w - regex match \w

"" - insert two " characters at current cursor position

<ESC> - VIM shorthand for hitting the escape key. In this context, return to command mode

P - paste current register

However, in the case of longer sets of strings on the current line that one wishes to surround, one would need to use substitution regex commands with a capture group such as:

:s/\(\w\+\)/"\1"/g

This command stores the resulting matched regex captured as group '1' and preform substitution to insert the group's contents inside your quotes. Thus, given the text:

fubar: 1

Becomes:

"fubar": 1

The vim intro help file is one of the best resources for anyone, along with the :help command.

2
  • 1
    This works great with visual mode as well
    – predikt
    Mar 29, 2016 at 17:52
  • 3
    I think a small improvement is to use ciw""<ESC>P because you can be with the cursor in the middle of the word and you can do "change inner word". Dec 4, 2017 at 23:45
3

Here's how I quoted a few of my visual selections:

  1. While in VISUAL mode select the text you need to quote, then press qqc""<Esc>Pq

    • qq begins recording your actions into record q
    • c puts selected text in a register and switches to INPUT mode
    • "" puts symbols you need to enclose the text with
    • <Esc> switches to NORMAL mode
    • P puts the text from the register in the cursor position, in-between quotes
    • q denotes the end of actions recording
  2. Now, whithin the same session, you will be able to quote any visual block by just typing @q, which playbacks our previosly recorded actions

  3. ???

  4. After using @q once you will be able issue @@ (repeat previous action) to quote subsequent visual selections

1
  • What is step 3? Please fix you answer.
    – DavidPostill
    Feb 5, 2017 at 10:13
2

Don't use visual mode. Simply change the text in the parens to "", and then paste the deleted text in between. ci)""<Esc>P

If you want to use visual mode, it works the same, you just drop the 'i)' part: c""<Esc>P

(<Esc> means hit the escape key)

2

Using the plugin surround.vim which seems to be enabled by default in modern Vim installations (at least in Ubuntu 20):

  1. Visually select text to be surrounded
  2. Type S" to surround the text with double quotes

Taken from Christian Ciupitu's excellent comment which is unfortunately buried down under the last question.

2
:s/\(\%V\(.*\)\%V.\)/"\1"/

Based on the other answers, %V matches up to one character before the end of the selection. So an extra char matching is included.

1

The solution using vim-surround plugin https://github.com/tpope/vim-surround

ysiw"
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 4, 2022 at 2:55
0

I use surround.vim

cs"' or cs'" -> change surroundings <from> - <to>

Surround.vim is all about "surroundings": parentheses, brackets, quotes, XML tags, and more. The plugin provides mappings to easily delete, change and add such surroundings in pairs.

https://github.com/tpope/vim-surroundl

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .