Mastering Vim: Enhancing Efficiency with Essential Commands and Shortcuts
Tags: linux, tutorial, beginners, softwareengineering
Published: 2023-06-05 14:46:04

[![GitHub Logo](https://res.cloudinary.com/bizstak/image/upload/v1685060686/github_f9ljwi.svg)](https://github.com/techstackmedia/software-engineering-series/tree/09-mastering-the-basic-of-vim-commands)

## 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](https://www.vim.org/download.php) 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](https://chocolatey.org/install).
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:

```powershell
Get-ExecutionPolicy
```

- If the output is "Restricted", enter one of the commands below:

```powershell
Set-ExecutionPolicy AllSigned
```

or

```powershell
Set-ExecutionPolicy Bypass -Scope Process
```

- Regardless of whether "Restricted" was the previous output or not, run the following command (copy and paste it into your terminal):

```powershell
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'))
```

- If there are no errors, check if Chocolatey was successfully installed by running the following command:

```powershell
choco
```

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](https://community.chocolatey.org/packages). Once you find the Vim package, execute the following command:

```powershell
choco install vim
```

- Open any terminal of your choice and type `vim` to start using Vim:

```powershell
vim
```

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

```sh
vim <filename>
```

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

```sh
vim README.md
```

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

```sh
vim <directory>
```

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

```sh
vim myProject
```

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:

```sh
brew install vim
```

### Linux (Ubuntu)

1. Open the Terminal application.
2. Install Vim using the package manager by executing the following command:

```sh
sudo apt-get update
sudo apt-get install vim
 ```

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:

```sh
vim <filename>
```

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

```sh
vim example.txt
```

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.,`a`jumps 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](https://github.com/VundleVim/Vundle.vim): A plugin manager for Vim.
- [NERDTree](https://github.com/preservim/nerdtree): A file explorer plugin for Vim.
- [CtrlP](https://github.com/ctrlpvim/ctrlp.vim): A fuzzy file finder plugin for Vim.
- [YouCompleteMe](https://github.com/ycm-core/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

- [Vim Official Website](https://www.vim.org/)
- [Vim Documentation](https://vimhelp.org/)
- [Vim Cheat Sheet](https://vim.rtorr.com/)
- [Vim GitHub Repository](https://github.com/vim/vim)

---