Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Add dictionary for user can add custom mapping rule. #32

wants to merge 2 commits into from

3 participants


introduce g:surround_custom_mapping dictionary which help filetype based custom mapping.

I hope this mini utility for surround.vim is bundled in orignal surround.vim.
User can easily add custom mapping rule for each &filetype or global scope based.

configuration example

let g:surround_custom_mapping = {}"{{{
let g:surround_custom_mapping._ = {
            \ 'p':  "<pre> \r </pre>",
            \ 'w':  "%w(\r)",
            \ }
let = {
            \ 'p':  "> \r <",
            \ }
let g:surround_custom_mapping.ruby = {
            \ '-':  "<% \r %>",
            \ '=':  "<%= \r %>",
            \ '9':  "(\r)",
            \ '5':  "%(\r)",
            \ '%':  "%(\r)",
            \ 'w':  "%w(\r)",
            \ '#':  "#{\r}",
            \ '3':  "#{\r}",
            \ 'e':  "begin \r end",
            \ 'E':  "<<EOS \r EOS",
            \ 'i':  "if \1if\1 \r end",
            \ 'u':  "unless \1unless\1 \r end",
            \ 'c':  "class \1class\1 \r end",
            \ 'm':  "module \1module\1 \r end",
            \ 'd':  "def \1def\1\2args\r..*\r(&)\2 \r end",
            \ 'p':  "\1method\1 do \2args\r..*\r|&| \2\r end",
            \ 'P':  "\1method\1 {\2args\r..*\r|&|\2 \r }",
            \ }
let g:surround_custom_mapping.javascript = {
            \ 'f':  "function(){ \r }"
            \ }
let g:surround_custom_mapping.lua = {
            \ 'f':  "function(){ \r }"
            \ }
let g:surround_custom_mapping.python = {
            \ 'p':  "print( \r)",
            \ '[':  "[\r]",
            \ }
let g:surround_custom_mapping.vim= {
            \'f':  "function! \r endfunction"
            \ }

This is already possible with global and buffer-local variables.


graywh , I know that, I use it's global and buffer-local variables.

The purpose of my change is that user don't need to use char2nr and user can use dictonary to define filetype specific custom mapping.

In Filetype autocmd dictonary value for &filetype is processed to define custome mapping.


Back when I first wrote surround.vim, Vim 7 was new and Vim 6 was still commonly provided with OS distributions. I strove to make surround.vim strictly backwards compatible (even though this meant only a subset of the functionality worked) so that I could still make use of it on servers an whatnot. Those days are long behind us, so for a while now I've had ambitious plans which included a hash like syntax for customization.

A couple of differences in my vision:

  • Instead of merely wrapping the old interface (and mapping the hash to variables), I want it to be first class. You should be able to modify the hash and have surroundings show up instantly.
  • I'd rather have per-buffer hashes than per-filetype hashes, as their a bit more flexible. If I do add the latter, it will leverage the former.

Now that I'm finally getting on top of my open source stuff, hopefully you'll see progress on this in the next month or two. I'll leave the pull request open until then.


I'm happy to hear your ambitious plan to support hash like customization.

I'm looking forward to see your new version of surround.vim!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 16, 2011
  1. @t9md
  2. @t9md

    I notice key 'global' for global mapping is not good, possibly

    t9md authored
    conflict in future. so user `_` as key for global mapping
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 0 deletions.
  1. +24 −0 plugin/surround.vim
24 plugin/surround.vim
@@ -621,6 +621,30 @@ if !exists("g:surround_no_mappings") || ! g:surround_no_mappings
"imap <C-S><C-S> <Plug>ISurround
+if !exists('g:surround_custom_mapping')
+ let g:surround_custom_mapping = {}
+function! s:surround_custom_map(scope) "{{{
+ if (a:scope != 'g') && empty(&ft)
+ return
+ endif
+ let map_dict = a:scope == 'g' ? '_' : &ft
+ if !has_key(g:surround_custom_mapping, map_dict)
+ return
+ end
+ for [key, action] in items(g:surround_custom_mapping[map_dict])
+ let command = "let ".a:scope.":surround_".char2nr(key)." = ".string(action)
+ execute command
+ endfor
+augroup Surround
+ autocmd!
+ autocmd VimEnter * call s:surround_custom_map('g')
+ autocmd FileType * call s:surround_custom_map('b')
+augroup END
let &cpo = s:cpo_save
" vim:set ft=vim sw=2 sts=2 et:
Something went wrong with that request. Please try again.