Neovim · Part 9 — Git, Terminal, Test và Debug
Dựng workflow hằng ngày trong Neovim: gitsigns, terminal tích hợp, test runner, debug mindset, quickfix và khi nào nên dùng shell.
Một editor dùng thật không chỉ sửa text. Nó phải hỗ trợ vòng lặp:
edit -> format -> lint -> test -> inspect diff -> commit
Phần này dựng workflow hằng ngày trong Neovim.
Git signs trong buffer
Cài gitsigns.nvim:
return {
{
"lewis6991/gitsigns.nvim",
event = { "BufReadPre", "BufNewFile" },
opts = {
on_attach = function(bufnr)
local gs = package.loaded.gitsigns
local map = function(mode, lhs, rhs, desc)
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, desc = desc })
end
map("n", "]h", gs.next_hunk, "Next hunk")
map("n", "[h", gs.prev_hunk, "Previous hunk")
map("n", "<leader>hs", gs.stage_hunk, "Stage hunk")
map("n", "<leader>hr", gs.reset_hunk, "Reset hunk")
map("v", "<leader>hs", function()
gs.stage_hunk({ vim.fn.line("."), vim.fn.line("v") })
end, "Stage selected hunk")
map("v", "<leader>hr", function()
gs.reset_hunk({ vim.fn.line("."), vim.fn.line("v") })
end, "Reset selected hunk")
map("n", "<leader>hp", gs.preview_hunk, "Preview hunk")
map("n", "<leader>hb", gs.blame_line, "Blame line")
end,
},
},
}
Gitsigns giúp bạn thấy thay đổi ngay trong gutter, stage/reset từng hunk, preview diff mà không rời buffer.
Git vẫn nên sống trong shell
Đừng cố đưa toàn bộ Git vào UI plugin. Những lệnh này nên thuộc:
git status --short
git diff
git diff --staged
git add -p
git commit
git log --oneline --graph --decorate -20
Neovim + terminal mạnh vì bạn không phải chọn giữa editor và shell. Bạn dùng cả hai.
Terminal trong Neovim
Mở terminal:
:terminal
Thoát Terminal mode về Normal:
<C-\><C-n>
Keymap tiện hơn:
vim.keymap.set("t", "<Esc><Esc>", "<C-\\><C-n>", { desc = "Exit terminal mode" })
vim.keymap.set("n", "<leader>tt", "<cmd>terminal<CR>", { desc = "Open terminal" })
Terminal workflow:
- chạy dev server ở terminal/tmux riêng nếu process sống lâu;
- dùng terminal trong Nvim cho command ngắn;
- dùng quickfix cho output lỗi cần nhảy vào file.
Test runner: bắt đầu bằng lệnh gốc
Trước khi cài test plugin, hãy map command theo project:
vim.keymap.set("n", "<leader>tn", function()
vim.cmd("write")
vim.cmd("botright split | terminal npm test -- --runInBand")
end, { desc = "Run npm tests" })
Với Vitest:
vim.keymap.set("n", "<leader>tv", function()
vim.cmd("write")
vim.cmd("botright split | terminal npm run test -- --run")
end, { desc = "Run Vitest once" })
Sau khi workflow ổn, bạn có thể thêm neotest để chạy test gần cursor, hiển thị summary, attach output.
Debug: đừng bắt đầu bằng DAP
Debug có nhiều tầng:
- đọc error message;
- grep stack trace;
- log có chủ đích;
- chạy test nhỏ;
- dùng debugger.
nvim-dap rất mạnh, nhưng setup DAP phụ thuộc runtime. Với JavaScript/TypeScript, bạn thường cần adapter như js-debug-adapter. Với Python là debugpy. Với Go là delve.
Khuyến nghị học:
- trước: test + logs + quickfix;
- sau: DAP cho project thật sự cần breakpoint.
Quickfix cho lỗi build/test
Nếu compiler/test output có format file:line:col, bạn có thể đưa vào quickfix.
Ví dụ TypeScript:
:make
:copen
Set makeprg theo project:
vim.opt.makeprg = "npm run typecheck"
Sau đó:
:make
:cnext
Đây là workflow rất Vim: command ngoài tạo lỗi, quickfix đưa bạn đến đúng file.
Một vòng làm việc mẫu
1. <leader>ff mở file
2. sửa bằng modal editing
3. <leader>f format
4. :write
5. test bằng terminal hoặc keymap
6. ]h/[h xem hunk
7. <leader>hp preview
8. git diff
9. git add -p
10. git commit
Bạn không cần tất cả trong UI. Điều pro là vòng lặp ngắn, rõ, ít ma thuật.
Cheatsheet
| Việc | Lệnh/key |
|---|---|
| Terminal | :terminal |
| Thoát terminal mode | <C-\><C-n> |
| Git status | git status --short |
| Diff trong shell | git diff |
| Hunk kế/trước | ]h, [h |
| Stage hunk | <leader>hs |
| Reset hunk | <leader>hr |
| Preview hunk | <leader>hp |
| Quickfix open | :copen |
| Quickfix next | :cnext |
Bài tập
- Cài
gitsigns.nvim. - Sửa một file, dùng
]hvà[hđể đi qua hunk. - Preview hunk rồi stage hunk.
- Mở terminal trong Nvim và chạy test/lint.
- Chạy
git diff --stagedtrong terminal ngoài hoặc terminal split.
Gợi ý thói quen
Luôn xem diff trước commit. Editor tốt không thay code review với chính mình.
Điều cốt lõi
Workflow pro trong Neovim không phải nhét mọi thứ vào plugin. Nó là sự phối hợp giữa buffer, shell, quickfix, Git signs, và test command để vòng lặp edit-test-review ngắn nhất có thể.