posts-go/posts/2023-05-03-neovim.md

169 lines
4.3 KiB
Markdown
Raw Permalink Normal View History

2023-05-02 17:42:11 +00:00
# My neovim workflow
2024-05-12 16:22:38 +00:00
![nvim-000](https://raw.githubusercontent.com/haunt98/posts-images/main/nvim-000.jxl)
2023-06-10 04:57:15 +00:00
2023-06-10 03:50:39 +00:00
All configs are in [my dotfiles](https://github.com/haunt98/dotfiles).
2023-07-19 08:31:38 +00:00
## Trick or treat
2023-08-05 18:56:25 +00:00
Search current word: `*`
2023-05-02 17:42:11 +00:00
Search multiple words:
2023-06-10 03:50:39 +00:00
```vim
2023-05-02 17:42:11 +00:00
:/\vword1|word2|word3
```
2023-06-10 03:50:39 +00:00
Replace word:
```vim
:%s/word1/word2/g
```
2023-07-07 09:58:37 +00:00
Delete all lines contain word:
```vim
:g/word/d
```
2023-08-09 09:53:49 +00:00
Delete all lines **not** contain word:
```vim
:g!/word/d
:v/word/d
```
Play macro `a` (after selecting lines):
2023-09-10 14:32:49 +00:00
```vim
:norm! @a
```
2023-07-19 08:31:38 +00:00
Sort lines (after selecting lines):
```vim
:sort -u
```
Reverse lines (after selcting lines):
```vim
:!tail -r
```
2023-08-10 06:40:58 +00:00
Column-ize lines:
```vim
:!column -t
```
2023-07-19 08:31:38 +00:00
## Jumpo
Basic:
2023-06-10 03:50:39 +00:00
- `gg`: first line
- `G`: last line
- `0`: first character of line
- `$`: last character of line
- `w`, `b`: word forward/backward
2023-06-15 19:17:40 +00:00
- `e`, `ge`: end of word current/before
- `W`, `B`: WORD (word with special char) forward/backward
- `E`, `gE`: end of WORD current/before
2023-06-24 17:18:07 +00:00
- `f{char}`, `F{char}`: find forward/backward character
2024-03-20 18:45:57 +00:00
- `%`: jump between matching pair of `()`, `[]`, `{}`
2023-06-10 03:50:39 +00:00
2023-07-19 08:31:38 +00:00
Advance:
2023-06-10 03:50:39 +00:00
- `CTRL-O`, `CTRL-I`: cursor position backward/forward
- `{`, `}`: paragraph backward/forward
- `H`: top of screen
- `M`: middle of screen
- `L`: bottom of screen
- `CTRL-]`, `CTRL-T`: jump to tag/jump back from tag
2023-08-05 18:56:25 +00:00
- Support jump to Go definition with
[fatih/vim-go](https://github.com/fatih/vim-go).
2023-06-10 03:50:39 +00:00
2023-07-19 08:31:38 +00:00
## Keymap
2023-07-04 06:24:03 +00:00
Use both `\` and `;` as leader key:
2023-06-10 03:50:39 +00:00
```lua
2023-07-04 06:24:03 +00:00
vim.keymap.set("n", ";", "<Leader>", { remap = true })
vim.keymap.set("n", "'", "<Leader>", { remap = true })
2023-07-07 06:49:38 +00:00
```
2023-09-23 05:34:27 +00:00
Ground rules:
2023-06-10 03:50:39 +00:00
2023-07-04 06:24:03 +00:00
- `<Leader>` prefix: prefer global keymap
- `<Space>` prefix: prefer lsp keymap, for coding of course :D
2024-05-16 17:50:28 +00:00
- Native neovim:
2024-05-21 16:48:25 +00:00
- `gcc`: comment/uncomment current line
- `gc`: comment/uncomment selected lines
- `gx`: open file/url under cursor
- `Q`, `@`: execute last macro
2024-05-16 17:50:28 +00:00
- `]d`, `[d`: next/previous diagnostic
- `K`: hover
2024-05-09 18:30:55 +00:00
- Black hole register:
- `<Leader>d`: `d` without yank
- `<Leader>c`: `c` without yank
- `<Leader>x`: `x` without yank
2023-07-04 06:24:03 +00:00
- With [ibhagwan/fzf-lua](https://github.com/ibhagwan/fzf-lua):
- `<Leader>f`: find files
2023-09-23 05:34:27 +00:00
- `<Leader>l`: find lines
2023-07-04 06:24:03 +00:00
- `<Leader>rg`: grep files
2024-05-09 18:30:55 +00:00
- `<Leader>g`: git status
2023-09-23 05:34:27 +00:00
- With [neovim/nvim-lspconfig](https://github.com/neovim/nvim-lspconfig)
helps:
2024-05-09 18:30:55 +00:00
- `<Space>s`: list lsp symbols
- `<Space>r`, `gr`: list references
- `<Space>i`, `gi`: list implementation
- `<Space>ca`: list code action
2024-05-21 16:48:25 +00:00
- With [hrsh7th/nvim-cmp](https://github.com/hrsh7th/nvim-cmp):
2024-05-09 18:30:55 +00:00
- `<C-Space>`: trigger completion
2023-08-05 18:56:25 +00:00
- With [nvim-tree/nvim-tree.lua](https://github.com/nvim-tree/nvim-tree.lua),
inside nvim-tree:
2023-07-04 06:24:03 +00:00
- `<C-n>`: toggle
- `<Leader>n`: locate file
2023-06-24 14:57:22 +00:00
- `a`: create
2023-06-23 07:40:04 +00:00
- `d`: delete
- `r`: rename
- `x`: cut
- `c`: copy
- `p`: paste
2023-06-24 15:24:39 +00:00
- `U`: toggle hidden
2023-06-23 07:40:04 +00:00
- With [lewis6991/gitsigns.nvim](https://github.com/lewis6991/gitsigns.nvim):
2023-06-15 19:06:11 +00:00
- `]c`, `[c`: next/previous git change
2023-07-08 11:31:12 +00:00
- With [tpope/vim-projectionist](https://github.com/tpope/vim-projectionist)
- `:A`: open alternate file
2023-06-23 07:40:04 +00:00
- With [echasnovski/mini.nvim](https://github.com/echasnovski/mini.nvim)
- With mini-surround
- `sa`: add surround
- `sd`: delete surround
- `sr`: replace surround
2024-05-09 18:30:55 +00:00
- With [stevearc/conform.nvim](https://github.com/stevearc/conform.nvim):
- `<Space>f`: format code
2023-09-23 05:34:27 +00:00
- With [neovim/nvim-lspconfig](https://github.com/neovim/nvim-lspconfig):
2024-05-09 18:30:55 +00:00
- `<Space>e`: current diagnostic
- `<Space>lr`: restart lsp server
- `gd`: go to definition
- `<Space>k`, `gk`: hover
2023-06-29 10:29:18 +00:00
- `<F2>`: rename
2024-05-09 18:30:55 +00:00
- With [github/copilot.vim](https://github.com/github/copilot.vim):
- `<M-Right>`: completion, replace `Tab`
2023-06-10 03:50:39 +00:00
2023-07-07 09:58:37 +00:00
## References / Thanks
2023-06-10 03:50:39 +00:00
2023-08-05 18:56:25 +00:00
- vim docs:
- [Seven habits of effective text editing 2.0](https://moolenaar.net/habits_2007.pdf)
2023-07-19 08:31:38 +00:00
- neovim official docs:
- [neovim Motion](https://neovim.io/doc/user/motion.html)
- [neovim Tagsrch](http://neovim.io/doc/user/tagsrch.html)
- [neovim Lua-guide](https://neovim.io/doc/user/lua-guide.html)
- Hidden gem:
- [Vim Tips (Revisited)](https://bluz71.github.io/2021/09/10/vim-tips-revisited.html)
2024-04-15 06:52:03 +00:00
- [How do I reverse selected lines order in Vim?](https://superuser.com/a/189956)
- [Use Vim macros to automate frequent tasks](https://www.redhat.com/sysadmin/use-vim-macros)
2024-03-20 18:45:57 +00:00
- [3 Vim commands for blazingly fast navigation between brackets ⚡](https://dev.to/m4xshen/3-vim-commands-for-blazingly-fast-navigation-between-brackets-55kc)