1

I'm a user of the surround.vim plugin that lets me surround words with quotes, brackets, etc.

I'm facing an issue with surrounding a single character with e.g. quotes, when the character is adjacent to other characters. Thus, I cannot use ysw" to quote the single character.

How can I operate on a single character? I'm used to using x to delete a single character, but I'm not familiar with a motion like w that describes a single character.

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

2

Faster than visual mode: ysl"

You can use both text-objects and motions with any operator, and hjkl are motions!

Note that according to :help x, the command x is equivalent to dl, while X is equivalent to dh. They have a kind of paradoxical definition that turns out to be nicely intuitive in practice.

The reason they work this is both are exclusive motions, which means the last character of the selection is not included. This "last character" is always determined relative to the buffer end: in other words, characters closer to G$ are "later" than characters closer to gg0! So yl will only yank the character you're on, while yh yanks the previous character. This holds for any operator, including the surround version given at the top.

| improve this answer | |
  • Thank you, but shouldn’t l affect the next character and not the character under the cursor? – Shuzheng Feb 18 at 16:12
  • Press vl and realize that the character under the cursor is highlighted; l in this case is a sort of exclusive motion that includes only the current char. In fact, x is equivalent to dl – D. Ben Knoble Feb 18 at 16:52
  • Shouldn’t dh be equivalent to dl then? – Shuzheng Feb 18 at 19:33
  • @Shuzheng i’m sure, but (somewhat paradoxically) I wouldnt expect so: probably equivalent to X – D. Ben Knoble Feb 18 at 19:34
  • @Shuzheng i just checked the docs and confirmed my above comment. – D. Ben Knoble Feb 18 at 19:46
0

S in visual mode with an argument wraps the selected text. So:

  1. Put your cursor on the character you want to quote.
  2. Press v to select it.
  3. Press S" to quoto.
| improve this answer | |

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.