In this tutorial, I will be discussing how we can make text uppercase in Vim. Additionally, we will see various case-switching options.
- Command Syntax to Case to Upper in Vim
- Changing to Uppercase in Vim – Examples
- List of Commands to Change Case In the Vim Editor
- Conclusion
Note: For this tutorial, I am using Vim on macOS; instructions will remain the same for Vim on other operating systems as well.
Command Syntax to Case to Upper in Vim
The syntax to use to switch case to upper in Vim is mentioned below:
In the above syntax, gU is a Vim operator, while <motion> can be replaced with motion parameters such as j, k, e, w, and so on.
To toggle the case in Vim tilde (~) key is used, if the ~ key is not mapped then use the following syntax:
If the ~ key is set for some other functionality then use:
Some commonly used motions are given in the table below:
To learn more about motion operators and parameters run :help cursor-motion command in the Vim editor.
Changing to Uppercase in Vim – Examples
Let’s implement case switching with examples in the Vim editor.
1. Change the Current Character to Uppercase
To switch the case of the current character to uppercase use tilde (~) only if the character is in lowercase. The ~ key is used to toggle the case in Vim. The term current character refers to the character that is currently under the cursor.
First, get into the NORMAL mode and press the ~ key.
The character will be in uppercase now and the cursor is shifted to the next character. If you hold down the ~ key, the letters will switch to uppercase one after the other.
2. Change the Current Word to Uppercase
To change the case of an entire word, use the gUaw command while keeping the cursor anywhere in the word.
3. Change the Characters to the End of a Word to Uppercase
To change the case of all the characters to the end of a word use the gUw command. It depends upon your cursor position on the word. All the characters after the cursor and a character under the cursor will switch to uppercase.
4. Change the Current Line to Uppercase
To switch the case of the entire line to uppercase in the Vim editor use the gUU command. When in NORMAL mode, place the cursor on the desired line and press gUU to convert it to uppercase.
5. Change From Cursor to the End of the Sentence to Uppercase
Use the command gU) or gU$ in NORMAL mode to change all the characters and words from the current position of the character till the end of the sentence.
This command will also include the character under the cursor.
6. Change From Cursor to the End of the Paragraph to Uppercase
If you want to change the case of all the characters and words to the end of the paragraph then use the gU} command in the NORMAL mode.
7. Change X Number of Lines Below and Above the Cursor to Uppercase
If you want a specific number of lines to switch to uppercase below the current position of the cursor then use gU<X>j command in the NORMAL mode. The X can be replaced with the number of lines. For example, if I put the cursor on line 2 and run the command gU2j, the output will be:
This command will also include the line under the cursor.
To make all the lines above the current cursor uppercase, use the gU<X>k command, and replace <X> with a number.
Note: Vim editor treats paragraphs as lines, if your document is formatted as paragraphs then the commands mentioned in this section will make the paragraphs uppercase.
8. Change From Cursor to the End Document to Uppercase
To change the entire document from the cursor to the end of the document to uppercase, use the gUG command.
If you want to make the entire document uppercase just bring your cursor anywhere in the first paragraph of the document and press gUG.
List of Commands to Change Case In the Vim Editor
Some useful commands for case changing in Vim are given in the following image.
Note: All the commands mentioned in the above image are changing case from the cursor position. Commands also refer to the current position of the cursor, whether it’s on a specific character or a whole line.
Conclusion
When it comes to providing modern word processor-like functionality Vim is not far behind. Changing case in Vim is one of the key operations while editing documents. The case of letters, words, sentences, paragraphs, and even the entire document can be changed to uppercase using the gU motion operator with options.