If you’re comfortable with basic Vim commands and want to take your skills to the next level, this guide will help you unlock some of the best tools Vim has to offer. Whether you’re programming, editing files, or working on large projects, these commands will make your workflow lightning-fast. In this post, we’ll cover six main categories of commands: file navigation, searching, grepping, editing, and autocomplete, all using vanilla Vim—no custom configuration required.
I also recommend checking out my YouTube Video on Practical Vim Commands.
Let’s dive in!
1. Navigating Between Files
Vim offers powerful commands to switch between files, which can significantly enhance your workflow.
-
:e
(Open a File)
To open a new file in Vim, use:e
. You can use the Tab key for auto-completion to find the file you need quickly.:e filename
Press
Tab
to cycle through suggestions based on your current working directory (:pwd
to check the current path). -
<C-^>
(Toggle Between Files)
You can quickly switch between the current file and the previous one usingControl-6
(<C-^>
). This is handy when you want to flip back and forth between two files. -
:tabe
(Open File in a New Tab)
Just like browser tabs, Vim lets you open files in separate tabs using:tabe
::tabe filename
Use
gt
to navigate to the next tab andgT
to go to the previous tab. This makes managing multiple files even easier.
2. Searching in Files
Searching within a file is crucial for navigating large files.
-
/
(Forward Search)
Use/
followed by the string you’re searching for. PressEnter
, and Vim will take you to the first occurrence. Pressn
to move to the next occurrence andN
(uppercase) to go back./search_string
-
?
(Backward Search)
Searching in the opposite direction is done with?
:?search_string
Just like
/
, pressingn
will take you to the next occurrence, andN
will reverse the direction. -
*
(Search Word Under Cursor)
To save yourself from typing the search term, simply place the cursor on a word and press*
. This will automatically search for that word across the file.n
andN
work the same way here for navigation.
3. Grepping Across Multiple Files
When working on larger projects, it’s often necessary to search across many files. Vim’s built-in grep tool, :vimgrep
, is perfect for this.
:vimgrep
(Search Multiple Files)
Use:vimgrep
to search for a specific pattern across multiple files. You can specify the pattern and file type:This command will search for the pattern in all Java files within the current directory and subdirectories. You can jump between the results using the commands:vimgrep /pattern/ **/*.java
:cnext
(:cn
) to move forward and:cprev
(:cp
) to move backward. Use:cfirst
and:clast
to navigate directly to the first or last result.
4. Editing Text Quickly
Vim provides incredible commands for quickly editing text within delimiters or specific parts of a line.
-
ci
(Change Inside Delimiters)
Theci
command is a huge time-saver when editing text inside quotes, brackets, or other delimiters. For example:ci"
: Change everything inside the quotes.ci(
: Change everything inside the parentheses.ci{
: Change everything inside the curly braces.
This command deletes the text inside the delimiter and puts you into insert mode so that you can immediately start typing your new content.
5. Autocomplete Without Plugins
Autocomplete helps speed up typing, especially when repeating long variable names or function calls.
-
<C-n>
(Autocomplete Next)
While typing, pressControl-n
(<C-n>
) to get a list of suggestions based on words already present in your file. This is an excellent way to reuse variable names or method names without typing them again. -
<C-p>
(Autocomplete Previous)
PressControl-p
(<C-p>
) to cycle backward through the autocomplete suggestions.
These commands work seamlessly in any file and don’t require any additional plugins or configurations.
6. Centering Lines
When navigating through a file, you might want the current line to appear in the center of the screen for better readability.
zz
(Center the Cursor Line)
Pressingzz
will center the current line in the middle of the screen, making it easier to focus on the context.
Putting It All Together
Let’s say you’re working on a Java project, and you want to search for all instances of a method named main
. Here’s how you might do it:
- Use
:vimgrep /main/ **/*.java
to find all occurrences ofmain
in your project. - Use
:cnext
(:cn
) and:cprev
(:cp
) to jump between occurrences. - Once you find the correct instance, use
/
to search for a specific string within the file, then*
to jump between identical instances.
Next, if you need to modify some text:
- Place the cursor inside a string and press
ci"
to change the text inside the quotes. - If you need to autocomplete a variable name, start typing and use
<C-n>
to see suggestions.
Conclusion
Mastering these commands will drastically improve your productivity in Vim. They allow you to navigate, edit, and manage files quickly, enabling a smooth and efficient editing experience. Even better, all these commands work out of the box with vanilla Vim—no configuration, no plugins—just the power of Vim itself.
If you’re new to these commands, practice them consistently, and soon they’ll become second nature. The more you use them, the faster and more efficient your workflow will become. Embrace the power of Vim, and happy coding!