9 Essential Vim Tips and Tricks for Advanced Users
The Vim text editor is packed with an array of features, making it challenging to master all of them. In this guide, we’ll explore essential tips and tricks that can enhance your text editing efficiency in Vim right now.
1. Managing Multiple Files
If you’re a programmer or rely on Vim for editing, you’re likely to handle several files at once. Here are some effective techniques for navigating multiple files in Vim.
Rather than opening each file in a separate terminal tab, you can load them all in a single tab by listing their names with the vim command:
This will create distinct buffers for each of the three files within Vim.
Utilize the :next
or :n
command to advance to the subsequent file, and the :prev
or :N
command to return to the previous one.
To jump to the first or last file directly, employ :bf
for the first file and :bl
for the last.
To start editing another file, execute the :e
command followed by the file name.
The :ls
command is useful for displaying all buffers that are currently open in your Vim session.
2. Enhance Efficiency with Auto Completes
Looking to save time while increasing accuracy? Abbreviations in Vim can significantly help by streamlining long or frequently typed words. You can create an abbreviation using the ab
command.
For instance, executing the following command replaces “asap”with “as soon as possible”:
You can also utilize abbreviations to fix common typos. For example, :ab recieve receive
will correct the spelling mistake automatically while you type.
If you want to bypass an abbreviation at specific moments, use Ctrl + V followed by the Space key after the word’s last character.
To eliminate an abbreviation, use the una
command. For instance, :una asap
will remove the abbreviation for “as soon as possible.”
To ensure your abbreviations persist for future sessions, add the full ab
command (without the leading colon) to your “~/.vimrc” file.
3. Use Split Windows for Easy Copying and Pasting
When copying code or text from one file to another, GUI editors make the process straightforward, but command-line editing can be cumbersome. Thankfully, Vim offers ways to simplify this task.
Begin by opening one file, then split the Vim window to add another file using the split
command, such as this:
This command performs a horizontal split of the Vim window. For a vertical split, simply use the vsplit
command instead. Once both files are opened, copy content from one by pressing Ctrl + W to switch focus to the other file, and then paste the content.
4. Save a File without Modify Permissions
Occasionally, you might find yourself unable to save changes to a file because it’s read-only. While you can close the file and reopen it with the correct permissions, this risks losing any edits you’ve previously made.
A better solution is to use the :w
command, followed by the external Bash command that saves your file with superuser rights:
This will prompt you for your password, afterwhich Vim saves your current edits.
You can also return to the system shell while editing by executing the :sh
command from within Vim.
When you are finished, type exit
to return to your Vim session.
5. Maintain Indentation While Copying and Pasting
Many developers rely on Vim’s auto-indentation feature, which is generally helpful, but can complicate pasting indented code. For example, pasting indented code into a Vim session with auto-indent enabled can result in misalignment.
You can address this by adding the pastetoggle
option to Vim’s configuration file. Open your vimrc file, then append the following line:
Save your vimrc, then open the target file and enter Insert Mode. Press F2 before pasting your indented code to maintain its original formatting.
6. Utilize Text Object Commands
Another valuable strategy to enhance your editing efficiency in Vim is to employ “text object commands”. These commands consider the context in which your cursor resides, allowing for precise operations within the text’s structure.
A particularly handy text object command is I
, which works in tandem with motion keys like W. For example, use C + I followed by W to modify the word situated under the cursor.
The I
command is also effective with other text delimiters. For instance, using C + I and pressing Shift + 9 will clear text nestled between parentheses beneath your cursor.
7. Speed Up Editing with Marks
In Vim, marks serve as a useful feature that enables you to save a location in your file to a specific key, much like bookmarks in a web browser. This allows for quick navigation between different portions of your text or code.
To create a mark, navigate to your desired position and press M, followed by the letter you want to associate with that mark. For instance, I’ll create a mark at point “A” .
To test if your new mark is functioning, press G twice to jump to the document’s start, then ` + A.
To swiftly return to your previous location, simply press the ` key twice.
To see a list of all marks in your current session, press Shift + ; (Semicolon), then type marks
in the command buffer.
8. Streamline Edits with Macros
In addition to convenient commands, you can also automate repetitive editing tasks using macros, a built-in Vim feature. This allows you to record a specific action and replay it throughout your file.
To create a macro, press Q followed by a designated key for your macro, for instance, I’ll use the “1” key.
Perform the desired edit on the current line; for example, edit the opening word to “That,”append “Hello, Make Tech Easier”at the line’s end, and then move to the next line.
After completing your changes, press Q to save your macro.
Test your new macro by pressing Shift + 2, followed by your assigned macro key.
9. Leverage Third-Party Plugins
Beyond its superb built-in capabilities, Vim is an extensible editor that can be customized with third-party plugins. This flexibility allows you to add additional functionality that may not be present in standard Vim, tailoring the editor to your preferences.
To get started with Vim plugins, consider installing Plug, a simple plugin manager that facilitates easy installation and management of your plugins directly within Vim. To install it, run the following command:
Edit your. vimrc file and append the following lines at the end:
After saving the. vimrc, restart Vim. Then, press Shift + ; to open the command buffer.
Type PlugStatus
into the buffer to verify that Plug was successfully installed on your Vim setup.
To add a plugin, navigate back to the call plug#begin()
section of your. vimrc, then type “Plug,” followed by the Git URL of your desired plugin. For example, using the following line will install the Goyo plugin:
Restart your Vim instance, access the command buffer, and execute PlugInstall
to initiate the installation process for your plugins.
With newfound knowledge on optimizing Vim, you can uncover the vast potential this powerful text editor holds. Discover how to transform Vim into an efficient word processor by adding simple modifications to your configuration file.
Image credit: Christina @ wocintechchat.com via Unsplash. All alterations and screenshots by Ramces Red.
Leave a Reply