Mastering Basic Vim Commands
Mastering Vim: Enhancing Efficiency with Essential Commands and Shortcuts
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.
To install Vim on your system, follow the steps below:
Visit the Vim for Windows website and download the latest version of the installer.
Run the installer and follow the on-screen instructions to complete the installation process.
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:
To install Chocolatey and use it to install Vim, follow these steps:
Search for PowerShell and run it as an administrator on Windows.
Go to the Chocolatey installation page.
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:
- If the output is "Restricted", enter one of the commands below:
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):
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:
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
- Open any terminal of your choice and type
vimto start using Vim:
Opening files and directories in Vim is a straightforward process. To open a specific file, you can use the following command:
For example, if you want to open the file named "README.md," you would use the command:
On the other hand, if you wish to open a directory, use the following command:
For instance, let's say you have a directory called "myProject," and you want to open it in Vim. You would execute the command:
By following these steps, you will be able to install Vim on your Windows system and begin using it for your text editing needs.
Open the Terminal application.
Install Vim using Homebrew by executing the following command:
brew install vim
Open the Terminal application.
Install Vim using the package manager by executing the following command:
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:
For example, to open a file named "example.txt," you would use the command:
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
hmoves the cursor one character to the left.
jmoves the cursor one line down.
kmoves the cursor one line up.
lmoves the cursor one character to the right.
wmoves the cursor to the beginning of the next word.
bmoves 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.
ggmoves the cursor to the first line of the document.
Gmoves the cursor to the last line of the document.
<line-number>Gmoves the cursor to a specific line number.
Copying, Cutting, and Pasting
venters visual mode, allowing you to select text.
ycopies the selected text (yank).
dcuts the selected text (delete).
ppastes the copied or cut text after the cursor.
Ppastes the copied or cut text before the cursor.
Undoing Changes and Quitting
uundoes the last change.
Ctrl + rredo the last undone change.
:qquits Vim (closes the current file).
:q!quits Vim without saving changes.
:wsaves the changes to the current file.
:wqsaves 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.
njumps to the next occurrence of the search term.
Njumps to the previous occurrence of the search term.
:s/search-term/replacementreplaces the first occurrence of "search-term" with "replacement" in the current line.
:s/search-term/replacement/greplaces all occurrences of "search-term" with "replacement" in the current line.
:%s/search-term/replacement/greplaces 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.
:e <filename>opens a file in a new buffer.
:lslists all open buffers.
:b <buffer-number>switches to a specific buffer.
:bdcloses the current buffer.
:qquits Vim if no other buffers are open.
: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 + wswitches between open windows.
Ctrl + w + h/j/k/lmoves the cursor to the window in the corresponding direction.
Ctrl + w + ccloses the current window.
Macros allow you to record a sequence of commands and replay them.
q<register>starts recording a macro in the specified register (e.g.,
qastarts recording in register
Perform the desired commands.
qstops recording the macro.
@<register>replays the macro stored in the specified register (e.g.,
@areplays the macro stored in register
Marks allow you to quickly navigate to specific locations within a file.
m<letter>sets a mark at the current cursor position (e.g.,
masets a mark named
'followed by the mark letter jumps to the line where the mark is set (e.g.,
'ajumps to the line of mark
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
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.
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!