Search code examples
vimselectionkeymapping

gvim: <S-Home> skips last character on line


Because I like using shift with cursor keys in insert mode to select text, I have put the following mappings in my .gvimrc:

inoremap <S-Home>   <C-O>vg0<C-G>
inoremap <S-End>    <C-O>vg$<C-G>

While this works perfectly in most cases, there is a problem when the cursor is located at the end of a line.

before

In that case when I use <S-Home>, the last character on the line is not included in the selection:

after

Complementarily, but less importantly, <S-End> at the end of a line selects the last character.

How can I avoid this behavior? I have the feeling that this is not specifically related to vim's "select mode", but that vim generally treats line endings different than other text editors.


Solution

  • Thanks to glts, this is the solution:

    inoremap <S-Home>   <C-\><C-O>vg0<C-G>
    inoremap <S-End>    <C-\><C-O>vg$<C-G>
    

    According to the help at :h i_CTRL-\_CTRL-O, CTRL-\ CTRL-O is "like CTRL-O but don't move the cursor". It doesn't really explain why CTRL-O would move the cursor, but it appears to have something to do with the cursor being "beyond the end of the line". I can't claim to understand what that means, but it kind-of supports my suspicion that vim generally treats line endings different than other text editors.