{"id":13682711,"url":"https://github.com/kaddkaka/vim_examples","last_synced_at":"2025-04-30T09:33:53.497Z","repository":{"id":48915411,"uuid":"429980720","full_name":"kaddkaka/vim_examples","owner":"kaddkaka","description":"Some examples of vim features captured from terminal with asciinema.","archived":false,"fork":false,"pushed_at":"2024-11-05T20:14:43.000Z","size":143,"stargazers_count":45,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-11-05T21:21:10.164Z","etag":null,"topics":["asciinema","examples","readme-only","showcase","vim","vim-bindings","vim-tutorial"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kaddkaka.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-20T01:32:35.000Z","updated_at":"2024-11-05T20:14:47.000Z","dependencies_parsed_at":"2023-11-28T08:25:53.547Z","dependency_job_id":"3db29283-38f7-4c58-a04d-7eece82f9721","html_url":"https://github.com/kaddkaka/vim_examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaddkaka%2Fvim_examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaddkaka%2Fvim_examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaddkaka%2Fvim_examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaddkaka%2Fvim_examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaddkaka","download_url":"https://codeload.github.com/kaddkaka/vim_examples/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224206195,"owners_count":17273404,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asciinema","examples","readme-only","showcase","vim","vim-bindings","vim-tutorial"],"created_at":"2024-08-02T13:01:51.566Z","updated_at":"2024-11-12T02:31:35.540Z","avatar_url":"https://github.com/kaddkaka.png","language":null,"funding_links":[],"categories":["Others"],"sub_categories":[],"readme":"# Builtins\n\n## Increment numbers (incremental sequence)\n[`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)\n\nbefore -\u003e after:\n\n\u003cimg src=\"https://user-images.githubusercontent.com/4508793/204096261-8d0a9c32-3b1b-4f33-b512-0e4bf064ea1b.png\" width=\"45%\" /\u003e -\u003e \u003cimg src=\"https://user-images.githubusercontent.com/4508793/204096275-f8c61e95-c763-473d-a69e-393537422bce.png\" width=\"45%\" /\u003e\n\n\u003cimg alt=\"g_ctrl_a\" src=\"https://user-images.githubusercontent.com/4508793/142710199-0d605d4c-9d0a-42d2-976a-6d15742834b1.gif\" /\u003e\n\n(key sequence in video: `jVG^A..uuugvg^A..uuugv10g^A`)\n\nTip: This also works with rectangle selection (`ctrl-v`).\n\n## Replay macro on each line in visual selection\n[`: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.\n\n![norm_q](https://user-images.githubusercontent.com/4508793/143023739-894e32bf-c1f7-4a50-8c03-19b0771fb87b.gif)\n\n(key sequence in video: `qqIconst ^[A;^[qjVjjj:norm @q\u003center\u003e`, q register content: `Iconst ^[A;^[`)\n\n## Navigate quickfix list\nThe [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).\n\n- [`: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).\n- [`:copen`](https://vimhelp.org/quickfix.txt.html#%3Acopen) is used to open the quickfix window to show the quickfix list.\n\n![qflist](https://user-images.githubusercontent.com/4508793/143112529-717fb6ea-d7ab-4f87-a5da-4c0df5f2a9c4.gif)\n\nMapping suggestions:\n```vim\nnnoremap \u003ca-j\u003e \u003ccmd\u003ecnext\u003ccr\u003e\nnnoremap \u003ca-k\u003e \u003ccmd\u003ecprev\u003ccr\u003e\n```\n\n- `:vimgrep /def test/ **/*.py` find all matches of \"def test\" in all pythons files recursively from cwd.\n- [`:cdo`](https://vimhelp.org/quickfix.txt.html#%3Acdo) executes a command at each entry in the quickfix list.\n\n## Use `!` to run external command (e.g. sort lines)\n[Filters](https://vimhelp.org/change.txt.html#filter) can be run for example by typing\n[`!`](https://vimhelp.org/change.txt.html#%21) with a visual selection. All text will be filtered\nthrough an external command. To sort all lines by numeric sort, use `!sort -n`.\n\n![filter in selection](sort.gif)\n\n(key sequence in video: `vip!sort -n\u003center\u003e`)\n\n## Use filtering and awk to sum all numbers in a specific column\nawk is a powerful tool, here used to sum all the fields of a specific column:\n - `:%! awk '{print; s+=$2} END {print s}'` sums the second field (`$2`) of\n   each line and prints the total at the `END`.\n\n![filter with awk](filter_awk.gif)\n\n## global: repeat a command for each line matching a pattern\nWith [`:global`](https://vimhelp.org/repeat.txt.html#%3Aglobal) a command can be repeated for each\nline that matches a pattern. Default pattern is last used search pattern.\n\n - `:g//d` deletes all lines matching the last search pattern\n - `:g//d E` delete the same lines and appends each delete to register `e` which can be pasted with\n   `\"ep`\n\n![global delete, append to register](global.gif)\n\nkey sequence in video: `*:g//d\u003center\u003e` `u` `:g//d E\u003center\u003ep`\n\n - `:g/pattern/norm @q`, play macro on each line matching pattern.\n - `:v/;$/ s/$/;`, Add `;` to all lines that does not end in semicolon.\n\nMore g power at [https://vim.fandom.com/wiki/Power_of_g](https://vim.fandom.com/wiki/Power_of_g)\n\n## Replace only within selection\nThe 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.\n\n![sub_in_selection](https://user-images.githubusercontent.com/4508793/143133723-69acd9ba-1516-4d21-a546-b04d9a84e622.gif)\n\n(key sequence in video: `wwww^VG$se/o\u003center\u003e`)\n\nMapping suggestion:\n```vim\nxnoremap s :s/\\%V\n```\n\n## Delete to search motion\nNormal 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.\n\n- `d/en\u003cc-g\u003e\u003cc-g\u003e\u003center\u003e` Will delete everything up to the 3rd match of search pattern \"en\".\n- 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.\n\n![d to search](https://user-images.githubusercontent.com/4508793/143139836-a1ac23f4-9367-447b-867c-06b6a7c7fdc3.gif)\n\n(key sequence in video: `d/en^G^G\u003center\u003e`)\n\n## Select what was just pasted, reselect last selection\n\n[`` `[ ``](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.\n\n![gv](https://user-images.githubusercontent.com/4508793/143313428-e6bba2f8-425b-468e-87b9-f02afcbf09b8.gif)\n\n(key sequence in video: ``viByPgg`[v`]^[jjjgv``)\n\nMapping suggestion:\n```vim\nnnoremap \u003cleader\u003egv `[v`]\n```\n\n## Repeat the last command line\n[`@:`](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:\n\n```vim\nnnoremap , @:\n```\n\nAlternative mapping suggestions might be the following, but I know I sometimes want to perform `j` `repeat-command` `j` `repeat-command` in fast succession.\n\n```vim\nnnoremap \u003ca-.\u003e @:\nnnoremap \u003cleader\u003e. @:\n```\n\n## Retrieve the word under cursor\n`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!\n\nUsing `\u003cc-r\u003e\u003cc-w\u003e` 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:\n\n- `:!git show \u003ccword\u003e`: 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.\n\n## Recursive macros\nBy playing the content of a register at the end of the recording, you can make\na macro recursive. (Make sure it has a stop condition or you will have to break\nit with `\u003cctrl-c\u003e`!)\n\nIf you forgot to do the macro recursive while recording it the first time, you\nmake it recursive with the sequence `qQ@qq` (assuming your macro is in the `q`\nregister)\n\n### Recursive within line\nA movement within a line such as ``f `` (f followed by space) will stop the\nrecursive macro at the end of a line. This can be used to modify each word on a\nline in some way:\n\n1. `qq` `ciw\"\u003cc-r\u003e-\"\u003cesc\u003ef l@q` `q`: surround each \"word\" on the line in quotes\n2. `qq` `gUiw2f l@q` `q`:  upper-case every 2nd \"word\" on the line\n\nWith both lines below visually selected, replaying macro 1 from above with\n`:norm e@q` will turn:\n```\n   Recursive over lines\nRecursive over lines\n```\n\nInto:\n```\n   \"Recursive\" \"over\" \"lines\"\n\"Recursive\" \"over\" \"lines\"\n```\n\nMapping suggestion:\n\n```vim\nnnoremap \u003cleader\u003eq qqqqq\n```\n\nQuestions:\n\n- What is actually different between `\u003cctrl-r\u003e-` and `\u003cctrl-r\u003e\"`?\n- Why doesn't this mapping seem to work: `nnoremap \u003cleader\u003eq qqqqq`?\n- Is there a \"within line\" version of `:g`?\n\n## Repeat last change in all of file (\"global repeat\", similar to g\u0026)\n\n[`ctrl-r` in insert mode](https://vimhelp.org/insert.txt.html#i_CTRL-R) can be\nused to insert the same text as last time with the dot (`.`) register. With this\nkind mapping you can easily repeat a modification like `ciw` (change in word) in\nall of the document.\n\n![global repeat](global_replace.gif)\n\nMapping suggestion:\n```vim\nnnoremap g. :%s//\u003cc-r\u003e./g\u003cesc\u003e\n```\n\n# Ideas/TODOs\n- appending to registers ([`\"Ayy`](https://vimhelp.org/change.txt.html#quotea))\n- inserting literal characters ([`ctrl-v`](https://vimhelp.org/insert.txt.html#i_CTRL-V))\n- pasting from register in insert mode ([`ctrl-r`](https://vimhelp.org/insert.txt.html#i_CTRL-R))\n- batch changes (`:cdo`, [`:bufdo`](https://vimhelp.org/windows.txt.html#%3Abufdo), `:windo`, `:argdo`, `:ldo`)\n- changelist: go back to previous edit location ([`g;`](https://vimhelp.org/motion.txt.html#g%3B))\n- insert at previous insert location ([`gi`](https://vimhelp.org/insert.txt.html#gi))\n- normal commands in insert mode ([`ctrl-o`](https://vimhelp.org/insert.txt.html#i_CTRL-O))\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaddkaka%2Fvim_examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaddkaka%2Fvim_examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaddkaka%2Fvim_examples/lists"}