(adjust previous/next navigation + minor manual clean) |
Ligelowbee (talk | contribs) No edit summary Tag: Visual edit |
||
(8 intermediate revisions by 5 users not shown) | |||
Line 12: | Line 12: | ||
|category2= |
|category2= |
||
}} |
}} |
||
− | All of you should know the gu{motion} and gU{motion} commands used to convert a region to lower/upper case. |
+ | All of you should know the gu{motion} and gU{motion} commands used to convert a region to lower/upper case. This tip explains how to capitalize a region. |
Similar to the gu/gU commands, here are some mappings for "gc" (c as in capitalize). |
Similar to the gu/gU commands, here are some mappings for "gc" (c as in capitalize). |
||
− | To capitalize the word "example" place the cursor over the word and type < |
+ | To capitalize the word "example" place the cursor over the word and type <code>gciw</code> (similar to <code>guiw</code>). |
<pre> |
<pre> |
||
Line 69: | Line 69: | ||
endif |
endif |
||
</pre> |
</pre> |
||
+ | |||
+ | If you would like every word in a selected visual area, or line, to be capitalized (instead of just the first word, which the above does) replace <code>gcgc</code>, <code>gcc</code>, <code>gc</code>, and <code>gc$</code> with: |
||
+ | |||
+ | <pre> |
||
+ | " Capitalize every word on the line |
||
+ | nmap gcc :s/\v<(.)(\w*)/\u\1\L\2/g<CR> |
||
+ | nmap gcgc gcc |
||
+ | |||
+ | " Capitalize every word in the visual selection. |
||
+ | " Since the visual selection range ('<,'>) is linewise |
||
+ | " need \%V to limit match to only characters in selection. |
||
+ | vmap gc :s/\%V\v<(.)(\w*)/\u\1\L\2/g<CR> \| `< |
||
+ | |||
+ | " Capitalize every word from current to last on line |
||
+ | " (needs gc mapping from above) |
||
+ | nmap gc$ viW$gc |
||
+ | </pre> |
||
+ | |||
+ | The search and replace regular expression is straight from <code>:help case</code> |
||
+ | |||
+ | [https://newbedev.com/capitalize-first-letter-of-each-word-in-a-selection-using-vim A full explaination of the regular expression can be found here.] |
||
==Comments== |
==Comments== |
Latest revision as of 17:10, 7 January 2022
created 2005 · complexity basic · author Soeren Sonntag · version 6.0
All of you should know the gu{motion} and gU{motion} commands used to convert a region to lower/upper case. This tip explains how to capitalize a region.
Similar to the gu/gU commands, here are some mappings for "gc" (c as in capitalize).
To capitalize the word "example" place the cursor over the word and type gciw
(similar to guiw
).
Before: this is an example. After: this is an Example.
To capitalize the whole line place the cursor somewhere on the line and either type gcgc or gcc (similar to gugu/guu).
Before: this is an Example. After: This is an example.
I defined some common mappings. Please feel free to add your own.
gcw - capitalize word (from cursor position to end of word) gcW - capitalize WORD (from cursor position to end of WORD) gciw - capitalize inner word (from start to end) gciW - capitalize inner WORD (from start to end) gcis - capitalize inner sentence gc$ - capitalize until end of line (from cursor postition) gcgc - capitalize whole line (from start to end) gcc - capitalize whole line {Visual}gc - capitalize highlighted text
Put the following lines into your vimrc file.
if (&tildeop) nmap gcw guw~l nmap gcW guW~l nmap gciw guiw~l nmap gciW guiW~l nmap gcis guis~l nmap gc$ gu$~l nmap gcgc guu~l nmap gcc guu~l vmap gc gu~l else nmap gcw guw~h nmap gcW guW~h nmap gciw guiw~h nmap gciW guiW~h nmap gcis guis~h nmap gc$ gu$~h nmap gcgc guu~h nmap gcc guu~h vmap gc gu~h endif
If you would like every word in a selected visual area, or line, to be capitalized (instead of just the first word, which the above does) replace gcgc
, gcc
, gc
, and gc$
with:
" Capitalize every word on the line nmap gcc :s/\v<(.)(\w*)/\u\1\L\2/g<CR> nmap gcgc gcc " Capitalize every word in the visual selection. " Since the visual selection range ('<,'>) is linewise " need \%V to limit match to only characters in selection. vmap gc :s/\%V\v<(.)(\w*)/\u\1\L\2/g<CR> \| `< " Capitalize every word from current to last on line " (needs gc mapping from above) nmap gc$ viW$gc
The search and replace regular expression is straight from :help case
A full explaination of the regular expression can be found here.
Comments[]
Of course, there's the cream-capitalize script script#242.
I would appreciate a mapping to capitalize a word in insert mode, after having typed half a word without having to change modes.
exampl<~>e to Example<cursor_insert_mode>
:imap <F8> <Esc>g~iwea
This command toggles the case of every character of a word in insert mode. Replace "~" with "U" for uppercase or "u" for lowercase change.