DEV Community

Cover image for Mastering Vim: Enhancing Efficiency with Essential Commands and Shortcuts
Bello Osagie
Bello Osagie

Posted on • Edited on • Originally published at blog.techstackmedia.com

Mastering Vim: Enhancing Efficiency with Essential Commands and Shortcuts

GitHub Logo

Introduction

Vim is a powerful and efficient text editor that has gained popularity among programmers and developers due to its speed and versatility. With its extensive collection of commands and customizable features, Vim provides a seamless editing experience. In this blog post, we will explore some essential Vim commands and shortcuts that will enhance your productivity and help you streamline your workflow. From basic navigation to advanced functionalities, we will cover a range of commands to help you master Vim and become a more proficient software engineer.

Installation

To install Vim on your system, follow the steps below:

Windows

  1. Visit the Vim for Windows website and download the latest version of the installer.
  2. Run the installer and follow the on-screen instructions to complete the installation process.
  3. Once the installation is complete, you can open Vim by searching for "Vim" in the Start menu or by running it from the command prompt.

I prefer using Chocolatey (choco) to install packages, so check out the installation steps below:

Steps

To install Chocolatey and use it to install Vim, follow these steps:

  1. Search for PowerShell and run it as an administrator on Windows.
  2. Go to the Chocolatey installation page.
  3. If this is your first time installing Chocolatey on your operating system, execute the following command in your terminal, based on the instructions provided on the installation page:
Get-ExecutionPolicy
Enter fullscreen mode Exit fullscreen mode
  • If the output is "Restricted", enter one of the commands below:
Set-ExecutionPolicy AllSigned
Enter fullscreen mode Exit fullscreen mode

or

Set-ExecutionPolicy Bypass -Scope Process
Enter fullscreen mode Exit fullscreen mode
  • Regardless of whether "Restricted" was the previous output or not, run the following command (copy and paste it into your terminal):
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Enter fullscreen mode Exit fullscreen mode
  • If there are no errors, check if Chocolatey was successfully installed by running the following command:
choco
Enter fullscreen mode Exit fullscreen mode

If the output displays the version of Chocolatey you are using, it means the installation was successful.

  • Now, search for the package you need (in this case, Vim) by visiting the package search page. Once you find the Vim package, execute the following command:
choco install vim
Enter fullscreen mode Exit fullscreen mode
  • Open any terminal of your choice and type vim to start using Vim:
vim
Enter fullscreen mode Exit fullscreen mode

Opening files and directories in Vim is a straightforward process. To open a specific file, you can use the following command:

vim <filename>
Enter fullscreen mode Exit fullscreen mode

For example, if you want to open the file named "README.md," you would use the command:

vim README.md
Enter fullscreen mode Exit fullscreen mode

On the other hand, if you wish to open a directory, use the following command:

vim <directory>
Enter fullscreen mode Exit fullscreen mode

For instance, let's say you have a directory called "myProject," and you want to open it in Vim. You would execute the command:

vim myProject
Enter fullscreen mode Exit fullscreen mode

By following these steps, you will be able to install Vim on your Windows system and begin using it for your text editing needs.

macOS

  1. Open the Terminal application.
  2. Install Vim using Homebrew by executing the following command:
brew install vim
Enter fullscreen mode Exit fullscreen mode

Linux (Ubuntu)

  1. Open the Terminal application.
  2. Install Vim using the package manager by executing the following command:
sudo apt-get update
sudo apt-get install vim
Enter fullscreen mode Exit fullscreen mode

Now that you have Vim installed on your system, let's dive into the basic commands and shortcuts that will help you get started.

Getting Started with Vim

To open a file in Vim, use the following command:

vim <filename>
Enter fullscreen mode Exit fullscreen mode

For example, to open a file named "example.txt," you would use the command:

vim example.txt
Enter fullscreen mode Exit fullscreen mode

Once you have a file open in Vim, you'll be in normal mode, where you can navigate, edit, and execute commands.

Navigating and Editing

In the commands below make sure you press the ESC key before typing or pressing any of the commands.

Moving within the Document

  • h moves the cursor one character to the left.
  • j moves the cursor one line down.
  • k moves the cursor one line up.
  • l moves the cursor one character to the right.
  • w moves the cursor to the beginning of the next word.
  • b moves the cursor to the beginning of the previous word.
  • 0 (zero) moves the cursor to the beginning of the current line.
  • $ moves the cursor to the end of the current line.
  • gg moves the cursor to the first line of the document.
  • G moves the cursor to the last line of the document.
  • <line-number>G moves the cursor to a specific line number.

Copying, Cutting, and Pasting

  • v enters visual mode, allowing you to select text.
  • y copies the selected text (yank).
  • d cuts the selected text (delete).
  • p pastes the copied or cut text after the cursor.
  • P pastes the copied or cut text before the cursor.

Undoing Changes and Quitting

  • u undoes the last change.
  • Ctrl + r redo the last undone change.
  • :q quits Vim (closes the current file).
  • :q! quits Vim without saving changes.
  • :w saves the changes to the current file.
  • :wq saves the changes and quits Vim.

Searching and Replacing

  • / enters search mode.
  • Type the search term and press Enter to search forward
  • ? enters backward search mode.
  • Type the search term and press Enter to search backward.
  • n jumps to the next occurrence of the search term.
  • N jumps to the previous occurrence of the search term.
  • :s/search-term/replacement replaces the first occurrence of "search-term" with "replacement" in the current line.
  • :s/search-term/replacement/g replaces all occurrences of "search-term" with "replacement" in the current line.
  • :%s/search-term/replacement/g replaces all occurrences of "search-term" with "replacement" in the entire file.

Buffers and Windows

Vim allows you to work with multiple files simultaneously using buffers and windows.

Buffers

  • :e <filename> opens a file in a new buffer.
  • :ls lists all open buffers.
  • :b <buffer-number> switches to a specific buffer.
  • :bd closes the current buffer.
  • :q quits Vim if no other buffers are open.

Windows

  • :sp <filename> splits the current window horizontally and opens the file in the new window.
  • :vsp <filename> splits the current window vertically and opens the file in the new window.
  • Ctrl + w + w switches between open windows.
  • Ctrl + w + h/j/k/l moves the cursor to the window in the corresponding direction.
  • Ctrl + w + c closes the current window.

Advanced Features

Macros

Macros allow you to record a sequence of commands and replay them.

  • q<register> starts recording a macro in the specified register (e.g., qa starts recording in register a).
  • Perform the desired commands.
  • q stops recording the macro.
  • @<register> replays the macro stored in the specified register (e.g., @a replays the macro stored in register a).

Marks

Marks allow you to quickly navigate to specific locations within a file.

  • m<letter> sets a mark at the current cursor position (e.g., ma sets a mark named a).
  • ' followed by the mark letter jumps to the line where the mark is set (e.g., 'a jumps to the line of mark a).
  • followed by the mark letter jumps to the exact cursor position where the mark is set (e.g.,ajumps to the cursor position of mark a).

Plugins

Vim supports a vast ecosystem of plugins that extend its functionality. Here are a few popular plugins:

  • Vundle: A plugin manager for Vim.
  • NERDTree: A file explorer plugin for Vim.
  • CtrlP: A fuzzy file finder plugin for Vim.
  • YouCompleteMe: A code completion plugin for Vim.

These plugins can be installed and managed using plugin managers such as Vundle or Vim-Plug.

Conclusion

Vim is a powerful text editor with a steep learning curve, but mastering its commands and shortcuts can greatly enhance your productivity as a software engineer. We have covered some essential commands and functionalities in this blog post, including navigation, editing, searching, and advanced features like macros and marks. By exploring and practicing these commands, you'll become more proficient in Vim and be able to edit text efficiently and effectively.

Happy Vim editing!


References


Top comments (0)