3

I'm writing a Vim plugin which will need to surround the word directly under your cursor with a quotes. I have tried using simple solutions which use commands like "b" and "e" executed in normal mode, in combination with other cursor position commands, though those felt clunky and non-robust.

I wondered if there was perhaps, a builtin way to surround text, or a more expedient method of doing so.

  • 2
    Welcome to Vi and Vim! As it stands, your question asks for answers for which it is hard to judge the best—words like simplest and fastest can be subjective and solicit answers based on opinion rather than fact and experience. That doesnt work well with the StackExchange mode. Please edit your question to clarify your goals and make them objective. [side note: have a look at tpope’s surround.vim on github] – D. Ben Knoble Sep 4 at 13:55
6

You can surround the word currently under the cursor in quotes with the following normal mode commands:

ciw""EscP

Replace iw with any other Vim motion/text object to surround other things with quotes*. Or "" with any other pair of characters to surround the object with different things.

If you want to surround the object with a longer piece of text, such as an HTML <p> tag, you can use Ctrl-R instead of the P put command:

ciw<p>Ctrl-R"</p>Esc

See :help i_CTRL-R for more details.

This latter command performs the change as a single edit, and thus is repeatable with the . command, but as discussed in the comments, when you repeat the command, it will enter the text from the original change command. However, as @user938271 explains, you can workaround this by using Ctrl-RCtrl-O or Ctrl-RCtrl-P instead of a plain Ctrl-R when inserting the contents of the register, making it easy to apply the edit quickly in several different places.

* Or use c from visual mode to surround text that is hard to describe with a single motion.

  • 3
    @filbranden: If you want the dot command to use the last deleted word (and not always the first), during the first edition, you can press C-r C-o " or C-r C-p " (instead of just C-r ") to insert the deleted word back into the buffer. – user938271 Sep 4 at 18:29
  • 2
    @filbranden: Reading the docs, it's unclear to me, too. But I think the difference is that C-r inserts the text "as if you typed it, but mappings and abbreviations are not used" - meaning that . doesn't "remember" the command C-r ", just the actual text that it expanded to (because that's what would have happened if you had typed it). But IMHO the docs really ought to call that out, if that's what is meant. – Kevin Sep 4 at 22:32
  • 2
    @Kevin: I don't think that's the relevant line from the help, because it's mentioned at :h i^r, and C-r doesn't make . behave like C-r C-o. I think the relevant line is "Does not replace characters!". If you insert the deleted word foo with C-r ", you'll see that the output of :echo @. is foo. But if you insert it with C-r C-o ", the output of :echo @. is ^R^O". In the dot register, the keys you pressed have not been replaced with the actual inserted characters. I think that's what "Does not replace characters!" refer to. – user938271 Sep 5 at 10:53
  • 2
    I could be wrong though; the help is not very clear. Also, the dot command works as expected with C-r C-o and C-r C-p, but only if the deleted text is characterwise; not linewise. – user938271 Sep 5 at 10:59
  • 2
    Sorry, the dot command does behave as expected; what I meant is that you can't use C-r C-o to surround a linewise text without breaking the undo sequence: i.imgur.com/rFGJXxy.gif Here, Up breaks the undo sequence (even if you prefix it with C-g U). – user938271 Sep 5 at 11:13
6

Yes, a plug-in for surrounding with quotes exists! vim-surround is what you're looking for.

To surround the current word in double quotes, you can use ysiw" once you have the plug-in installed.

ys is the command to surround and object (there's also cs to replace one delmiter with another, ds to remove surrounding), then iw is a text-object defining what to surround and finally " is what to surround it with.

You might want to read the docs for vim-surround to see all that it can do. It's really a lot!

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.