Vim is a highly efficient text editor that’s popular in the world of programming and system administration. Vim stands for “Vi IMproved”, building on the classic “vi” editor with more advanced features. If you’re looking for a powerful editor that can speed up your work, learning Vim is worth it.
This post will help you get familiar with the basic commands in Vim, so you can get started editing and managing text files in no time.
Opening Files with Vim
To start editing a file using Vim, you can open it from your terminal. Just type:
vim <file-name>
Replace <file-name> with the actual name of the file you want to edit. If the file doesn’t exist yet, Vim will create it for you.
Modes in Vim
Vim operates with different modes that determine how the editor behaves. The most important modes for beginners are:
- Insert Mode: This is where you can type and edit text, similar to other text editors. You enter this mode by pressing
i. - Command Mode: This mode allows you to issue commands for saving, quitting, searching, and more. To get to Command Mode, press
Esc. This is also known as “Colon Mode” because many commands start with a colon (:). - Escape Mode: Also referred to as Normal Mode, this is the mode Vim starts in by default. You can’t type text here, but you can perform actions like copying, pasting, or navigating through the text. Press
Escto enter this mode from any other mode.
How to Switch Between Modes:
- Press
ito switch from command mode to insert mode. You’ll be able to type text directly into the file. - Press
Escto switch back to Escape (Normal) Mode. This allows you to enter commands to manage your file. - From Escape Mode, type
:to enter Command Mode and give specific instructions to Vim (like saving or quitting).
Common Commands in Command Mode:
You can use the following commands by typing a colon (:), followed by the command, and pressing Enter:
:q→ Quit Vim. If you haven’t made any changes, this will simply exit the editor.:wq→ Save the file and then quit.:q!→ Quit without saving any changes. Useful when you’ve made edits you don’t want to keep.
Other Useful Commands:
:set nu→ Displays line numbers in the file.:set nonu→ Turns off line numbers.:/<word-to-find>→ Searches for a word from the top of the file down.:?<word-to-find>→ Searches for a word from the bottom of the file up.
Escape Mode Commands:
When in Escape Mode (Normal Mode), you can use a number of key combinations to navigate and manipulate the text more efficiently:
u→ Undo the last change.yy→ Copy (yank) a line.dd→ Delete a line.p→ Paste the last copied or deleted line.
Navigation Shortcuts:
Shift + G→ Jump to the bottom of the file.gg→ Jump to the top of the file.
Inserting Text
To start typing and editing text, switch to Insert Mode by pressing i. Now you can enter text just like in a standard text editor. When you’re done typing, press Esc to return to Escape Mode and continue using the powerful Vim commands.
No Mouse or Arrow Keys in Vim
One thing that surprises many beginners is that Vim doesn’t rely on the mouse or arrow keys like most modern text editors. Instead, you navigate using the keyboard. While this may feel unusual at first, it helps speed up editing once you get used to it.
To move around the text:
- Use
hto move left. - Use
jto move down. - Use
kto move up. - Use
lto move right.
This is part of Vim’s efficiency- by keeping your hands on the keyboard, you can edit files much faster than switching between the mouse and keyboard!
Note:
–> Arrow keys work in Vim to move the cursor around. However, many Vim users prefer to navigate with the keyboard shortcuts (h, j, k, l) for increased efficiency.
Remember: Vim can seem challenging at first, but with practice, you’ll find it incredibly powerful and efficient. Keep experimenting with the commands and shortcuts, and soon enough, you’ll be navigating and editing like a pro!
