If you are reading this article then you are in trouble. You wanted to edit a file using vim editor but got stuck in it. Stop panicking and wait with re-installing the OS and starting all from scratch – in this article I will show you how to exit a vim trap.
Thoát trình soạn thảo VIM bằng tổ hợp phím
Trình soạn thảo Vim rất khó đối với người mới bắt đầu, nhưng khi bạn học các tổ hợp phím phù hợp bằng VIM sẽ trở nên dễ dàng. Dưới đây là các tổ hợp phím phổ biến nhất để thoát khỏi vim.
Thoát các thay đổi lưu vim
Để thoát vim bằng cách lưu các thay đổi, hãy nhấn Esc để thoát chế độ chỉnh sửa rồi nhấn giữ ca và nhấn dấu hai chấm key (:). Then enter “wq” and press Enter. Here “w” stands for write and “q” stands for “quit”. On US keyboards the colon : là khóa giống như dấu chấm phẩy ;, located right of the letter “L” key.
:wq
Thoát vim mà không lưu thay đổi
To exit vim without saving changes hold down Shift and press the colon key (:). Then enter “q!” and press Enter.
:q!

Lưu các thay đổi mà không cần trình chỉnh sửa VIM hiện có
To save changes without exiting vim hold down Shift and press the colon key (:). Then enter “w” and press Enter.
:w
Thành thạo VIM
Bây giờ, khi tôi đã trình bày những kiến thức cơ bản về thoát Vim và lưu các thay đổi, hãy tiếp tục với một số phím tắt và tính năng hữu ích của Vim để nâng cao trình độ của bạn.
Đang khởi động VIM để chỉnh sửa tệp
Thông thường các nhà phát triển khởi chạy vim để chỉnh sửa tệp bằng lệnh:
vim filename
Nếu tập tin bị thiếu, nó sẽ được tạo. Bạn có thể chỉ định nhiều tệp để chỉnh sửa:
vim file1 file2 file3
Để mở tệp trên dòng cụ thể, bạn có thể thêm [num] sau lệnh vim. Ví dụ mở file ở dòng 127 nhập:
vim filename +127
Điều hướng trong VIM
Once you’ve entered Vim, navigating efficiently is key. Here are some popular shortcuts:
- Di chuyển con trỏ:
- Sử dụng các phím mũi tên để di chuyển theo các hướng tương ứng (lên, xuống, trái, phải).
- Alternatively, employ the
h,j,k, andlkeys for left, down, up, and right movements, respectively.
- Điều hướng từ:
- Move the cursor to the beginning of the next word with
w. - Navigate to the end of the current word using
e. - Jump to the beginning of the previous word with
b.
- Move the cursor to the beginning of the next word with
- Các dòng điều hướng:
- Go to the start of the line by pressing
0. - Move to the end of the line with
$.
- Go to the start of the line by pressing
Chỉnh sửa văn bản trong VIM
Once you’ve reached the desired location, these shortcuts will aid in text manipulation:
- Vào chế độ chèn:
- Press
ito enter insert mode before the cursor. - Use
Ito start inserting at the beginning of the line. - Employ
ato append after the cursor. Acho phép thêm vào cuối dòng.
- Press
- Xóa văn bản:
- Delete the character under the cursor with
x. - Erase the entire line with
dd. - Cut (delete) the current line and enter insert mode with
cc.
- Delete the character under the cursor with
- Sao chép và dán:
- Copy a line with
yy. - Paste the copied line below the current line using
p.
- Copy a line with
- Hoàn tác và làm lại:
- Undo the last action with
u. - Redo with
Ctrl + r.
- Undo the last action with
Tìm kiếm trong VIM
Tìm kiếm hiệu quả là rất quan trọng. Dưới đây là một số phím tắt liên quan đến tìm kiếm:
- Tìm kiếm văn bản:
- Initiate a search with
/, followed by the desired term. - Use
nto jump to the next occurrence andNto go to the previous one.
- Initiate a search with
- Thay thế văn bản:
- Substitute the current word with a new one using
:s/old/new/g.
- Substitute the current word with a new one using
Thoát Vim
Tôi đã trình bày những điều cơ bản ngay từ đầu, nhưng đây là một vài lựa chọn khác:
- Lưu và thoát tất cả:
- To save all changes and quit multiple files simultaneously, use
:waVà:qa.
- To save all changes and quit multiple files simultaneously, use
- Thoát mà không lưu:
- If you want to exit Vim without saving any changes made in any of the files,
:qa!will do the trick.
- If you want to exit Vim without saving any changes made in any of the files,
Phần kết luận
Hãy nhớ rằng, thành thạo Vim cần có thời gian và luyện tập. Bạn càng sử dụng nó, nó càng trở nên thoải mái và mạnh mẽ hơn. Chúc bạn chỉnh sửa vui vẻ!






