Mapping <Shift>-Arrows to selecting characters/lines

本秂侑毒 提交于 2019-11-30 06:39:18

Vim doesn't bend to that easily in my opinion. The terminal one doesn't even recognize Shift-Up in my case! I thought the v (character-wise selection) or V (line-wise selection) was among the easier concepts to learn about vi/vim.

If this works (can't test right now), this is something you'll want:

" activate visual mode in normal mode
nmap <S-Up> V
nmap <S-Down> V
" these are mapped in visual mode
vmap <S-Up> k
vmap <S-Down> j
" 
" etc...
" similarly <S-Left>, <S-Right> for v

I completed @escrafford mapping with insert mode's ones:

" shift+arrow selection
nmap <S-Up> v<Up>
nmap <S-Down> v<Down>
nmap <S-Left> v<Left>
nmap <S-Right> v<Right>
vmap <S-Up> <Up>
vmap <S-Down> <Down>
vmap <S-Left> <Left>
vmap <S-Right> <Right>
imap <S-Up> <Esc>v<Up>
imap <S-Down> <Esc>v<Down>
imap <S-Left> <Esc>v<Left>
imap <S-Right> <Esc>v<Right>

Also mapping usual copy/cut/paste like this you can return to insert mode after select+copy, for example.

vmap <C-c> y<Esc>i
vmap <C-x> d<Esc>i
map <C-v> pi
imap <C-v> <Esc>pi
imap <C-z> <Esc>ui

Now you can start a shift+arrow selection from any mode, then C-c to copy, and then C-v to paste. You always end in insert mode, so you have also C-z to undo.

I think this approaches more to the 'expected standard' behaviour for a text editor yu are asking for.

Slightly different from progo's answer - this gives the same feel as mac apps normally have:

nmap <S-Up> v<Up>
nmap <S-Down> v<Down>
nmap <S-Left> v<Left>
nmap <S-Right> v<Right>
vmap <S-Up> <Up>
vmap <S-Down> <Down>
vmap <S-Left> <Left>
vmap <S-Right> <Right>

The differences being switch to visual mode instead of visual line mode, and not losing the initial up/down etc keystroke.

There's an specific option for this: keymodel:

'keymodel' 'km'     string  (default "")
            global
            {not in Vi}
    List of comma separated words, which enable special things that keys
    can do.  These values can be used:
       startsel Using a shifted special key starts selection (either
            Select mode or Visual mode, depending on "key" being
            present in 'selectmode').
       stopsel  Using a not-shifted special key stops selection.
    Special keys in this context are the cursor keys, <End>, <Home>,
    <PageUp> and <PageDown>.
    The 'keymodel' option is set by the |:behave| command.

TL;DR: To enable the behavior you want, use:

set keymodel=startsel

If you also want to leave visual mode when using <Up> or <Down> without <Shift> pressed, you can use:

set keymodel=startsel,stopsel

I found another solution that is easier to execute. The command ':behave mswin' does all that is needed to use shift plus cursor keys to select text. Works from any mode. It also supports Cmd-c, Cmd-v and Cmd-x. It works in MacVim but I did not try other platforms.

It is definitely recommended that you don't remap this feature. Simply switching to visual mode and using v and the arrow keys is a better idea. V will select the entire line, v$ will select to the end of the line and vw will select the next word. There are many more commands you can use to select different lines and words. Learning these commands will not only be useful for selecting but also useful for editing your files more efficiently.

This mapping keeps insert mode during selection (visual mode) and it starts on the correct position. You can also select a word to the left or right using Ctrl-Shift-Left/Right (if your terminal supports it):

" Select with shift + arrows
inoremap    <S-Left>              <Left><C-o>v
inoremap    <S-Right>             <C-o>v
inoremap    <S-Up>                <Left><C-o>v<Up><Right>
inoremap    <S-Down>              <C-o>v<Down><Left>
imap        <C-S-Left>            <S-Left><C-Left>
imap        <C-S-Right>           <S-Right><C-Right>
vnoremap    <S-Left>              <Left>
vnoremap    <S-Right>             <Right>
vnoremap    <S-Up>                <Up>
vnoremap    <S-Down>              <Down>

" Auto unselect when not holding shift
vmap        <Left>                <Esc>
vmap        <Right>               <Esc><Right>
vmap        <Up>                  <Esc><Up>
vmap        <Down>                <Esc><Down>

This may be useful for quickly selecting small parts when you're in insert mode but I recommend using the default commands for selecting larger parts.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!