Sunday, May 12, 2013

Re: Substitution gets broken when mapped

On 2013-05-12 22:01, Sylvia Ganush wrote:
> To remove trailing spaces I do:
>
> :%s/\s\+$//
>
> And it works perfectly. Since it's such a useful command I add this
> mapping to my .vimrc:
>
> nmap <leader>w :%s/\s\+$// <cr>

In theory, this *should* work (though the extra space before the
"<cr>" can be optionally removed, as can the two trailing "/"
characters). And, if tried in a raw session of Vim (using "-u
custom_vimrc" at startup, where "custom_vimrc" contains just

set nocp
nmap <leader>w :%s/\s\+$// <cr>
" nnoremap <leader>w :%s/\s\+$<cr>

This suggests that...

> But when I use the mapping, many extra lines get deleted and the
> text is mangled.

you have some other mapping that is interfering, perhaps created by a
plugin. I'd check the output of ":map" to see if something else has
been mapped to that sequence.

I'd also default to using "nnoremap" unless I had reason to use
"nmap" otherwise. I don't think that has anything to do with this
case, but by defaulting to nnoremap, I find I'm less likely to
surprise myself in the few cases where it really would make a
difference.

-tim


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: