logo logo

The next-generation blog, news, and magazine theme for you to start sharing your stories today!

The Blogzine

Save on Premium Membership

Get the insights report trusted by experts around the globe. Become a Member Today!

View pricing plans

New York, USA (HQ)

750 Sing Sing Rd, Horseheads, NY, 14845

Call: 469-537-2410 (Toll-free)

hello@blogzine.com

9 Essential Vim Tips and Tricks for Advanced Users

avatar
Louis Ferguson

An editor at Blogzine


  • 🕑 5 minutes read
  • 2 Views
9 Essential Vim Tips and Tricks for Advanced Users
A photograph of a person working in front of a computer.

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.

A terminal showing the contents of the last file in the multi-file Vim buffer.

To start editing another file, execute the :e command followed by the file name.

A terminal highlighting the open command for a new file in the command buffer.

The :ls command is useful for displaying all buffers that are currently open in your Vim session.

A terminal showing the list of currently active buffers inside Vim.

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”:

A terminal showing the automatic text expansion feature in Vim.

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.

A terminal highlighting the user interrupt for Vim's automatic abbreviation command.

To eliminate an abbreviation, use the una command. For instance, :una asap will remove the abbreviation for “as soon as possible.”

A terminal highlighting the command to remove an abbreviation in Vim.

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:

A terminal showing two files in split display inside Vim.

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.

A terminal highlighting the copied line of code from the

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 terminal showing the error that Vim returns when it doesn't have the permission to modify a file.

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.

A terminal showing the internal shell session inside 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.

A terminal showing a misaligned file with unnecessary indentations.

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.

A terminal showing a properly aligned code block using pastetoggle.

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.

A terminal highlighting the modified text using Text Object commands.

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.

A terminal highlighting the Text Object command applied to the parenthesis delimiter.

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” .

A terminal showing the location of a custom Vim mark.

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.

A terminal highlighting the custom mark inside Vim's registers.

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.

A terminal highlighting the prompt that Vim creates whenever it's recording a macro.

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.

A terminal showing the Vim macro working properly.

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.

A terminal highlighting the PlugStatus command for Vim Plug.

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:

A terminal highlighting the Plug entry for installing a plugin in Vim.

Restart your Vim instance, access the command buffer, and execute PlugInstall to initiate the installation process for your plugins.

A terminal showing the installation process for the Goyo Vim plugin.

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.

Source



Leave a Reply

Your email address will not be published. Required fields are marked *