Basic commands for working with the Vim editor
The Vim (Vi IMproved) editor is one of the most powerful and popular text editors in the Unix and Linux world. It provides powerful text editing capabilities and can be challenging for beginners due to its unintuitive command structure. In this article, we will cover the basic commands and principles of working with Vim, which will help you start using this editor effectively.
Vim Modes
Vim operates in several modes, and understanding these modes is key to using the editor successfully:
- Normal mode: The main mode for navigating and editing text. In this mode, you can enter commands to perform various actions.
- Command-line mode: Used to execute commands, such as saving or exiting. Accessed via :.
- Insert mode: The mode for directly entering text. You can switch to this mode with the i key.
- Visual mode: Used to select text. Enabled by the v key
Search and replace
To search for text, use the / command in command line mode:
/text — search forward for text
?text — search backward for text
n — go to the next occurrence of the found text
N — go to the previous occurrence of the found text
To replace text, use the :s command in command line mode:
:s/old/new/ — replace the first occurrence in a line
:s/old/new/g — replace all occurrences in a line
:%s/old/new/g — replace all occurrences in the entire file
Saving and exiting
To save and exit Vim, use the :w command:
:w — save the file
:w filename — save the file with a new name
:q — quit Vim
:q! — exit without saving changes
:wq — save file and exit
Additional commands
gg — go to the beginning of the file
G — go to the end of the file
:set number — show line numbers
:set nonumber — hide line numbers
:help — open Vim help