1. Introduction
In the work of a developer like us, there are times when we cannot use fancy IDEs with powerful support to manipulate files in the system, or with source code on the server. Setting an editor like Sublime Text, Gedit, … to edit files is not possible, but it is forced to use other text editors like Nano, Vim, … In this article we will be together. learn how to use Vim to "tame" it, hope that through this article you will get acquainted with Vim will feel easier, Vim less complicated, dry and love Vim more ?
2. Some advantages of Vim
- With a "huge" file, opening with Vim will be an extremely reasonable choice, will be softer than many other editors.
- Vim is supported on many operating systems (Vim was first available only on Linux and Unix platforms, later also on Windows).
- In some cases, using Vim to edit files is even faster than using other text editors (of course, you need to be fluent in ?).
- All operations can be done via keyboard shortcuts, so we may not need a mouse when using Vim. You look cool, it will be more cool when using Vim with keyboard shortcuts, command lines as fast as power, type tap and then enter the way and … Done !!! ?
3. The corresponding modes and shortcuts in Vim
3.1. Normal mode
This is the first mode when you open a file with Vim, where we use commands and shortcuts to perform the next task in Vim. The shortcut to enter normal mode is: Esc
(used when you are in another mode, and normally when you just enter Vim is normal).
3.2. Insert mode
This is the mode used to change the content, insert characters into the file.
Shortcut to insert mode:
i - Chèn vào ngay trước vị trí hiện tại của con trỏ
a - Chèn vào ngay sau vị trí hiện tại của con trỏ
I - Chèn vào ngay đầu dòng hiện tại
A - Chèn vào ngay cuối dòng hiện tại
3.3. Visual mode
This mode allows you to select text areas to work with (highlighted) (eg select a text area to copy, cut, delete).
Shortcuts to visual mode:
v - Lựa chọn vùng(bôi đen) ngay tại vị trí con trỏ hiện tại, lựa chọn vùng bôi đen bằng cách sử dụng các phím điều hướng mũi tên lên, xuống, phải, trái
V - Lựa chọn vùng theo nguyên dòng, điểu chỉnh vùng chọn bằng phím lên, xuống
3.4. Command mode
This mode is used to manipulate commands in Vim, such as saving content made in other modes, exiting Vim, etc.
Shortcut to enter mode command :
4. Set of commands and shortcuts in Vim
Open the file with Vim:
$ vim file_name
or $ vi file_name
Move cursor keys:
In addition to the arrow keys, you can use the following keys to navigate the cursor:
h - sang trái
l - sang phải
k - lên
j - xuống
gg - lên đầu file với ký tự đầu dòng
G - xuống dưới cùng file với ký tự đầu dòng
10G hoặc :10 - nhảy đến dòng thứ 10 của file
Ctrl + G - xem thông tin của dòng hiện tại
(eg in the file's first line, what column number, …)
w - từ kế tiếp
e - kết thúc của từ hiện tại
0 - đến đầu dòng
$ - đến cuối dòng
% - di chuyển đến các ngoặc trong dòng
[[ - nhảy đến nơi bắt đầu function
Copy, Cut and Paste commands
y - copy phần text đã chọn vào clipboard
yy - copy dòng hiện tại
y$ - copy đến cuối dòng
D - copy đến cuối file
p - paste
dd - cut dòng hiện tại
The commands to save and exit Vim after finishing editing:
:q - Thoát khỏi Vim và không lưu
:q! - Bắt buộc thoát khỏi Vim và không lưu
:w - Lưu file
:w! - Bắt buộc ghi đè file
wq - Lưu file và thoát
Search
/từ cần tìm - tìm kiếm từ "từ cần tìm" từ trên xuống dưới
/từ cần tìm - tìm kiếm từ "từ cần tìm" từ dưới lên trên
/cWord - tìm cả chữ hoa lẫn thường từ "Word"
/< val - tìm từ bắt đầu bằng "va". Vd value, valid
/end> - tìm từ kết thúc bằng end
* hoặc n - di chuyển con trỏ đến vị trí có từ đang cần tìm kiếm tiếp theo
10yy - copy 10 dòng đầu của file
Replace
:%s/oldword/newword/g - replace toàn bộ oldword thành newword trong file
:%s/oldword/newword/gc - replace toàn bộ oldword thành newword trong file kèm theo confirm
5. Work with multiple files at the same time
5.1. Open multiple files to edit:
$ vi file1.txt file2.txt file3.php file4.html
$ vi *.txt
- Some useful commands when working with multiple files at once::
:ls - xem danh sách các file đang được mở
:bnext hoặc pn- chuyển sang file tiếp theo
:bprev hoặc bp - chuyển sang file trước đó
:bfirst - chuyển tới file đầu tiên
:blast - chuyển tới file cuối cùng
5.2. Works with multiple windows
- We can edit files more easily by using the parallel window function in Vim
:sp filename.txt - Chia 2 file thành 2 cửa sổ theo chiều ngang
:vs filename.txt - Chia 2 file thành 2 cửa sổ theo chiều dọc
In case filename.txt does not exist, it will create a new file named filename.txt
Use 2 times Ctrl + w
to switch between files.
6. Finish
Above are some common commands and shortcuts used in Vim, in my opinion, that much if we remember and apply well, we can use Vim quite proficiently ??. Of course my article certainly has many shortcomings about the functions, shortcuts and commands in Vim, so it is good for you to add them to yourselves ?Thank!