Neovim · Part 8 — Search, Navigation và Refactor
Thay Command Palette của VSCode bằng picker, live grep, quickfix, LSP references, symbols, rename và workflow refactor an toàn.
Nếu modal editing là tốc độ trong một file, thì search/navigation là tốc độ trong cả project.
VSCode có Cmd+P, sidebar search, symbol search. Neovim có:
- fuzzy finder;
- live grep bằng
ripgrep; - quickfix/location list;
- LSP references/symbols;
:grep,:vimgrep,:cdo,:argdo.
Chọn picker
Hai lựa chọn phổ biến:
- Telescope: lâu đời, nhiều extension, dễ học.
- Snacks picker: mới hơn, tích hợp trong bộ QoL của Folke.
Series này dùng Telescope vì dễ giải thích và tài liệu dồi dào. Khi đã vững, bạn có thể đổi picker mà không đổi mental model.
Setup Telescope
Tạo lua/plugins/search.lua:
return {
{
"nvim-telescope/telescope.nvim",
dependencies = {
"nvim-lua/plenary.nvim",
{
"nvim-telescope/telescope-fzf-native.nvim",
build = "make",
cond = function()
return vim.fn.executable("make") == 1
end,
},
},
cmd = "Telescope",
keys = {
{ "<leader>ff", "<cmd>Telescope find_files<CR>", desc = "Find files" },
{ "<leader>fg", "<cmd>Telescope live_grep<CR>", desc = "Live grep" },
{ "<leader>fb", "<cmd>Telescope buffers<CR>", desc = "Find buffers" },
{ "<leader>fh", "<cmd>Telescope help_tags<CR>", desc = "Help tags" },
{ "<leader>fs", "<cmd>Telescope lsp_document_symbols<CR>", desc = "Document symbols" },
{ "<leader>fr", "<cmd>Telescope lsp_references<CR>", desc = "References" },
},
config = function()
local telescope = require("telescope")
telescope.setup({
defaults = {
mappings = {
i = {
["<C-j>"] = "move_selection_next",
["<C-k>"] = "move_selection_previous",
},
},
},
})
pcall(telescope.load_extension, "fzf")
end,
},
}
Cài ripgrep và fd để picker nhanh:
brew install ripgrep fd
# hoặc apt/dnf/pacman tùy hệ điều hành
Mapping từ VSCode
| VSCode | Nvim |
|---|---|
Cmd+P / Ctrl+P | <leader>ff |
| Search toàn project | <leader>fg |
| Open symbol in file | <leader>fs hoặc gO |
| Find references | grr hoặc <leader>fr |
| Rename symbol | grn |
| Code action | gra |
| Problems panel | diagnostics location list |
Bạn không cần sidebar file tree nếu find_files và live_grep đã thành phản xạ.
Quickfix là superpower
Live grep đẹp, nhưng quickfix mới là công cụ refactor mạnh.
Grep bằng lệnh gốc:
:grep TODO src
:copen
Đi qua kết quả:
:cnext
:cprev
Thay đổi hàng loạt:
:cdo s/oldName/newName/g | update
Cẩn thận: :cdo chạy trên mọi entry trong quickfix. Trước khi dùng, hãy kiểm tra danh sách và đảm bảo Git sạch hoặc diff dễ review.
Search trong file
| Lệnh | Ý nghĩa |
|---|---|
/text | search forward |
?text | search backward |
n | kết quả kế |
N | kết quả trước |
* | search word dưới cursor |
# | search word dưới cursor ngược lại |
:%s/a/b/g | replace toàn file |
:%s/a/b/gc | replace toàn file có confirm |
Khi replace, thêm c để confirm:
:%s/useState/useSignal/gc
Refactor an toàn bằng LSP
| Việc | Lệnh |
|---|---|
| Rename symbol | grn |
| Code action | gra |
| References | grr |
| Implementation | gri |
| Type definition | grt trên Nvim mới |
| Document symbols | gO |
Refactor workflow:
- chạy test trước nếu có;
- dùng LSP rename thay vì search/replace khi đổi symbol;
- dùng live grep kiểm tra tên cũ;
- chạy formatter/linter;
- xem Git diff.
Search/replace tốt cho text. Rename symbol tốt cho code.
Project navigation không cần sidebar
Một workflow nhanh:
<leader>ffmở file.<leader>fgtìm text.grrxem references.gOxem symbol trong file.:copenxử lý danh sách kết quả.<C-o>quay lại vị trí cũ,<C-i>tiến tới vị trí sau.
Jump list rất đáng học:
| Key | Ý nghĩa |
|---|---|
<C-o> | jump back |
<C-i> | jump forward |
:jumps | xem jump list |
mA | đặt mark A |
'A | quay lại mark A |
Bài tập
- Cài Telescope.
- Dùng
<leader>ffmở 5 file khác nhau. - Dùng
<leader>fgtìm một function đang được dùng nhiều nơi. - Đẩy kết quả grep vào quickfix, mở
:copen. - Dùng LSP rename một biến local, rồi live grep tên cũ.
Gợi ý
Nếu bạn đang ở repo frontend, tìm useEffect, fetch, hoặc tên component. Đừng refactor symbol public lớn ngay. Luyện với biến local trước.
Điều cốt lõi
Search pro trong Neovim là sự kết hợp giữa picker để tìm nhanh, quickfix để xử lý danh sách, và LSP để refactor đúng ngữ nghĩa. Ba lớp này thay gần hết Command Palette/Search panel của VSCode.