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)

8
  • map <leader>s :split<CR>| " ,s for split. Note the |.
    – Antony
    Aug 8, 2016 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 8, 2016 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 8, 2016 at 15:41
  • 1
    It was the eol-comments that did it. Aug 8, 2016 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. Aug 9, 2016 at 6:41

1 Answer 1

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>
2
  • 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 8, 2016 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. Aug 8, 2016 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.