tpope / vim-surround

surround.vim: Delete/change/add parentheses/quotes/XML-tags/much more with ease

Home Page:https://www.vim.org/scripts/script.php?script_id=1697

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom surroundings don't work with `cs` or `ds`

hbarcelos opened this issue · comments

Using any type of quotation is a pain with US-intl keyboards, because you are always expected to type an extra Space to get things done:

Hello | world

To get the whole line into quotes, I have to press yss"Space:

"Hello | world"

That's how the keyboard is supposed to work and I won't try to mess with it. To make things a bit easier though, I created the following custom surroundings and mappings:

let g:surround_{char2nr("d")} = "\"\r\""
onoremap ad a"
xnoremap ad a"
onoremap id i"
xnoremap id i"

let g:surround_{char2nr("s")} = "'\r'"
onoremap as a'
xnoremap as a'
onoremap is i'
xnoremap is i'

let g:surround_{char2nr("a")} = "`\r`"
onoremap aa a`
xnoremap aa a`
onoremap ia i`
xnoremap ia i`

So if I have:

Hello | world
  • yssd gives me "Hello World"
  • ysss gives me 'Hello World'
  • yssa gives me `Hello World`

So far so good. However the issue is that I cannot use cs neither ds with the custom surroundings:

"Hello | world"

dsd and csd have no effect.

But if I use them as the target for cs, they do work:

"Hello | world"

cs"s gives me 'Hello World'.

Edit:

I also tried to create normal mode mappings like:

nnoremap dsd ds"

But if fails.

However, if I use nmap instead, then it works:

nmap dsd ds"

There's two problems here:

  1. We apparently only support built-in text objects, not custom ones. (I'm actually surprised I did it this way, but hesitate to change it 15 years later.)
  2. Text objects like a" include leading whitespace, so we special case them anyways.

Your nmap dsd ds" is probably the way to go, or you could use the <Plug> equivalent:

nmap dsd <Plug>Dsurround"

This will work for cs/<Plug>Curround too.

  1. Text objects like a" include leading whitespace, so we special case them anyways.

That's the case for en-US keyboards, but not for other layouts, such as pt-BR or es-ES. No leading space is required.

Anyway, In case anyone find it useful, here's my current config to make it work in every mode:

let g:surround_{char2nr("d")} = "\"\r\""
nmap dsd <Plug>Dsurround"
nmap csd <Plug>Csurround"
onoremap ad a"
xnoremap ad a"
onoremap id i"
xnoremap id i"

let g:surround_{char2nr("s")} = "'\r'"
nmap dss <Plug>Dsurround'
nmap css <Plug>Csurround'
onoremap as a'
xnoremap as a'
onoremap is i'
xnoremap is i'

let g:surround_{char2nr("a")} = "`\r`"
nmap dsa <Plug>Dsurround`
nmap csa <Plug>Csurround`
onoremap aa a`
xnoremap aa a`
onoremap ia i`
xnoremap ia i`
  1. Text objects like a" include leading whitespace, so we special case them anyways.

That's the case for en-US keyboards, but not for other layouts, such as pt-BR or es-ES. No leading space is required.

I mean that if you da", it deletes any whitespace before the string, which is not what we want for ds".

Oh, my bad 😅. It makes sense.
Thanks a lot.