1

I chose to use gvim as my primary text editor, but still would like it to behave more like other visual editors I'm used to. One aspect of that is that when I have wrap enabled (set linebreak) and use the arrow keys <Up> and <Down> in insert mode, I'd like to move the cursor to the previous / next screen line, not logical line. This can be achieved using the mappings:

inoremap <Up>   <C-O>gk
inoremap <Down> <C-O>gj

...and everything is fine.

Except, in select mode. While using <S-Right>, <S-Left> works as expected, <S-Up> and <S-Down> still move in terms of logical lines. On http://vim.wikia.com I found the following suggestion for additional mappings:

noremap  <S-Up>     vgk
noremap  <S-Down>   vgj
inoremap <S-Up>     <C-O>vgk
inoremap <S-Down>   <C-O>vgj

The two latter mappings now enable that when I start a selection by pressing <S-Down> in insert mode, I get a selection from the previous position to the same position in the next screen line. But when I already have a selection (already am in select mode), pressing <S-Down> moves one line down but loses the selection.

I would expect to achieve this it would be necessary to have specific mapping for select mode (snoremap), but wasn't able to figure out how to do it.


Because of the discussion with glts whether select mode is useless or not, maybe some background information is in order: Select mode appears to be vim's attempt to provide something close to the selection behavior found in most other visual editors on MS Windows, Mac OS, and even Linux, which in turn is inspired by IBM's CUA. Since it is only really useful with the accompanying keyboard mappings ^C, ^X, ^V, it is meant to be used in conjunction with mswin.vim which provides these mappings. My question is motivated by an attempt to complement these mappings such that select mode works as expected also for wrapped text.

A. Donda
  • 8,381
  • 2
  • 20
  • 49
  • Theoretically, this question belongs on superuser. However, most vim questions on stackexchange appear to be on stackoverflow – that's why I put mine here, too. – A. Donda Sep 07 '13 at 22:19
  • In Vim the selection mode is called ***Visual** mode*. (Vim does have something else called "Select mode" but you can immediately forget about it since its mostly useless in practice.) You can make Visual mode mappings using `:xnoremap`. – glts Sep 07 '13 at 23:18
  • @glts is there any reason to use xnoremap instead of vnoremap? – FDinoff Sep 07 '13 at 23:59
  • 2
    @FDinoff Principle of least surprise? `:xnoremap` is specifically for Visual mode, `:vnoremap` is for both Visual and Select mode, which is hardly ever appropriate. `:h mapmode-x` has a warning from potential confusion. – glts Sep 08 '13 at 00:29
  • Dear @glts, I'm pretty sure what I mean is ***Select** mode*. :-) – A. Donda Sep 08 '13 at 00:41
  • It's harder to make Vim behave like other editors than adjusting yourself to how it works. Much of its power derives directly from its "different" design: refusing that "difference" will lead you nowhere. – romainl Sep 08 '13 at 06:20
  • @romainl – Why is it that vimers always have to lecture others about the "right way" to do things? That's an extremely annoying behavior, that I haven't seen with any other kind of software before. I have *my* reasons for choosing vim, and I have *my* reasons for not wanting to use it "the vim way". vim's power derives from being extremely customizable and programmable, not from "staying out of insert mode". And my power derives from not always having to reset my motor system when switching between different softwares. Seriously, what is this?! – A. Donda Sep 29 '13 at 22:35
  • You can be wrong all you want, it won't prevent people who know better (because they were just as wrong as you at some time) to tell you that you are. – romainl Sep 30 '13 at 04:51

2 Answers2

1

For Select mode, if that is really what you mean, these mappings would work:

vnoremap <S-Up>   gk
vnoremap <S-Down> gj
imap     <S-Up>   <Esc>gh<S-Up>
imap     <S-Down> <Esc><Right>gh<S-Down>
nmap     <S-Up>   gh<S-Up>
nmap     <S-Down> gh<S-Down>

Note the gh command (Select mode) instead of v (Visual mode).

But be warned that – and this is an assumption on my part – the general populace of Vim users shun Select mode, seeing as it runs counter to the Vim way.

Visual mode is much more powerful, since in addition to replacing text, you can also yank it into a register, make it uppercase or lowercase, change the extent of the Visual selection, etc. etc. Have a look at :h vim-modes.

glts
  • 21,808
  • 12
  • 73
  • 94
  • Thanks a lot, glts! I'm not sure I have understood all the details yet, but your solution works. After I posted the question I came up with a solution of my own, see my own answer. If you have a moment, I'd be interested in your comments on the relative merits of your and my approach. – A. Donda Sep 08 '13 at 13:40
  • Concerning "serious user", I can assure you that I am quite serious. :) But I have to point out that (in comments on SO and elsewhere) I found a disturbing tendency among vim experts: They seem to imagine there is only *one way* to properly use an editor, and everything else is misguided. While this may be deeply felt, I would urge vim experts to be less outspoken about it, because frankly it is a tiny bit condescending, and also just plain wrong. I can tweak the UI to fit habits formed elsewhere, and still use the power of vim, in this case simply by using visual mode if it is better suited. – A. Donda Sep 08 '13 at 13:55
  • @A.Donda Sure, everybody do as suits them best. Some people like to use operators to do an edit, whereas others do the same in Visual mode, because they prefer the Visual feedback of Visual mode. Both methods are of course perfectly fine! In the case of Select mode, however, I stand by my statement: Select mode has simply *only* disadvantages and *no* advantages compared with Visual mode; Visual mode can do the same as Select mode and *a lot* more. – glts Sep 08 '13 at 13:57
  • In practically every other editor (ok, except vi, Emacs, joe), I make a selection either by mouse, or by using Shift-Arrow keys. AFAIK this is known as CUA-conforming behavior. The corresponding mode on vim is select mode, not visual mode. – My hands have about 20 years of practice doing these movements, and I use them everyday in other places, e.g. the built-in editor of MATLAB, or the compose window of Gmail in Chrome. Of course visual mode is more powerful, but most of the time I don't need this power. Why should I always have to switch? – A. Donda Sep 08 '13 at 14:06
  • @A.Donda Ok, that's fair. I've had such a disregard for Select mode that I didn't even know you could use it with Ctrl-C, Ctrl-X, Ctrl-V ... thanks! – glts Sep 08 '13 at 14:11
  • Had to revise the comment because there wasn't enough space... Thanks to you for considering a problem you think doesn't make sense! – A. Donda Sep 08 '13 at 14:13
0

Here is what I came up with myself:

Make <Up> and <Down> move to previous / next screen line. (In insert mode, <C-O> switches to normal mode for one command. In normal mode, gj and gk are the 'move by screen line' commands.)

inoremap <Up>       <C-O>gk
inoremap <Down>     <C-O>gj

Same for <S-Up> and <S-Down> in insert mode, entering select mode. (In normal mode, v enters visual mode. gj and gk work also in visual mode. In visual mode, <C-G> enters select mode.)

inoremap <S-Up>     <C-O>vgk<C-G>
inoremap <S-Down>   <C-O>vgj<C-G>

Same for <S-Up> and <S-Down> in select mode. (In select mode, <C-O> switches to visual mode for one command.)

snoremap <S-Up>     <C-O>gk
snoremap <S-Down>   <C-O>gj
A. Donda
  • 8,381
  • 2
  • 20
  • 49
  • Now I see! Learned something new here, I didn't know you could use `` in Select mode. – glts Sep 08 '13 at 14:03