Vim Tips Wiki
(Move categories to tip template)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(One intermediate revision by the same user not shown)
Line 4: Line 4:
 
|previous=902
 
|previous=902
 
|next=904
 
|next=904
|created=March 28, 2005
+
|created=2005
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Gerald Lai
 
|author=Gerald Lai
Line 20: Line 20:
 
When it is in insert mode, the cursor touches the alphabet beside it. A few examples below (assume pipe character as cursor):
 
When it is in insert mode, the cursor touches the alphabet beside it. A few examples below (assume pipe character as cursor):
   
*<tt>foo| bar</tt> &ndash; "foo" gets capitalized
+
*<code>foo| bar</code> &ndash; "foo" gets capitalized
*<tt>foo |bar</tt> &ndash; "bar" gets capitalized
+
*<code>foo |bar</code> &ndash; "bar" gets capitalized
*<tt>foo b|ar</tt> &ndash; "bar" gets capitalized
+
*<code>foo b|ar</code> &ndash; "bar" gets capitalized
*<tt>foo bar | more</tt> &ndash; no word gets capitalized
+
*<code>foo bar | more</code> &ndash; no word gets capitalized
   
 
Cursor stays where it is after capitalization, in its current mode.
 
Cursor stays where it is after capitalization, in its current mode.
Line 29: Line 29:
 
<pre>
 
<pre>
 
"Place in vimrc file
 
"Place in vimrc file
"&lt;F7&gt; for word case toggle &amp; &lt;F8&gt; for word capitalization
+
"<F7> for word case toggle & <F8> for word capitalization
 
"Normal mode mappings:
 
"Normal mode mappings:
nmap &lt;F7&gt; mzg~iw`z
+
nmap <F7> mzg~iw`z
nmap &lt;F8&gt; mzgUiw`z
+
nmap <F8> mzgUiw`z
   
 
"Insert mode mappings:
 
"Insert mode mappings:
imap &lt;F7&gt; _&lt;Esc&gt;mza&lt;C-Right&gt;&lt;Esc&gt;bg~iw`zi&lt;Del&gt;
+
imap <F7> _<Esc>mza<C-Right><Esc>bg~iw`zi<Del>
imap &lt;F8&gt; _&lt;Esc&gt;mza&lt;C-Right&gt;&lt;Esc&gt;bgUiw`zi&lt;Del&gt;
+
imap <F8> _<Esc>mza<C-Right><Esc>bgUiw`zi<Del>
 
</pre>
 
</pre>
   
Line 45: Line 45:
   
 
<pre>
 
<pre>
imap &lt;F8&gt; _&lt;Esc&gt;mzwbg~iw`zi&lt;Del&gt;
+
imap <F8> _<Esc>mzwbg~iw`zi<Del>
imap &lt;S-F8&gt; _&lt;Esc&gt;mzwbgUiw`zi&lt;Del&gt;
+
imap &lt;S-F8> _<Esc>mzwbgUiw`zi<Del>
 
</pre>
 
</pre>
   
Line 53: Line 53:
   
 
<pre>
 
<pre>
imap &lt;F8&gt; _&lt;Esc&gt;mzi&lt;S-Right&gt;&lt;C-o&gt;b&lt;C-o&gt;g~iw&lt;C-o&gt;`z&lt;Del&gt;
+
imap <F8> _<Esc>mzi&lt;S-Right><C-o>b<C-o>g~iw<C-o>`z<Del>
imap &lt;S-F8&gt; _&lt;Esc&gt;mzi&lt;S-Right&gt;&lt;C-o&gt;b&lt;C-o&gt;gUiw&lt;C-o&gt;`z&lt;Del&gt;
+
imap &lt;S-F8> _<Esc>mzi&lt;S-Right><C-o>b<C-o>gUiw<C-o>`z<Del>
 
</pre>
 
</pre>
   

Latest revision as of 05:55, 13 July 2012

Tip 903 Printable Monobook Previous Next

created 2005 · complexity intermediate · author Gerald Lai · version 5.7


For those of you who require a way of capitalizing (or toggling the case) of words in either insert or normal mode but want the cursor to stay put where it currently is, you're in luck.

The mappings below work in such a way where the word being capitalized is the word where one of its alphabets is being "touched" by the cursor.

When it is in normal mode, the cursor only touches the alphabet underneath it (i.e. alphabet under flashing cursor block).

When it is in insert mode, the cursor touches the alphabet beside it. A few examples below (assume pipe character as cursor):

  • foo| bar – "foo" gets capitalized
  • foo |bar – "bar" gets capitalized
  • foo b|ar – "bar" gets capitalized
  • foo bar | more – no word gets capitalized

Cursor stays where it is after capitalization, in its current mode.

"Place in vimrc file
"<F7> for word case toggle & <F8> for word capitalization
"Normal mode mappings:
nmap <F7> mzg~iw`z
nmap <F8> mzgUiw`z

"Insert mode mappings:
imap <F7> _<Esc>mza<C-Right><Esc>bg~iw`zi<Del>
imap <F8> _<Esc>mza<C-Right><Esc>bgUiw`zi<Del>

Type "~" for togglecase, "U" for uppercase, "u" for lowercase.

Comments[]

These ones seem to work better than the last. Updated insert mode mappings:

imap <F8> _<Esc>mzwbg~iw`zi<Del>
imap <S-F8> _<Esc>mzwbgUiw`zi<Del>

The last mappings broke for words at the end of the file.

imap <F8> _<Esc>mzi<S-Right><C-o>b<C-o>g~iw<C-o>`z<Del>
imap <S-F8> _<Esc>mzi<S-Right><C-o>b<C-o>gUiw<C-o>`z<Del>