Vim enclose in quotes

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.

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.


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.

Tags:

Vim