Question

I just remapped some keys in my .vimrc and I'm getting some wierd behavior.

When I select text in visual mode, and hit the delete key, it surrounds the highlighted text with ""...

Here's the relevant pieces of my .vimrc

"Using Vundle
Bundle 'tpope/vim-surround'    

let mapleader= ","
let g:mapleader= ","

set backspace=eol,start,indent

"""""""""""""""""""""""""""""""""""""""""""""""""""
" => Surround Vim Shortcuts
"""""""""""""""""""""""""""""""""""""""""""""""""""
vmap ' S'
vmap " S"
vmap { S{
vmap } S}
vmap ( S(
vmap ) S)
vmap [ S[
vmap ] S]

" tags
vmap < S<
vmap > S>

" addressing the problem introduced by tags
vnoremap << <
vnoremap >> >

map <leader>s' ysiw'
map <leader>s" ysiw"
map <leader>s( ysiw)
map <leader>s[ ysiw]
map <leader>s{ ysiw}

map <leader>d' yss'
map <leader>d" yss"
map <leader>d( yss)
map <leader>d[ yss]
map <leader>d{ yss}

vmap <Leader>S <Plug>VSurround

Example:

  def persisted?
    false
  end

# I select it in visual mode, hit delete and it becomes:

  "def persisted?
  false
end"

I know a lot of this configuration is personal preference so any advice is welcome, but more importantly, why am I getting this behavior?

Was it helpful?

Solution

When I try :verbose map <BS> I get

s  <BS>        * b<BS>
    Last set from ~/<...>/after/plugin/snipMate.vim
x  <BS>          "-d

This tells me that there is a Select-mode mapping that will enter a "b" (deleting the selected text and entering Insert mode) followed by an Insert-mode <BS>; the * means that the b<BS> will not be remapped; and this mapping is defined by snipMate.vim. There is also a Visual-mode mapping to "-d. Since there is no *, it will be remapped.

Something is broken: I cannot figure out where that second mapping comes from.

Maybe if I have the second mapping, so do you. If so, then the " gets mapped (defined in your vimrc file) to S", and I guess that surround.vim does something with that. That more or less explains what you see. I am not sure what mode vim is in after all that, so I am not sure what the -d at the end will do.

Try :xunmap <BS> or :xnoremap <BS> "-d and see if it helps. If so, try putting it at the end of your vimrc file. If that does not work, try putting it inside a VimEnter autocommand.

:help :map
:help map-modes

OTHER TIPS

It's working for me so it's probably some kind of conflict. I'm still relatively new to Vim but I've had to sort out plugin and config conflicts several times by now. Plugins and user configurations all may affect each other so you might encounter really bizarre behaviour sometimes.

What's been working for me is to eliminate possible causes of the problem gradually. You might rename your vimrc file to prevent Vim from finding it, and then see if the problem persists. If it doesn't, something in your vimrc is not working well with your plugins. In that case, try commenting out lines of your vimrc to narrow down the cause of the problem (make sure to create a backup).

If the problem wasn't caused by your vimrc, it's likely a plugin conflict. Try moving away plugin files from the plugin folder in Vim plugin path and see if the problem persists.

For the record I'm using the newest MacVim (Snapshot 72) and when I put in

:verbose map <BS>
the output is:

v <BS>  "-d

I did what @benjifisher suggested and added the line:

xnoremap <BS> "-d  

to my .vimrc. The output now is

x  <BS>      *"-d  
s  <BS>       "-d  

and delete now works the way it should. So... Thanks!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top