22

For a long time I've known that I can use ~ in Vim to toggle the case of a character. But is there a way to map a key to capitalize a word and go back to the previous position?

For example:

I like to drink Coca co[l]

If my cursor is at "l" and I realize I need to make the "c" capitalize as well, currently, I need to do:

<C-c> b ~ ll i

Is there a way to map a single key to make the first letter of the word under cursor to be capitalized and keep the cursor at original position?

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
Patrick
  • 4,186
  • 9
  • 32
  • 45

5 Answers5

16
:nmap <whatever> m`b~``
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 2
    it is better to make it m`lb~`` so that you can have the same effect even if you are already on the first character of the word. Using it myself – Hennadii Madan Jun 08 '16 at 11:39
  • thanks, but it doesn't work in visual mode (with `vmap`). –  Feb 26 '17 at 19:53
11

Personally, this is exactly the kind of thing I prefer not to make a macro for. There's no need to fill up your .vimrc with dozens of such one-off solutions, because the solution flows so naturally from the "toolbox" of standard Vim commands, that you can just string it together like second nature.

I'm typing a long word:

the gallicizatio|

(the | is the position of the cursor). Suddenly I realize that I forgot to capitalize the G of "Gallicization". So, bam!, I hit ESC (which is mapped to the caps lock key on my keyboard, so it takes only a flick of the pinky), followed by b~A, and I continue typing as if nothing happened. That errant g was capitalized in the time it would take an Emacs user to begin moving his right hand toward the arrow keys, and I've already moved on to the rest of the sentence.

In contrast, with a macro that I haven't used in a while, it would probably take me more time just to remember what key(s) I assigned to that macro. The better solution is to learn the important "core" commands very well, which can be combined on the fly according to simple rules, with millions of possible effects.

Maxy-B
  • 2,772
  • 1
  • 25
  • 33
  • Thanks for the answer! Though, to me, remembering a built-in command sequence is no difference then remembering a mapping or macro created by me. Although on a new machine, without preferred mapping, one has to revert back to whatever he remembers on the built-in commands :) – Patrick Aug 11 '12 at 17:59
  • 2
    @Patrick: the point is that you *don't* need to remember the built-in command sequence (`b~A` in this case). You remember a relatively small number of individual built-in commands, and the *rules* for combining them in unlimited ways. The same way that you can construct infinite, novel sentences of English by remembering a finite number of morphemes and the rules for combining them. – Maxy-B Nov 08 '12 at 20:19
  • an Emacs user would do `M-b` `M-c` to get same result and not moving to arrow keys – Carlo Espino Oct 19 '14 at 18:36
3

I find myself doing this often, and using the EX command line to got through multiple records that match a condition. In this case, I use a backreference that looks like this:

:%s/\(\w\)\(\w*\)/\U\1\L\2/g

and BOOM, problem solved across all the words that are in a certain context.

EDIT: Look here too, just realized there was this link which has similar answers:

Capitalize first letter of each word in a selection using vim

Community
  • 1
  • 1
ovatsug25
  • 7,786
  • 7
  • 34
  • 48
2

you can also use macro

q<register> <C-c> b ~ ll i q

and then do @<register> every time you need to use it.

Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
0

Change the upper/lower cases of many lines

You can change the first letter from lower to upper or vice versa using

step -1-

Highlight all the lines that you want to change using :<,> or using ctrl+v then j or k

step -2-

Use l or h to adjust the highlight to first letter of each line

step -3-

From upper cases to lower cases use gu From lower cases to upper cases use gU this will work for all the letters that you will highlight Note: You can use

  • gu (upper -> lower)
  • gU (lower -> upper) and
  • g~ to switch between the cases (upper -> lower or lower -> upper)
Dr Neo
  • 724
  • 6
  • 11