(This Blog post is part of a collaborative work between Me and Mustapha El Idrissi, Consult his devTo page for more information: https://dev.to/appsbymuss)
Introduction about Vim
Vim (short for Vi IMproved) is a highly configurable and efficient text editor that extends the functionality of the classic Unix-based Vi editor. Created by Bram Moolenaar, Vim is designed to maximize productivity in text editing through a combination of keyboard-driven commands and modes.
What are Buffers in vim ?
In Vim, a buffer is essentially an in-memory representation of a file. When you open a file in Vim, it is loaded into a buffer, allowing you to make changes to it.
Basics of Vim
To Edit a file (or Create one)
vim <file_name>
Saving and Quitting
- To save the modifications to a file: you simply press [:] then type w and press ENTER
- To save the mods. and quit the file: you do the same thing except it's :wq
- To quit a file and discard the mods.: press [:] and type [q!] then ENTER
There are 4 Modes in Vim
NORMAL MODE: It is the default mode. It is mostly useful for navigating through a file, however there are some shortcuts that make modifications to the file even with this mode. For example:
- Pressing [0]: Places the cursor at the start of the current line.
- Pressing [$]: Places the cursor at the end of the current line.
- Pressing [u]: Undo's the last change that was done to the "buffer".
- Pressing [x]: Deletes one character (where the cursor is).
- Pressing [dd]: Deletes the current line (but it gets saved in the "paste buffer", which means the contents can be "pasted".
- Pressing [p]: Pastes whatever is copied (in the "paste buffer")
- Pressing [gg]: Places the cursor at the first line in the file.
- Pressing [G]: Places the cursor at the last line in the file
INSERT MODE: This mode is activated by pressing [i]. It is used to insert new data to the document. Here are some examples of shortcuts:
- Pressing [a + Shift]: Places the cursor at the end of the current line and activates INSERT MODE.
- Pressing [i]: Activates INSERT MODE.
VISUAL MODE: This mode is useful to select specific subtext and apply modifications on it. It is activated by pressing [v]. Here are a few examples of shortcuts:
- Pressing [y]: The selected section of text will be copied (not cut) and get placed in the "paste buffer" (to be pasted somewhere)
- Pressing [:] then Typing [sort ui]: This will sort the selected lines alphabetically and ensure there are no duplicate lines.
COMMAND MODE: Accessed by pressing [:]. Used for entering commands such as saving files, quitting Vim, and searching.
examples:
- [:! <LINUX_COMMAND>] - Execute a command while in vim
File Operations
- [:q] - Close the current window/buffer.
- [:w] - Write changes to the current file.
- [:q!] - Close the current window/buffer and discard of the changes.
- [:wq] - Write changes to the current file and close the window.
- [:r <file_name>] - Copy the buffer of "file_name" and paste it in the current buffer.
- [:w <file_name>] - Copy and Write the current buffer to "file_name"
String Search
- [:%s/<regExpr>] - Search and highlight the first occurence of a string/pattern.
- [:%s/<regExpr>/String2/g] - Search and replace ALL the occurences of a string/pattern.
Buffers
Buffers can be extremely useful when you want to edit multiple files at the same time. Using COMMAND MODE, here are some useful tricks:
[:ls] - Lists all the buffers currently accessible. (the one active has a %a sign)
[:e <file_name>] - Load a file into a new buffer.
[:b <buffer_number>] - To switch to a buffer
[:enew] - To create an empty buffer (whose content that you can later on save to a file or to another buffer)
[:bn] - Switches to the next buffer in the list.
[:bp] - Switches to the previous buffer in the list.
[:bw <buffer_number>] - Saves changes to the buffer X.
[:bd <buffer_name>] - Deletes the buffer X.
[:bd! <buffer_name>] - Deletes the buffer X discarding of any changes.
Splitting the screen
Horizentally
[:sp[lit] <file_name>] - Splits the current file with "file_name" horizentally, This step can be done multiple times with multiple files.
Vertically
[:vsp[lit] <file_name>] - Splits the current file with "file_name" horizentally, This step can be done multiple times with multiple files.
[Ctrl + ww] - To switch between "splits".
Advantages of a Terminal-based Text editor
- Vim is very light resources-wise, which makes it ideal to run on low-end machines.
- It is terminal-based which means it can be executed and used remotely without any trouble using something like SSH.
- The learning curve to master Vim is a bit steep but everyone can master the basics quite easily after a bit of practice.
Top comments (0)