DEV Community

EME GUG
EME GUG

Posted on

Vim/Neovim cho người mới: 10 phút để productive

Vim không khó như mọi người nghĩ. Bạn chỉ cần biết 20% commands để productive 80% thời gian.

Tại sao Vim?

  • Có sẵn trên mọi server (SSH vào là có)
  • Nhanh hơn bất kỳ editor nào khi thành thạo
  • Không cần rời keyboard

4 Modes

  1. Normal (mặc định): di chuyển và thao tác text
  2. Insert (i): gõ text bình thường
  3. Visual (v): chọn text
  4. Command (:): chạy commands

Esc luôn đưa bạn về Normal mode.

Di chuyển (Normal mode)

h j k l     → trái xuống lên phải
w / b       → nhảy đầu từ tiếp / trước
0 / $       → đầu dòng / cuối dòng
gg / G      → đầu file / cuối file
Ctrl+d/u    → scroll nửa trang xuống/lên
/keyword    → tìm kiếm (n = kết quả tiếp)
Enter fullscreen mode Exit fullscreen mode

Chỉnh sửa

i           → insert trước cursor
a           → insert sau cursor
o / O       → dòng mới dưới / trên
dd          → xóa cả dòng
yy          → copy cả dòng
p           → paste
u           → undo
Ctrl+r      → redo
Enter fullscreen mode Exit fullscreen mode

Combo hay dùng nhất

ciw         → đổi cả từ (change inner word)
ci"         → đổi text trong "..."
di(         → xóa text trong (...)
yiw         → copy cả từ
dw          → xóa từ cursor đến cuối từ
D           → xóa từ cursor đến cuối dòng
.           → lặp lại thao tác vừa làm
Enter fullscreen mode Exit fullscreen mode

Commands hay dùng

:w          → lưu
:q          → thoát
:wq         → lưu và thoát
:q!         → thoát không lưu
:%s/old/new/g → find & replace toàn file
:set number → hiện số dòng
Enter fullscreen mode Exit fullscreen mode

Config tối thiểu (~/.vimrc)

set number          " Hiện số dòng
set relativenumber  " Số dòng tương đối
set tabstop=4       " Tab = 4 spaces
set shiftwidth=4
set expandtab       " Tab thành spaces
set autoindent
set ignorecase      " Search không phân biệt hoa thường
set smartcase       " Trừ khi gõ chữ hoa
set hlsearch        " Highlight search results
set incsearch       " Search real-time
set clipboard=unnamedplus " Copy/paste với system clipboard
syntax on
Enter fullscreen mode Exit fullscreen mode

Neovim + minimal plugins

# Cài Neovim
sudo apt install neovim
# hoặc: brew install neovim
Enter fullscreen mode Exit fullscreen mode

File ~/.config/nvim/init.vim:

" Bao gồm config vim cơ bản
source ~/.vimrc

" Plugin manager (vim-plug)
call plug#begin()
Plug 'tpope/vim-commentary'   " gcc để comment
Plug 'tpope/vim-surround'     " cs'" đổi ' thành "
Plug 'junegunn/fzf.vim'       " Ctrl+p tìm file
call plug#end()
Enter fullscreen mode Exit fullscreen mode

5 phút mỗi ngày

  1. Tuần 1: hjkl, i, Esc, :wq — đủ để sống sót
  2. Tuần 2: w, b, dd, yy, p — bắt đầu nhanh hơn
  3. Tuần 3: ciw, ci", /search — bắt đầu thấy mạnh
  4. Tuần 4: . repeat, visual mode — productivity tăng rõ

Đừng cố học hết. Mỗi ngày thêm 1 command mới.


Bạn dùng Vim hay VS Code? Hay cả hai?

Top comments (0)