Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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");
share|improve this question

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
I think there's a bug. printf(text goes here); becomes printf("text goes her"e);. –  Cristian Ciupitu Jul 12 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 at 23:35
    
For the record, I used with vim-enhanced-7.4.179-1.fc20.x86_64. –  Cristian Ciupitu Jul 12 at 23:41

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.
share|improve this answer
1  
tl;dr Install surround.vim, visually select the text then press S". –  Cristian Ciupitu Jul 12 at 22:45
2  
@CristianCiupitu ysi)" without visual mode. –  FDinoff Jul 12 at 23:13

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.