0

In my vimrc I have a shortcut for opening a vertical split:

let mapleader = ','
map <leader>v :vsplit<CR> " ,v for vsplit
map <leader>s :split<CR>  " ,s for split

This has been working fine for months, but recently it's started misbehaving: almost every time I try to split the window, it does split but then changes a load of text at the cursor and leaves itself in insert mode.

Here's an example:

before split: const queryValidation
after split: plit", n

All that I did was hit ,v which has been working fine until recently. I can't work out why this is happening now. My vim version is 7.4.2109.

(This doesn't happen if I just use :vsplit)

Karl Yngve Lervåg
  • 9,494
  • 25
  • 35
GTF
  • 111
  • 4
  • map <leader>s :split<CR>| " ,s for split. Note the |. – Antony Aug 08 '16 at 14:06
  • 2
    By the way, :vsplit and :split already have normal mode commands: <C-w>v and <C-w>s. No need to waste keys on redundant shortcuts. – romainl Aug 08 '16 at 14:39
  • 1
    Note that this question had the same problem (btw the title doesn't indicate clearly that this is a duplicate) and this answer I made also indicates how to solve this problem, it could be worth reading it for a further mapping problem :-) – statox Aug 08 '16 at 15:41
  • @romainl yes, but <C-w>v is a lot more awkward for me to type than ,v. – GTF Aug 08 '16 at 19:21
  • @statox that answer is killer, thanks. It could have been the eol-comments that did it. – GTF Aug 08 '16 at 19:22
  • 1
    It was the eol-comments that did it. – Karl Yngve Lervåg Aug 08 '16 at 21:48
  • 2
    Just to add to this: When you use " in a mapping, it will typically imply the normal mode key ", which indicates a register. For example, the following map will ensure that dd does not add to the most recent register, but instead save to the "black hole"-register: nnoremap dd "_dd. The point is, since " is a reasonable part of the right-hand side of a map, it can NOT be used to start a comment. This explains why the comments screw up your mapping, and why it works when you remove them. – Karl Yngve Lervåg Aug 09 '16 at 06:41
  • @GTF: You can trust Karl he is right: the problem does come from the " comment. I'm not sure why it seemed to work before but Karl's explaination is totally right. – statox Aug 09 '16 at 07:14

1 Answers1

5

You should not use end-of-line comments, in particular for mappings. See :h comment. Also, I suggest that you use nnoremap instead of simply map. Thus, you should use:

nnoremap <leader>v :vsplit<CR>
nnoremap <leader>s :split<CR>
Karl Yngve Lervåg
  • 9,494
  • 25
  • 35
  • 1
    Could you explain why I should use nnoremap instead of map in this case? Thanks for the no eol comments, by the way, I think that may have done the trick (I'll give it a day or two to be sure, though). – GTF Aug 08 '16 at 19:20
  • 2
    I know it "did the trick". The eol comments screwed up your mapping. nnoremap ensures that the mapping is non recursive. If you do not need map, then you should always use nnoremap (or noremap). It is all well described in the docs, see :h :map-commands. – Karl Yngve Lervåg Aug 08 '16 at 21:47