views:

67

answers:

6

In VIM, it's really easy to change a word of text to use uppercase or lowercase:

# in visual mode

# change word to uppercase
gUw

# change word to lowercase
guw

Is there a simple way to modify the word to use initial caps?

+1  A: 

If you're on the word:

bgUl

If you're at the beginning of the word:

gUl

Unpacking that: b goes back one word (or to the beginning of the word you're on), gU upcases over movement, l moves right one character (which will be the first letter in the word).

wdebeaum
+1  A: 

Assuming cursor is at the beginning of the word, use

gUl

(if the word was all-lowercase) or

gUllgue

to explicitly make the first letter capital and other lower case.

It's the same that you used, only instead of w (word motion) you use l (one symbol motion).

If the cursor is somewhere in the middle of the word, prepend b (go to the beginning of the word) to the commands above.

You can map some key to do this if you use it often.

Roman Cheplyaka
+1  A: 

Depending on what your use-case is, any of the following may work.

  1. Use ~ to toggle the case of the letter under your cursor.
  2. Use :s/\<\(\w\)\(\w*\)\>/\u\1\L\2/ to search for a word, upper-case the first letter, and lower-case the rest.
  3. guiwgUl to lower-case the word your cursor is on and then upper-case the first letter.
jamessan
+2  A: 

I'd suggest moving to the beginning of the word with whatever motion command(s) you want, then pressing ~. This behavior is affected by the tildeop option, see :help ~ and :help tildeop for more info.

Randy Morris
A: 

Side note: I have a plugin (well, it's not its main purpose though) that is able to convert names between camel case, underscore separated words, etc. Move the cursor on an identifier, and type :NameConvert lower_camel_case for instance (the command supports completion (<tab>, <c-d>) to display all the possible naming schemes)

To install it, you'll need lh-dev, and lh-vim-lib.

Luc Hermitte
A: 

There is a function toupper which you can use to make conversion. Even in substitution you can use that. Like find all sentence beginnings, and convert the first character to upper case, as explained here: search and replace

thegeek