Vim Tips Wiki
Register
(→‎Comments: using keymap to keep the .vimrc clean, what to do now?)
 
(15 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1183
 
|id=1183
 
|previous=1182
 
|previous=1182
|next=1185
+
|next=1189
|created=March 23, 2006
+
|created=2006
 
|complexity=basic
 
|complexity=basic
 
|author=Gerald Lai
 
|author=Gerald Lai
|version=5.7
+
|version=6.0
 
|rating=8/2
 
|rating=8/2
|category1=
+
|category1=Map
|category2=
+
|category2=Usage
 
}}
 
}}
  +
Pressing the Caps Lock key in Vim can cause trouble due to Vim's modal nature. Have you ever used Caps Lock in insert mode, then switched back to normal mode to issue a few commands, completely forgetting that Caps Lock is still on? Using this tip, you can enable Caps Lock only when typing text, eliminating those pesky <code><Esc>JJJJUUU</code> commands you accidentally enter.
This is a hack to provide CAPS Lock functionality only within these situations, so you don't have to turn the CAPS Lock off whenever you return to Normal mode:
 
*Insert mode
 
*Command-line mode
 
*when entering a search pattern
 
*the argument of the commands that accept a text character, such as "r" and "f"
 
*for the input() line
 
   
First, place the mappings below into your vimrc. Then, to toggle the CAPS Lock in *Insert mode*, hit Ctrl-6 (i.e., hold down Ctrl and press 6) instead of the _real_ Caps Lock key.
+
After implementing this tip, you do not need to use the Caps Lock key. Instead, when in insert mode, press Ctrl-<code>^</code> (hold down Ctrl and press <code>^</code> – the <code>6</code> key on US keyboards) to toggle "Caps Lock" on or off.
   
  +
This "Caps Lock" does not apply in normal mode. It only applies when:
  +
*Typing while in insert mode.
  +
*Typing in the command line.
 
*Typing a search pattern.
  +
*Typing a character for commands like <code>r</code> and <code>f</code>.
 
*Typing in the <code>input()</code> line.
  +
  +
==Using language mappings==
  +
To use Ctrl-<code>^</code> to toggle "Caps Lock", place the following in your [[vimrc]] (or just yank the lines in Vim, then type <code>:@"</code> to execute them):
 
<pre>
 
<pre>
  +
" Execute 'lnoremap x X' and 'lnoremap X x' for each letter a-z.
lnoremap a A
 
  +
for c in range(char2nr('A'), char2nr('Z'))
lnoremap b B
 
  +
execute 'lnoremap ' . nr2char(c+32) . ' ' . nr2char(c)
lnoremap c C
 
  +
execute 'lnoremap ' . nr2char(c) . ' ' . nr2char(c+32)
lnoremap d D
 
  +
endfor
lnoremap e E
 
lnoremap f F
 
lnoremap g G
 
lnoremap h H
 
lnoremap i I
 
lnoremap j J
 
lnoremap k K
 
lnoremap l L
 
lnoremap m M
 
lnoremap n N
 
lnoremap o O
 
lnoremap p P
 
lnoremap q Q
 
lnoremap r R
 
lnoremap s S
 
lnoremap t T
 
lnoremap u U
 
lnoremap v V
 
lnoremap w W
 
lnoremap x X
 
lnoremap y Y
 
lnoremap z Z
 
lnoremap A a
 
lnoremap B b
 
lnoremap C c
 
lnoremap D d
 
lnoremap E e
 
lnoremap F f
 
lnoremap G g
 
lnoremap H h
 
lnoremap I i
 
lnoremap J j
 
lnoremap K k
 
lnoremap L l
 
lnoremap M m
 
lnoremap N n
 
lnoremap O o
 
lnoremap P p
 
lnoremap Q q
 
lnoremap R r
 
lnoremap S s
 
lnoremap T t
 
lnoremap U u
 
lnoremap V v
 
lnoremap W w
 
lnoremap X x
 
lnoremap Y y
 
lnoremap Z z
 
 
</pre>
 
</pre>
   
  +
Since the idea is to allow you to forget about capslock, it may also be desirable to automatically turn off this special mode when exiting insert mode:
==References==
 
*{{help|language-mapping}}
 
*{{help|i_CTRL-^}}
 
*[[VimTip1083]]
 
 
==Comments==
 
The alternate Caps Lock provided by this tip is NOT effective while in Normal mode (i.e., after hitting Esc in Insert/Visual mode).
 
 
The advantage is that this alternate Caps Lock, once activated, only works in modes where it "really matters", like while you're typing/replacing in Insert mode, or inputting something into the :cmd-line. Langmapping is Vim's way of achieving such a task, because it is mode-specific.
 
 
If you do insist on doing _without_ langmaps, there is a suggestion of using AutoHotKey in the comments of [[VimTip1083]] (that was provided as a reference). I can't say that this is very portable across different platforms.
 
 
If you are using this tip, but still wish to disable the alternate Caps Lock when pressing Esc, you could do
 
 
 
<pre>
 
<pre>
 
" Kill the capslock when leaving insert mode.
:inoremap <silent><Esc> <Esc>:set imi=0<CR>
 
 
autocmd InsertLeave * set iminsert=0
:vnoremap <silent><Esc> <Esc>:set imi=0<CR>
 
 
</pre>
 
</pre>
   
  +
Note the use of the <code>'iminsert'</code> option. Pressing <code>CTRL-^</code> actually toggles this option between 0 and 1, which automatically enables or disables your language maps. A related option, <code>'imsearch'</code>, controls the mappings while entering a search pattern.
----
 
One could also use the 'keymap' option to accomplish the same thing, while [[Keep your vimrc file clean|keeping their .vimrc file clean]] and additionally adding an indicator to the status line while in this "capslock" mode. After reading this tip, I placed the following in $HOME/vimfiles/keymap/insert-only_capslock.vim:
 
   
  +
==Using a [[How_to_make_a_keymap|keymap]]==
<pre>
 
  +
An alternative procedure that also causes Ctrl-<code>^</code> to toggle "Caps Lock", is to use a keymap (this is ''not'' a mapping; see {{help|mbyte-keymap}}).
" set caps lock, but only for insert and command mode, when pressing CTRL-^
 
" based on http://vim.wikia.com/wiki/VimTip1183
 
   
  +
Using the <code>'keymap'</code> option [[Keep your vimrc file clean|keeps vimrc clean]] and adds an indicator to the status line while in this "Caps Lock" mode.
let b:keymap_name = "CAPS"
 
   
  +
Complete and place the following in file <code>~/.vim/keymap/insert-only_capslock.vim</code> on Unix-based systems, or file <code>$HOME/vimfiles/keymap/insert-only_capslock.vim</code> on Windows systems:
 
<pre>
 
" Toggle Caps Lock by pressing Ctrl-^, but only for insert and command mode.
 
let b:keymap_name = "CAPS"
 
loadkeymap
 
loadkeymap
 
a A
 
a A
Line 109: Line 54:
 
c C
 
c C
 
d D
 
d D
  +
e E
 
  +
...
f F
 
  +
g G
 
h H
 
i I
 
j J
 
k K
 
l L
 
m M
 
n N
 
o O
 
p P
 
q Q
 
r R
 
s S
 
t T
 
u U
 
v V
 
w W
 
x X
 
 
y Y
 
y Y
 
z Z
 
z Z
  +
 
A a
 
A a
 
B b
 
B b
  +
C c
 
  +
...
D d
 
  +
E e
 
F f
 
G g
 
H h
 
I i
 
J j
 
K k
 
L l
 
M m
 
N n
 
O o
 
P p
 
Q q
 
R r
 
S s
 
T t
 
U u
 
V v
 
W w
 
X x
 
Y y
 
 
Z z
 
Z z
 
</pre>
 
</pre>
   
and the following in my .vimrc:
+
Put the following in your [[vimrc]]:
 
 
<pre>
 
<pre>
" insert and command-line mode caps lock
+
" Insert and command-line mode Caps Lock.
  +
" Lock search keymap to be the same as insert mode.
  +
set imsearch=-1
  +
" Load the keymap that acts like capslock.
 
set keymap=insert-only_capslock
 
set keymap=insert-only_capslock
" turn it off by default
+
" Turn it off by default.
 
set iminsert=0
 
set iminsert=0
 
" Kill the capslock when leaving insert mode
 
autocmd InsertLeave * set iminsert=0
 
 
</pre>
 
</pre>
   
  +
The same <code>InsertLeave</code> autocmd defined for the language mappings can be used for this method.
I'm thinking about also changing my cursor color when in this "capslock" mode, from {{help|mbyte-keymap}}:
 
   
  +
You may want to include the following in your vimrc so the cursor color changes when "Caps Lock" is on:
 
<pre>
 
<pre>
 
:highlight Cursor guifg=NONE guibg=Green
 
:highlight Cursor guifg=NONE guibg=Green
Line 178: Line 87:
 
</pre>
 
</pre>
   
  +
Using the keymap procedure, the effect of pressing Ctrl-<code>^</code> is buffer local (so "Caps Lock" may be on in one buffer and off in another).
I don't know fully the advantages/disadvantages of this method vs. the lnoremap commands in the tip, so I'm not sure whether to present both methods in the tip, or just one in entirety while briefly mentioning the other. The InsertLeave autocmd and cursor stuff should probably be in the tip proper, and the 'iminsert' and 'imsearch' options mentioned briefly.
 
   
 
==References==
--[[User:Fritzophrenic|Fritzophrenic]] 14:43, 3 August 2009 (UTC)
 
 
*{{help|language-mapping}}
 
*{{help|i_CTRL-^}}
  +
 
==Comments==
  +
Status line information:
  +
<pre>
  +
" Set following to show "<CAPS>" in the status line when "Caps Lock" is on.
  +
let b:keymap_name = "CAPS"
  +
" Show b:keymap_name in status line.
  +
:set statusline^=%k
  +
</pre>
  +
  +
You can see the effect of the "language mappings" method by listing all language mappings with the command:
  +
<pre>
  +
:lmap
  +
</pre>
 
----
  +
One could also set a <code>lCursor</code> highlight different from the <code>Cursor</code> highlight, as a reminder that "pseudo-CapsLock mode" is on (however it is '''not''' possible to use different <code>lCursor</code> highlights for different keymaps used in parallel in different buffers, except maybe by means of fancy <code>BufEnter</code> autocommands); it could then (if one didn't play with <code>'iminsert'</code>) apply also to the operand of Normal-mode <code>r f t</code> and the like, and, with <code>'imsearch'</code> set to -1, to <code>/</code> and <code>?</code> searches.
  +
:Does this work with the lmaps or just with the keymap? This would be good to know. The buffer info is useful, but the highlight info is already in the tip. --[[User:Fritzophrenic|Fritzophrenic]] 18:31, September 1, 2009 (UTC)
  +
  +
::IIUC, <code>Cursor</code> vs. <code>lCursor</code> vs. <code>CursorIM</code> highlighting of the cursor depends on <code>'iminsert'</code> being set to 0, 1 or 2 respectively; so it ought to apply also to lmaps. The advantage of a keymap is that it will set <code>b:keymap_name</code> which will show on the statusline when keymaps/lmaps are active <code>(&imi == 1)</code>. If <code>b:keymap_name</code> is unset you get <code><lang></code> instead when <code>'imi'</code> equals 1.--[[User:Tonymec|Tonymec]] 19:53, September 1, 2009 (UTC)
  +
----
  +
With "national keyboards" other than en-US, hitting the <code>6</code> digit key while holding down the <code>Ctrl</code> key may or may not produce <code>Ctrl-^</code>, but you can e.g. use the following mappings to use <code>F5</code> instead in that case:
  +
<pre>
  +
noremap <F5> :let &l:imi = !&l:imi<CR>
 
inoremap <F5> <C-O>:let &l:imi = !&l:imi<CR>
  +
cnoremap <F5> <C-^>
  +
</pre>
  +
  +
----

Latest revision as of 08:21, 24 September 2012

Tip 1183 Printable Monobook Previous Next

created 2006 · complexity basic · author Gerald Lai · version 6.0


Pressing the Caps Lock key in Vim can cause trouble due to Vim's modal nature. Have you ever used Caps Lock in insert mode, then switched back to normal mode to issue a few commands, completely forgetting that Caps Lock is still on? Using this tip, you can enable Caps Lock only when typing text, eliminating those pesky <Esc>JJJJUUU commands you accidentally enter.

After implementing this tip, you do not need to use the Caps Lock key. Instead, when in insert mode, press Ctrl-^ (hold down Ctrl and press ^ – the 6 key on US keyboards) to toggle "Caps Lock" on or off.

This "Caps Lock" does not apply in normal mode. It only applies when:

  • Typing while in insert mode.
  • Typing in the command line.
  • Typing a search pattern.
  • Typing a character for commands like r and f.
  • Typing in the input() line.

Using language mappings[]

To use Ctrl-^ to toggle "Caps Lock", place the following in your vimrc (or just yank the lines in Vim, then type :@" to execute them):

" Execute 'lnoremap x X' and 'lnoremap X x' for each letter a-z.
for c in range(char2nr('A'), char2nr('Z'))
  execute 'lnoremap ' . nr2char(c+32) . ' ' . nr2char(c)
  execute 'lnoremap ' . nr2char(c) . ' ' . nr2char(c+32)
endfor

Since the idea is to allow you to forget about capslock, it may also be desirable to automatically turn off this special mode when exiting insert mode:

" Kill the capslock when leaving insert mode.
autocmd InsertLeave * set iminsert=0

Note the use of the 'iminsert' option. Pressing CTRL-^ actually toggles this option between 0 and 1, which automatically enables or disables your language maps. A related option, 'imsearch', controls the mappings while entering a search pattern.

Using a keymap[]

An alternative procedure that also causes Ctrl-^ to toggle "Caps Lock", is to use a keymap (this is not a mapping; see :help mbyte-keymap).

Using the 'keymap' option keeps vimrc clean and adds an indicator to the status line while in this "Caps Lock" mode.

Complete and place the following in file ~/.vim/keymap/insert-only_capslock.vim on Unix-based systems, or file $HOME/vimfiles/keymap/insert-only_capslock.vim on Windows systems:

" Toggle Caps Lock by pressing Ctrl-^, but only for insert and command mode.
let b:keymap_name = "CAPS"
loadkeymap
a A
b B
c C
d D

...

y Y
z Z

A a
B b

...

Z z

Put the following in your vimrc:

" Insert and command-line mode Caps Lock.
" Lock search keymap to be the same as insert mode.
set imsearch=-1
" Load the keymap that acts like capslock.
set keymap=insert-only_capslock
" Turn it off by default.
set iminsert=0

The same InsertLeave autocmd defined for the language mappings can be used for this method.

You may want to include the following in your vimrc so the cursor color changes when "Caps Lock" is on:

:highlight Cursor guifg=NONE guibg=Green
:highlight lCursor guifg=NONE guibg=Cyan

Using the keymap procedure, the effect of pressing Ctrl-^ is buffer local (so "Caps Lock" may be on in one buffer and off in another).

References[]

Comments[]

Status line information:

" Set following to show "<CAPS>" in the status line when "Caps Lock" is on.
let b:keymap_name = "CAPS"
" Show b:keymap_name in status line.
:set statusline^=%k

You can see the effect of the "language mappings" method by listing all language mappings with the command:

:lmap

One could also set a lCursor highlight different from the Cursor highlight, as a reminder that "pseudo-CapsLock mode" is on (however it is not possible to use different lCursor highlights for different keymaps used in parallel in different buffers, except maybe by means of fancy BufEnter autocommands); it could then (if one didn't play with 'iminsert') apply also to the operand of Normal-mode r f t and the like, and, with 'imsearch' set to -1, to / and ? searches.

Does this work with the lmaps or just with the keymap? This would be good to know. The buffer info is useful, but the highlight info is already in the tip. --Fritzophrenic 18:31, September 1, 2009 (UTC)
IIUC, Cursor vs. lCursor vs. CursorIM highlighting of the cursor depends on 'iminsert' being set to 0, 1 or 2 respectively; so it ought to apply also to lmaps. The advantage of a keymap is that it will set b:keymap_name which will show on the statusline when keymaps/lmaps are active (&imi == 1). If b:keymap_name is unset you get <lang> instead when 'imi' equals 1.--Tonymec 19:53, September 1, 2009 (UTC)

With "national keyboards" other than en-US, hitting the 6 digit key while holding down the Ctrl key may or may not produce Ctrl-^, but you can e.g. use the following mappings to use F5 instead in that case:

noremap  <F5> :let &l:imi = !&l:imi<CR>
inoremap <F5> <C-O>:let &l:imi = !&l:imi<CR>
cnoremap <F5> <C-^>