Vim Tips Wiki
(tweak a little and respond to comments)
 
(6 intermediate revisions by 4 users not shown)
Line 2: Line 2:
 
|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=6.0
 
|version=6.0
 
|rating=8/2
 
|rating=8/2
|category1=Usage
+
|category1=Map
|category2=Map
+
|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 <tt><Esc>JJJJUUU</tt> commands you accidentally enter.
+
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.
   
After implementing this tip, you do not need to use the Caps Lock key. Instead, when in insert mode, press Ctrl-<tt>^</tt> (hold down Ctrl and press <tt>^</tt> – the <tt>6</tt> key on US keyboards) to toggle "Caps Lock" on or off.
+
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:
 
This "Caps Lock" does not apply in normal mode. It only applies when:
Line 19: Line 19:
 
*Typing in the command line.
 
*Typing in the command line.
 
*Typing a search pattern.
 
*Typing a search pattern.
*Typing a character for commands like <tt>r</tt> and <tt>f</tt>.
+
*Typing a character for commands like <code>r</code> and <code>f</code>.
*Typing in the <tt>input()</tt> line.
+
*Typing in the <code>input()</code> line.
   
 
==Using language mappings==
 
==Using language mappings==
To use Ctrl-<tt>^</tt> to toggle "Caps Lock", place the following in your [[vimrc]] (or just yank the lines in Vim, then type <tt>:@"</tt> to execute them):
+
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.
 
" Execute 'lnoremap x X' and 'lnoremap X x' for each letter a-z.
Line 32: Line 32:
 
</pre>
 
</pre>
   
Since the idea is to allow you to forget about capslock, it may also be desireable to automatically turn off this special mode when exiting insert mode:
+
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:
 
<pre>
 
<pre>
" Kill the capslock when leaving insert mode
+
" Kill the capslock when leaving insert mode.
 
autocmd InsertLeave * set iminsert=0
 
autocmd InsertLeave * set iminsert=0
 
</pre>
 
</pre>
   
Note the use of the <tt>'iminsert'</tt> option. Pressing <tt>CTRL-^</tt> actually toggles this option between 0 and 1, which automatically enables or disables your language maps. A related option, <tt>'imsearch'</tt>, controls the mappings while entering a search pattern.
+
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.
   
==Using a keymap==
+
==Using a [[How_to_make_a_keymap|keymap]]==
An alternative procedure that also causes Ctrl-<tt>^</tt> to toggle "Caps Lock", is to use a keymap (this is ''not'' a mapping; see {{help|mbyte-keymap}}).
+
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}}).
   
One could also use the 'keymap' option to accomplish the same thing, while [[Keep your vimrc file clean|keeping their vimrc clean]] and additionally adding an indicator to the status line while in this "capslock" mode.
+
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.
   
Complete and place the following in file <tt>~/.vim/keymap/insert-only_capslock.vim</tt> on Unix-based systems, or file <tt>$HOME/vimfiles/keymap/insert-only_capslock.vim</tt> on Windows systems:
+
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>
 
<pre>
 
" Toggle Caps Lock by pressing Ctrl-^, but only for insert and command mode.
 
" Toggle Caps Lock by pressing Ctrl-^, but only for insert and command mode.
Line 79: Line 79:
 
</pre>
 
</pre>
   
The same InsertLeave autocmd defined for the language mappings can be used for this method.
+
The same <code>InsertLeave</code> 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:
 
You may want to include the following in your vimrc so the cursor color changes when "Caps Lock" is on:
Line 87: Line 87:
 
</pre>
 
</pre>
   
Using the keymap procedure, the effect of pressing Ctrl-<tt>^</tt> is buffer local (so "Caps Lock" may be on in one buffer and off in another).
+
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).
   
 
==References==
 
==References==
Line 94: Line 94:
   
 
==Comments==
 
==Comments==
{{todo}}
 
*Edit conflict: Fritzophrenic, sorry but I tried to save a major cleanup just after you. Needs checking because I had changed some of your text.
 
*Please check what I've done! Some more polishing should occur, and perhaps merge following. I'm not sure if it is worth keeping the "Using a keymap" section. [[User:JohnBeckett|JohnBeckett]] 04:11, 1 September 2009 (UTC)
 
::I like a lot of your changes, but I'll certainly do some more polishing when I get a chance. I like the keymap section, though you show how to get similar functionality below, so perhaps it isn't needed. I do think it should be fairly obvious what the pattern is and we don't need to include the entire keymap file, so I have shortened it. I personally think it is clear enough this way and that it detracts from the point of the tip when listing the entire file, but let me know if you disagree. --[[User:Fritzophrenic|Fritzophrenic]] 18:31, September 1, 2009 (UTC)
 
 
 
Status line information:
 
Status line information:
 
<pre>
 
<pre>
 
" Set following to show "<CAPS>" in the status line when "Caps Lock" is on.
 
" Set following to show "<CAPS>" in the status line when "Caps Lock" is on.
let b:keymap_name = 'CAPS'
+
let b:keymap_name = "CAPS"
 
" Show b:keymap_name in status line.
 
" Show b:keymap_name in status line.
 
:set statusline^=%k
 
:set statusline^=%k
 
</pre>
 
</pre>
 
Use the following to always disable "Caps Lock" when Esc is pressed:
 
<pre>
 
:inoremap <silent> <Esc> <Esc>:set imi=0<CR>
 
:vnoremap <silent> <Esc> <Esc>:set imi=0<CR>
 
</pre>
 
 
:I don't think the above two mappings are very good to include. The insertleave autocmd mentioned in the tip is better than the imap, and unless I'm mistaken there isn't really any reason for the vmap. These mappings don't apply in visual mode anyway! --[[User:Fritzophrenic|Fritzophrenic]] 18:31, September 1, 2009 (UTC)
 
   
 
You can see the effect of the "language mappings" method by listing all language mappings with the command:
 
You can see the effect of the "language mappings" method by listing all language mappings with the command:
Line 119: Line 106:
 
:lmap
 
:lmap
 
</pre>
 
</pre>
  +
----
 
One could also set a <tt>lCursor</tt> highlight different from the <tt>Cursor</tt> highlight, as a reminder that "pseudo-CapsLock mode" is on (however it is '''not''' possible to use different <tt>lCursor</tt> highlights for different keymaps used in parallel in different buffers, except maybe by means of fancy <tt>BufEnter</tt> autocommands); it could then (if one didn't play with <tt>'iminsert'</tt>) apply also to the operand of Normal-mode <tt>r f t</tt> and the like, and, with <tt>'imsearch'</tt> set to -1, to <tt>/</tt> and <tt>?</tt> searches.
+
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)
 
: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 <tt>6</tt> digit key while holding down the <tt>Ctrl</tt> key may or may not produce <tt>Ctrl-^</tt>, but you can e.g. use the following mappings to use <tt>F5</tt> instead in that case:
 
  +
----
 
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>
 
<pre>
 
noremap <F5> :let &l:imi = !&l:imi<CR>
 
noremap <F5> :let &l:imi = !&l:imi<CR>

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-^>