2

I want to surround a word with quotes

Here is my set of commands:

  • ciw
  • then when I am in insert mode, I type ' (I use the vim
    extension on Visual code so I get two ' I do not know if it the case for "normal" vim)
  • then Ctrl+C+t
  • then p

It works, but there may be a shorter version to achieve this. Could you help ?

2
2

You can give a try to vim-surround plugin. To put a word into quotes you will go ysiw'. The plugin gives you the power to:

  • add (ysiw")

    word -> "word"
    
  • change (csiw"')

    'word'
    
  • delete (ds')

    word
    

the surroundings not only of sigle words but of all kinds of text objects:

  • surround words inside parentheses (ysi)")

    def func(some sequence of words) -> def func("some sequence of words")
    
  • change parentheses to square brackets (cs)])

    list = (var1, var2, var3) -> list = [var1, var2, var3] 
    
2
  • 1
    I do mention vim-surround in my answer, FYI. – B Layer Dec 22 '20 at 14:01
  • Aaaand it gets accepted. Where did I put that darn bottle of Valium.... – B Layer Dec 22 '20 at 15:29
2

If you're using the VS Code extension that I think you are then my understanding is that it supports at least some plugins and one of those is vim-surround. If that's the case and you're open to using plugins just jump to the last paragraph. Otherwise, read on.

From Normal mode with the cursor on the first character of the word you could do...

i"<C-O>f<Space>"<Esc>

Ctrl-O, from Insert, let's you enter one Normal mode command before returning to Insert.

f<Space> goes to the next space character on the line (after the word).

Problem with this is if the word is not followed by a space (e.g you are at the end of the line or it's followed by a punctuation mark). You'd need to adjust the Normal mode command (e.g. to f. or $<Left>).

That's just one example of how to accomplish this but I think rather than try to optimize a manual command you should use something more robust (but complicated) and put it in a mapping. For instance this substitution command:

:%s/\<\w*\%#\w*\>/"&"

That says take the cursor position (\%#) and any word characters surrounding it (both \w*) and replace with itself surrounded by quotes.

A Normal mode mapping for this:

:nnoremap <leader>q :%s/\<\w*\%#\w*\>/"&"<CR>

Now you've optimized things down to a key press or two...whatever the length of the mapping you choose.

What I'd really suggest, though, is that you just get the vim-surround plugin. Then you can easily wrap words (and more) with anything that you'd usually wrap words with (e.g. parens).

6
  • It might be worth adding word boundaries to the pattern – D. Ben Knoble Dec 22 '20 at 13:49
  • Possibly over-cautious (personally I'd probably prefer \S over \w) but done. – B Layer Dec 22 '20 at 14:00
  • Thanks a lot for all the details and explanation ! – Makoto Dec 22 '20 at 15:39
  • 1
    @Makoto You're welcome. I'm tempted to ask why you accepted the other answer (which repeats some of this answer and adds details that can be found in the docs) but it's your right to do whatever you want with that designation so I'll instead just say, welcome to Vi and Vim SE. – B Layer Dec 22 '20 at 15:49
  • @B Layer both answers are exactly what I need to know (and provided even more information, which is pretty cool), I just wanted to help a little a newcomer (I still upvoted your answer) – Makoto Dec 22 '20 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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