vim-surround icon indicating copy to clipboard operation
vim-surround copied to clipboard

Surrounding multiple times

Open Chinggis6 opened this issue 8 years ago • 14 comments

Thanks, the plugin is great, I use yss often, and like how the difference between pair characters is implemented (in ] and [ for example). But, what to do when I need to surround the line with double, triple or N amount of characters? I tried 2yss] and yss2] to surround the line with double brackets ([[line]]) (which is happening frequently when dealing with org mode in emacs or wikimarkup on wikipedia) but it didn't work (obviously). Any solutions?

Chinggis6 avatar Nov 03 '16 19:11 Chinggis6

you could do yss]ysa]], which surrounds the line with brackets then surrounds those brackets with another pair of brackets. You could probably write a function / plugin that takes a count and interprets it this way: yss<char> followed by count-1 applications of ysa<char><char>.

Wonder if such an extension would be useful enough to include in the plugin... Personally I don't need to do this too often.

jpassaro avatar Nov 10 '16 16:11 jpassaro

@jpassaro I do this a lot! For example, when in markdown, you want to surround things with ``` to make it into a code block. In Python, you want to surround things with """ to make it into a block comment.

offchan42 avatar Nov 28 '16 03:11 offchan42

Currently, I map it like this: https://github.com/off99555/vimrc/commit/ed9b0428d898a20587629652d35e7e9f61d0761f

So S3" would surround things with 3 double quotes. It's not using vim-surround plugin's engine at all. I think it might be possible that vim-surround should allow counts.

offchan42 avatar Nov 29 '16 10:11 offchan42

And a lot of times, I want to surround things with arbitrary text like HEY my text HEY. It should be obvious for the vim-surround maintainer to implement this because it's like surrounding with tag but no <> symbol. The desired key should be something like ysw T (for text) followed by HEY. Of course, this T key can be chosen, there are literally hundreds of keys we can substitute in.

offchan42 avatar Nov 29 '16 10:11 offchan42

@tpope would it be possible to implement above feature somehow? Or are there any workarounds to get to the desired behaviour? This is especially interesting when editing markdown files. See https://github.com/tpope/vim-surround/issues/202 and https://github.com/tpope/vim-surround/issues/15

jottr avatar May 13 '17 14:05 jottr

Totally up for this. Right now, the required motion is ysiw*l. for a single word. In other cases, things get complex enough that you might need to make a complex text selection twice.

boardfish avatar Oct 16 '17 12:10 boardfish

@boardfish you might consider using . (dot) to repeat the command until we have numbers ;-)

Chinggis6 avatar Oct 16 '17 12:10 Chinggis6

^ oh it doesn't work even with vim-repeat. :-(

though ysw (unlike yss) worked partially... that is it prompts for a character input to surround the word with (instead of remembering the previous character)

Chinggis6 avatar Oct 16 '17 12:10 Chinggis6

Totally opt for this feature. Would fit more nicely into vim's language. 3dsb to delete 3 bracket surroundings for example. PS: vim-repeat is also not working that way. For me dsb 2. does not work.

rattletat avatar Dec 31 '17 00:12 rattletat

@off99555 does this help?

"Code block for markdown
 autocmd FileType markdown let b:surround_45 = "```\n\r\n```"

"Block comment for python
 autocmd FileType python let b:surround_45 = "\"\"\"\n\r\n\"\"\""

rjmccabe3701 avatar Oct 03 '19 03:10 rjmccabe3701

Thanks @rjmccabe3701! With that config the one-line case is covered by yss-. I wish ysip didn't remove indentation, but that seems to be default surround behavior.

I edit a lot of Jira markup in vim, so I'm gonna go wild and set this up for {code}, {noformat}, and the like.

trapier avatar Oct 03 '19 17:10 trapier

For markdown code blocks, I use this to wrap ``` around my selection:

vmap <Leader>mc S続<esc>:%s/続/```<CR>

Though yss- with let b:surround_45 is much more elegant.

michaelfresco avatar Mar 05 '21 14:03 michaelfresco

Something along these lines would be very useful for using writing **bold text** in markdown.

naught101 avatar Apr 30 '24 12:04 naught101

Something along these lines would be very useful for using writing bold text in markdown.

This should work:

let b:surround_{char2nr('o')} = "**\r**"

Mnemonic: bOld.

qadzek avatar May 06 '24 13:05 qadzek