Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top