21

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,604
  • 6
  • 56
  • 100
Patrick
  • 4,012
  • 9
  • 31
  • 43

4 Answers4

16
:nmap <whatever> m`b~``
Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
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,652
  • 1
  • 23
  • 32
  • 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
  • 6,978
  • 7
  • 30
  • 47
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
  • 72,441
  • 71
  • 163
  • 242