How to set the initial caps in VIM?

In VIM, it is very easy to change the word of the text to use uppercase or lowercase letters:

# in visual mode # change word to uppercase gUw # change word to lowercase guw 

Is there an easy way to change the word to use the initial caps?

+4
source share
6 answers

Assuming the cursor is at the beginning of a word, use

 gUl 

(if the word was lowercase) or

 gUllgue 

to explicitly create the first letter capital and another lower case register.

Same as you used, instead of w (word motion) you use l (single character movement).

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

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

+4
source

I suggest going to the beginning of the word with any movement commands (s), and then pressing ~ . The tildeop option affects this behavior, see :help ~ and :help tildeop for more information.

+3
source

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

  • Use ~ to switch the case of the letter under your cursor.
  • Use :s/\<\(\w\)\(\w*\)\>/\u\1\L\2/ to search for a word, in upper case of the first letter and lower case of the rest.
  • guiwgUl to omit the word your cursor is in, then the upper case of the first letter.
+2
source

If you use the word:

 bgUl 

If you are at the beginning of a word:

 gUl 

Unpacking: b returns one word (or to the beginning of the word you are in), gU upper part of the movement, l moves to the right one character (which will be the first letter in the word).

+1
source

Side note: I have a plugin (well, thatโ€™s not its main purpose), able to convert names between camel case, underlining separated words, etc. Move the cursor to the identifier and enter :NameConvert lower_camel_case , for example (the command supports completion ( <tab> , <cd> ) to display all possible naming schemes)

To install it, you will need lh-dev and LH-VIM Pb .

0
source

There is a toupper function that you can use to convert. Even in substitution, you can use it. For example, find all the initial sentences and convert the first character to uppercase, as described here: search and replace

0
source

Source: https://habr.com/ru/post/1314856/


All Articles