tpope / vim-surround

surround.vim: Delete/change/add parentheses/quotes/XML-tags/much more with ease
https://www.vim.org/scripts/script.php?script_id=1697
13.39k stars 444 forks source link

Surrounding multiple times #213

Open Chinggis6 opened 7 years ago

Chinggis6 commented 7 years ago

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?

jpassaro commented 7 years ago

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.

offchan42 commented 7 years ago

@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 commented 7 years ago

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 commented 7 years ago

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.

jottr commented 7 years ago

@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

boardfish commented 6 years ago

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.

Chinggis6 commented 6 years ago

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

Chinggis6 commented 6 years ago

^ 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)

rattletat commented 6 years ago

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.

rjmccabe3701 commented 5 years ago

@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\"\"\""
trapier commented 5 years ago

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.

michaelfresco commented 3 years ago

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.

naught101 commented 5 months ago

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

qadzek commented 5 months ago

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.