├── README.md ├── filter_awk.gif ├── git.md ├── global.gif ├── global_replace.gif ├── sort.gif └── vehicles.list /README.md: -------------------------------------------------------------------------------- 1 | # Builtins 2 | 3 | ## Increment numbers (incremental sequence) 4 | [`ctrl-a`](https://vimhelp.org/change.txt.html#CTRL-A) increments the number at or after the cursor, [`ctrl-x`](https://vimhelp.org/change.txt.html#CTRL-X) decrements the number. With visual selection an incremental sequence can be achieved with [`g ctrl-a`](https://vimhelp.org/change.txt.html#v_g_CTRL-A) 5 | 6 | before -> after: 7 | 8 | -> 9 | 10 | g_ctrl_a 11 | 12 | (key sequence in video: `jVG^A..uuugvg^A..uuugv10g^A`) 13 | 14 | Tip: This also works with rectangle selection (`ctrl-v`). 15 | 16 | ## Replay macro on each line in visual selection 17 | [`:norm @q`](https://vimhelp.org/various.txt.html#%3Anorm) in visual mode will perform the normal command `@q` (play macro in register q) on each line in selection. [`qq`](https://vimhelp.org/repeat.txt.html#q) is used to record the macro. 18 | 19 | ![norm_q](https://user-images.githubusercontent.com/4508793/143023739-894e32bf-c1f7-4a50-8c03-19b0771fb87b.gif) 20 | 21 | (key sequence in video: `qqIconst ^[A;^[qjVjjj:norm @q`, q register content: `Iconst ^[A;^[`) 22 | 23 | ## Navigate quickfix list 24 | The [quickfix list](https://vimhelp.org/quickfix.txt.html#quickfix) can be populated with locations in several ways. A key to using it effectively is to have mappings for [`:cnext`](https://vimhelp.org/quickfix.txt.html#%3Acnext) and [`:cprev`](https://vimhelp.org/quickfix.txt.html#%3Acprev). 25 | 26 | - [`:vimgrep en *`](https://vimhelp.org/quickfix.txt.html#%3Avimgrep) is used to find all occurences of `en` in all files in cwd (current working directory). 27 | - [`:copen`](https://vimhelp.org/quickfix.txt.html#%3Acopen) is used to open the quickfix window to show the quickfix list. 28 | 29 | ![qflist](https://user-images.githubusercontent.com/4508793/143112529-717fb6ea-d7ab-4f87-a5da-4c0df5f2a9c4.gif) 30 | 31 | Mapping suggestions: 32 | ```vim 33 | nnoremap cnext 34 | nnoremap cprev 35 | ``` 36 | 37 | - `:vimgrep /def test/ **/*.py` find all matches of "def test" in all pythons files recursively from cwd. 38 | - [`:cdo`](https://vimhelp.org/quickfix.txt.html#%3Acdo) executes a command at each entry in the quickfix list. 39 | 40 | ## Use `!` to run external command (e.g. sort lines) 41 | [Filters](https://vimhelp.org/change.txt.html#filter) can be run for example by typing 42 | [`!`](https://vimhelp.org/change.txt.html#%21) with a visual selection. All text will be filtered 43 | through an external command. To sort all lines by numeric sort, use `!sort -n`. 44 | 45 | ![filter in selection](sort.gif) 46 | 47 | (key sequence in video: `vip!sort -n`) 48 | 49 | ## Use filtering and awk to sum all numbers in a specific column 50 | awk is a powerful tool, here used to sum all the fields of a specific column: 51 | - `:%! awk '{print; s+=$2} END {print s}'` sums the second field (`$2`) of 52 | each line and prints the total at the `END`. 53 | 54 | ![filter with awk](filter_awk.gif) 55 | 56 | ## global: repeat a command for each line matching a pattern 57 | With [`:global`](https://vimhelp.org/repeat.txt.html#%3Aglobal) a command can be repeated for each 58 | line that matches a pattern. Default pattern is last used search pattern. 59 | 60 | - `:g//d` deletes all lines matching the last search pattern 61 | - `:g//d E` delete the same lines and appends each delete to register `e` which can be pasted with 62 | `"ep` 63 | 64 | ![global delete, append to register](global.gif) 65 | 66 | key sequence in video: `*:g//d` `u` `:g//d Ep` 67 | 68 | - `:g/pattern/norm @q`, play macro on each line matching pattern. 69 | - `:v/;$/ s/$/;`, Add `;` to all lines that does not end in semicolon. 70 | 71 | More g power at [https://vim.fandom.com/wiki/Power_of_g](https://vim.fandom.com/wiki/Power_of_g) 72 | 73 | ## Replace only within selection 74 | The search [pattern atom](https://vimhelp.org/pattern.txt.html#pattern-atoms) [`\%V`](https://vimhelp.org/pattern.txt.html#%2F%5C%25V) can be used to match inside visual area. This can be used to replace only within a (rectangle) selection. 75 | 76 | ![sub_in_selection](https://user-images.githubusercontent.com/4508793/143133723-69acd9ba-1516-4d21-a546-b04d9a84e622.gif) 77 | 78 | (key sequence in video: `wwww^VG$se/o`) 79 | 80 | Mapping suggestion: 81 | ```vim 82 | xnoremap s :s/\%V 83 | ``` 84 | 85 | ## Delete to search motion 86 | Normal commands like [`d`](https://vimhelp.org/change.txt.html#d) can take any [motions](https://vimhelp.org/intro.txt.html#%7Bmotion%7D) for example a search `/`. When searching and current match is displayed, use [`ctrl-g`](https://vimhelp.org/cmdline.txt.html#%2F_CTRL-G) to move to the next match. 87 | 88 | - `d/en` Will delete everything up to the 3rd match of search pattern "en". 89 | - Use a [search-offset](https://vimhelp.org/pattern.txt.html#search-offset) like `/e` in `d/en/e` to delete to the end of the match. 90 | 91 | ![d to search](https://user-images.githubusercontent.com/4508793/143139836-a1ac23f4-9367-447b-867c-06b6a7c7fdc3.gif) 92 | 93 | (key sequence in video: `d/en^G^G`) 94 | 95 | ## Select what was just pasted, reselect last selection 96 | 97 | [`` `[ ``](https://vimhelp.org/motion.txt.html#%60%5B) moves to the first character of the previously changed or yanked text, [`` ]` ``](https://vimhelp.org/motion.txt.html#%60%5D) moves to the last. This means that `` `[v`] `` will visually select what was just pasted. [`gv`](https://vimhelp.org/visual.txt.html#gv) will reselect the previous selected area. 98 | 99 | ![gv](https://user-images.githubusercontent.com/4508793/143313428-e6bba2f8-425b-468e-87b9-f02afcbf09b8.gif) 100 | 101 | (key sequence in video: ``viByPgg`[v`]^[jjjgv``) 102 | 103 | Mapping suggestion: 104 | ```vim 105 | nnoremap gv `[v`] 106 | ``` 107 | 108 | ## Repeat the last command line 109 | [`@:`](https://vimhelp.org/repeat.txt.html#%40%3A) can be used to repeat the last command line command. More convenient when mapped to a single key and I have currently chosen `,` as my repeat-command key. `,` is used to repeat the last `fFtT` motion in reverse direction, but I seldom use it so I have a mapping that shadows this builtin: 110 | 111 | ```vim 112 | nnoremap , @: 113 | ``` 114 | 115 | Alternative mapping suggestions might be the following, but I know I sometimes want to perform `j` `repeat-command` `j` `repeat-command` in fast succession. 116 | 117 | ```vim 118 | nnoremap @: 119 | nnoremap . @: 120 | ``` 121 | 122 | ## Retrieve the word under cursor 123 | `ctrl-r` can be used in insert mode and the command line to insert contents of registers, but also some other stuff like the word under the cursor! 124 | 125 | Using `` will put the current word under cursor where you are at, but if you want to do this in a command you dont want this combination to "expand" immediately but rather when you execute the command. I asked a question about this at [reddit](https://www.reddit.com/r/vim/comments/1183ygn/is_it_possible_to_fetch_the_text_under_cursor/), and got replies from Fantastic_Cow7272 and yegappanl that led me to this kind of command: 126 | 127 | - `:!git show `: Run this with the cursor on a commit hash (for example in a git rebase `edit-todo` file) to show git information about that commit. 128 | 129 | ## Recursive macros 130 | By playing the content of a register at the end of the recording, you can make 131 | a macro recursive. (Make sure it has a stop condition or you will have to break 132 | it with ``!) 133 | 134 | If you forgot to do the macro recursive while recording it the first time, you 135 | make it recursive with the sequence `qQ@qq` (assuming your macro is in the `q` 136 | register) 137 | 138 | ### Recursive within line 139 | A movement within a line such as ``f `` (f followed by space) will stop the 140 | recursive macro at the end of a line. This can be used to modify each word on a 141 | line in some way: 142 | 143 | 1. `qq` `ciw"-"f l@q` `q`: surround each "word" on the line in quotes 144 | 2. `qq` `gUiw2f l@q` `q`: upper-case every 2nd "word" on the line 145 | 146 | With both lines below visually selected, replaying macro 1 from above with 147 | `:norm e@q` will turn: 148 | ``` 149 | Recursive over lines 150 | Recursive over lines 151 | ``` 152 | 153 | Into: 154 | ``` 155 | "Recursive" "over" "lines" 156 | "Recursive" "over" "lines" 157 | ``` 158 | 159 | Mapping suggestion: 160 | 161 | ```vim 162 | nnoremap q qqqqq 163 | ``` 164 | 165 | Questions: 166 | 167 | - What is actually different between `-` and `"`? 168 | - Why doesn't this mapping seem to work: `nnoremap q qqqqq`? 169 | - Is there a "within line" version of `:g`? 170 | 171 | ## Repeat last change in all of file ("global repeat", similar to g&) 172 | 173 | [`ctrl-r` in insert mode](https://vimhelp.org/insert.txt.html#i_CTRL-R) can be 174 | used to insert the same text as last time with the dot (`.`) register. With this 175 | kind mapping you can easily repeat a modification like `ciw` (change in word) in 176 | all of the document. 177 | 178 | ![global repeat](global_replace.gif) 179 | 180 | Mapping suggestion: 181 | ```vim 182 | nnoremap g. :%s//./g 183 | ``` 184 | 185 | # Ideas/TODOs 186 | - appending to registers ([`"Ayy`](https://vimhelp.org/change.txt.html#quotea)) 187 | - inserting literal characters ([`ctrl-v`](https://vimhelp.org/insert.txt.html#i_CTRL-V)) 188 | - pasting from register in insert mode ([`ctrl-r`](https://vimhelp.org/insert.txt.html#i_CTRL-R)) 189 | - batch changes (`:cdo`, [`:bufdo`](https://vimhelp.org/windows.txt.html#%3Abufdo), `:windo`, `:argdo`, `:ldo`) 190 | - changelist: go back to previous edit location ([`g;`](https://vimhelp.org/motion.txt.html#g%3B)) 191 | - insert at previous insert location ([`gi`](https://vimhelp.org/insert.txt.html#gi)) 192 | - normal commands in insert mode ([`ctrl-o`](https://vimhelp.org/insert.txt.html#i_CTRL-O)) 193 | -------------------------------------------------------------------------------- /filter_awk.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaddkaka/vim_examples/795358f20c8b52baf6e8ed5528e04223fda3b679/filter_awk.gif -------------------------------------------------------------------------------- /git.md: -------------------------------------------------------------------------------- 1 | I mostly use git from the command line, but there are some vim-git integrations that I really like, listed below. 2 | 3 | For the repository where I spend the majority of my working time, I use `git worktree`s to manage several ongoing items: 4 | 5 | * `main` - worktree that tracks the main branch (readonly intention) 6 | * `feature` - current feature work 7 | * `review` - for code review 8 | * `tmp` 9 | * `hack` 10 | 11 | The worktrees are added from the primary worktree (e.g. `git worktree add ../feature`) and they let me have several branches checked out at the same time. I don't create new worktrees, I reuse the ones I have. 12 | 13 | To work on a new feature I `cd` to `feature/`, create a new branch and launch my editor from there. 14 | 15 | These worktrees share one `.git` (only need to fetch once, and saves disk space compared to having several full clones) and they also share one stash (which means I can `git stash` in one worktree and `git stash pop` in another). They also share the same settings and git hooks. 16 | 17 | `git branch -vv` lists all my local branches and in what worktree they are checked out in. 18 | 19 | To review a branch I use `git switch -d origin/feature_A_from_coworker` to avoid creating a local copy of the branch. 20 | 21 | # git-jump 22 | The git source contains several peripheral tools, [git-jump](https://github.com/git/git/blob/master/contrib/git-jump/git-jump) 23 | is one of them. It can be used to start vim with a bunch of location loaded into quickfix list: 24 | 25 | - `git jump grep banana` quickly find all bananas in the repo 26 | - `git jump merge` quickly fix all merge conflicts (e.g. when rebasing) 27 | - `git jump diff` revisit all positions of unstaged changes 28 | - `git jump diff HEAD~` 29 | 30 | # vim-fugitive 31 | The parts of [vim-fugitive](https://github.com/tpope/vim-fugitive) I use the most: 32 | 33 | - `:Ggrep banana` - find all bananas in the repo 34 | - `:Git blame`, mappings: 35 | - `P` reblame at `commit~` 36 | - `o` view the patch 37 | - `:Gvsplit main:%` - inspect the current file's state at branch "main" 38 | - `:Gdiffsplit main:%` compare with main branch 39 | 40 | Related config: 41 | ```vim 42 | Plug 'tpope/vim-fugitive' 43 | nnoremap g :Ggrep -q 44 | nnoremap gb Git blame 45 | ``` 46 | 47 | # fzf 48 | [fzf](https://github.com/junegunn/fzf)'s vim plugin has the handy command `:Gfiles` to quickly find files in a git repo. 49 | I also have `:Files %:h` mapped to quickly find other files in the same folder as the current file. 50 | 51 | Related [config] (https://github.com/kaddkaka/dotfiles/blob/main/dot_config/nvim/small.vim#L47): 52 | ```vim 53 | Plug 'junegunn/fzf', { 'do': { -> fzf#install() } } 54 | Plug 'junegunn/fzf.vim' 55 | 56 | map f GFiles 57 | map F Files 58 | map l Files %:h 59 | ``` 60 | 61 | # tig 62 | I use [tig](https://github.com/jonas/tig) to get an overview the commit history. It has builtin mapping for pressing `e` at a change in a 63 | diff to immediately open vim at that locaiton. 64 | 65 | I have some bindings for quickly rebasing the top of my commit history: 66 | 67 | ``` 68 | bind main R !git rebase -i %(commit) --keep-empty 69 | bind diff R !git rebase -i %(commit) --keep-empty 70 | bind main S !git rebase -i %(commit) --autosquash 71 | bind diff S !git rebase -i %(commit) --autosquash 72 | ``` 73 | 74 | When I want to fix comments from a Merge Review I have a custom script bound to `E` that starts a rebase edit session 75 | with all changes of that commit in quickfix list with git-jump. 76 | 77 | ``` 78 | bind main E