(Insert TipProposed template + minor manual clean) |
(Change <tt> to <code>, perhaps also minor tweak.) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{TipNew |
||
− | {{TipProposed |
||
− | |id= |
+ | |id=1564 |
− | |previous= |
+ | |previous=1563 |
− | |next= |
+ | |next=1565 |
− | |created= |
+ | |created=2008 |
|complexity=basic |
|complexity=basic |
||
|author=Benshi |
|author=Benshi |
||
Line 11: | Line 11: | ||
|category2= |
|category2= |
||
}} |
}} |
||
+ | It can be convenient to use the numeric keypad when you need to enter a lot of numbers. However, your keyboard may not have a keypad, or you may not want to move your hands from the home row. |
||
− | In this tip you will get an explanation on how to create a special insert mode in which some keys will be maped to the numbers "<tt>0-9</tt>" and remaped to their original function as soon as this mode is left. |
||
+ | The following script allows you to switch Vim to a special "VimLock" mode (like pressing the Num Lock key). In this mode, pressing keys on the home row (a, s, d, ...) will generate digits (1, 2, 3, ...) rather than letters. Press Escape to exit from VimLock mode. |
||
− | First, define two functions (preferably in your [[vimrc]]): |
||
+ | |||
+ | Put the following in your [[vimrc]]: |
||
<pre> |
<pre> |
||
⚫ | |||
− | function! |
+ | function! VimLock(enable) |
− | imap a 1 |
||
+ | if a:enable |
||
− | imap s 2 |
||
+ | inoremap a 1 |
||
− | imap d 3 |
||
+ | inoremap s 2 |
||
− | imap f 4 |
||
+ | inoremap d 3 |
||
− | imap g 5 |
||
+ | inoremap f 4 |
||
− | imap h 6 |
||
+ | inoremap g 5 |
||
− | imap j 7 |
||
+ | inoremap h 6 |
||
− | imap k 8 |
||
+ | inoremap j 7 |
||
− | imap l 9 |
||
+ | inoremap k 8 |
||
− | imap ö 0 |
||
+ | inoremap l 9 |
||
⚫ | |||
+ | inoremap ; 0 |
||
⚫ | |||
+ | else |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | iunmap <Esc> |
||
+ | endif |
||
endfunction |
endfunction |
||
− | |||
− | function! Blockend() |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | imap <Esc> <Esc><CR> |
||
− | ndfunction |
||
</pre> |
</pre> |
||
⚫ | Now you can press <code>Ctrl-i</code> to enter insert mode and map the keys "<code>a s d f g h j k l ;</code>" to the digits "<code>1 2 3 4 5 6 7 8 9 0</code>". Pressing <code>Esc</code> will end this mode and restore the letter keys. Since <code>Ctrl-i</code> is Tab, you can just press the Tab key to enter VimLock mode. |
||
− | Now you can map the new mode to a key, for example <tt>Control-i</tt>: |
||
− | <pre> |
||
⚫ | |||
− | </pre> |
||
+ | The mappings assume a QWERTY US keyboard where the <code>;</code> key is on the right of <code>L</code>. If necessary, change <code>;</code> to suit your keyboard. |
||
⚫ | |||
==References== |
==References== |
||
Line 55: | Line 55: | ||
*{{help|user-functions}} |
*{{help|user-functions}} |
||
*{{help|:call}} |
*{{help|:call}} |
||
− | *{{help|: |
+ | *{{help|:inoremap}} |
*{{help|:iunmap}} |
*{{help|:iunmap}} |
||
Latest revision as of 06:33, 13 July 2012
created 2008 · complexity basic · author Benshi · version 7.0
It can be convenient to use the numeric keypad when you need to enter a lot of numbers. However, your keyboard may not have a keypad, or you may not want to move your hands from the home row.
The following script allows you to switch Vim to a special "VimLock" mode (like pressing the Num Lock key). In this mode, pressing keys on the home row (a, s, d, ...) will generate digits (1, 2, 3, ...) rather than letters. Press Escape to exit from VimLock mode.
Put the following in your vimrc:
nnoremap <C-I> :call VimLock(1)<CR>i function! VimLock(enable) if a:enable inoremap a 1 inoremap s 2 inoremap d 3 inoremap f 4 inoremap g 5 inoremap h 6 inoremap j 7 inoremap k 8 inoremap l 9 inoremap ; 0 inoremap <Esc> <Esc>:call VimLock(0)<CR> else iunmap a iunmap s iunmap d iunmap f iunmap g iunmap h iunmap j iunmap k iunmap l iunmap ; iunmap <Esc> endif endfunction
Now you can press Ctrl-i
to enter insert mode and map the keys "a s d f g h j k l ;
" to the digits "1 2 3 4 5 6 7 8 9 0
". Pressing Esc
will end this mode and restore the letter keys. Since Ctrl-i
is Tab, you can just press the Tab key to enter VimLock mode.
The mappings assume a QWERTY US keyboard where the ;
key is on the right of L
. If necessary, change ;
to suit your keyboard.