From 062412ccd90803f883e82100e632d57ca3dcfe1e Mon Sep 17 00:00:00 2001 From: FireIsGood Date: Wed, 22 May 2024 03:18:35 -0700 Subject: [PATCH] fix: vim format Fixes the tables --- source/_posts/vim.md | 420 ++++++++++++++++++++----------------------- 1 file changed, 193 insertions(+), 227 deletions(-) diff --git a/source/_posts/vim.md b/source/_posts/vim.md index 7b2f9c9..3ba4b65 100644 --- a/source/_posts/vim.md +++ b/source/_posts/vim.md @@ -2,32 +2,33 @@ title: Vim date: 2020-11-25 18:28:43 background: bg-[#46933f] -tags: - - vi - - text - - editor - - terminal - - shortcut +tags: + - vi + - text + - editor + - terminal + - shortcut categories: - - Toolkit + - Toolkit intro: | - A useful collection of [Vim](http://www.vim.org/) 8.2 quick reference cheat sheets to help you learn vim editor faster. + A useful collection of [Vim](http://www.vim.org/) 8.2 quick reference cheat sheets to help you learn vim editor faster. plugins: - - copyCode + - copyCode --- -Getting Started ---------------- +## Getting Started ### Motion Diagrams {.row-span-2} ```bash ▼/▶ Cursor ▽/▷ Target ``` + #### Left-right motions + ```bash -┌───────────── | -├───────────── 0 $ ──────────────┐ +┌───────────── | +├───────────── 0 $ ──────────────┐ │ ┌────────── ^ fe ────────┐ │ │ │ ┌─────── Fo te ───────┐│ │ │ │ │┌────── To 30| ───┐ ││ │ @@ -37,79 +38,88 @@ Getting Started ▽ ▽ ▽▽ ▽ ▽ ▽▼ ▼▽ ▽ ▽ ▽ ▽▽ ▽ echo "A cheatsheet from quickref.me" ``` + #### Up-down motions + ```bash - SCREEN 1 START ┌─┬─────────▷ #!/usr/bin/python - │ │ ┌───▷ + │ │ ┌───▷ │ │ │ print("Hello") │ │ { } ▶ print("Vim") │ │ │ print("!") - │ │ └─▷ + │ │ └─▷ │ │ ┌───┬───▷ print("Welcome") G gg H M L k j ▶ print("to") │ │ └─▷ print("cheatsheets.zip") │ │ print("/vim") -│ │ -│ └─────▷ +│ │ +│ └─────▷ │ - SCREEN 1 END └──────────────▷ print("SCREEN 2") -``` +``` ### Motions {.row-span-2} | Shortcut | Description | -|--------------------------------|-------------------| +| ------------------------------ | :---------------- | | `h` _\|_ `j` _\|_ `k` _\|_ `l` | Arrow keys | | `` _/_ `` | Half-page up/down | | `` _/_ `` | Page up/down | + {.shortcuts} #### Words{.left-text} + | Shortcut | Description | -|--------------|---------------------------| +| ------------ | :------------------------ | | `b` _/_ `w` | Previous/Next word | | `ge` _/_ `e` | Previous/Next end of word | + {.shortcuts} #### Line | Shortcut | Description | -|----------------------|-----------------------------| +| -------------------- | :-------------------------- | | `0` _(zero)_ _/_ `$` | Start/End of line | | `^` | Start of line _(non-blank)_ | + {.shortcuts} #### Character -| Shortcut | Description | -|---------------|-------------------------------------| -| `Fe` _/_ `fe` | Move to previous/next `e` | -| `To` _/_ `to` | Move before/after previous/next `o` | -| `|` _/_ `n|` | Go to first/`n`th column | +| Shortcut | Description | +| ----------------------------------- | :---------------------------------- | +| `Fe` _/_ `fe` | Move to previous/next `e` | +| `To` _/_ `to` | Move before/after previous/next `o` | +| \|_/_ n\| | Go to first/`n`th column | + {.shortcuts} #### Document | Shortcut | Description | -|----------------|--------------------------| +| -------------- | :----------------------- | | `gg` _/_ `G` | First/Last line | | `:n` _\|_ `nG` | Go to line `n` | | `}` _/_ `{` | Next/Previous empty line | + {.shortcuts} #### Window -| Shortcut | Description | -|-------------------------|-----------------------------| -| `H` _/_ `M` _/_ `L` | Top/Middle/Bottom screen | -| `zt` _/_ `zz` _/_ `zb` | Top/Center/Bottom this line | +| Shortcut | Description | +| ---------------------- | :-------------------------- | +| `H` _/_ `M` _/_ `L` | Top/Middle/Bottom screen | +| `zt` _/_ `zz` _/_ `zb` | Top/Center/Bottom this line | + {.shortcuts} - ### Insert Mode + | Shortcut | Description | -|------------------------|-------------------------------| +| ---------------------- | :---------------------------- | | `i` _/_ `a` | Insert before/after cursor | | `I` _/_ `A` | Insert start/end of line | | `o` _/_ `O` _(letter)_ | Insert new line below/above | @@ -117,13 +127,13 @@ G gg H M L k j ▶ print("to") | `C` _/_ `cc` | Change to end of/current line | | `gi` | Insert at last insert point | | `Esc` _\|_ `` | Exit insert mode | + {.shortcuts} - - ### Saving and Exiting + | Shortcut | Description | -|---------------------------|-------------------------| +| ------------------------- | :---------------------- | | `:w` | Save | | `:q` | Close file | | `:wq` _\|_ `:x` _\|_ `ZZ` | Save and quit | @@ -134,25 +144,26 @@ G gg H M L k j ▶ print("to") | `:w` now.txt | Write to `now.txt` | | `:sav` new.txt | Save and edit `new.txt` | | `:w` !sudo tee % | Write to readonly file | + {.shortcuts} - - ### Normal Mode + | Shortcut | Description | -|-----------------------|------------------------------| +| --------------------- | :--------------------------- | | `r` | Replace one character | | `R` | Enter Replace mode | | `u` _/_ `3u` | Undo changes `1` / `3` times | | `U` | Undo changes on one line | | `J` | Join with next line | | `` _/_ 5 `` | Redo changes `1` / `5` times | + {.shortcuts} - ### Cut and paste {.row-span-2} + | Shortcut | Description | -|------------------|-------------------------------| +| ---------------- | :---------------------------- | | `x` | Delete character _(Cut)_ | | `p` _/_ `P` | Paste after/before | | `xp` | Swap two characters | @@ -163,34 +174,35 @@ G gg H M L k j ▶ print("to") | `yy` | Yank line _(Copy)_ | | `"*p` _\|_ `"+p` | Paste from system clipboard | | `"*y` _\|_ `"+y` | Paste to system clipboard | -{.shortcuts} +{.shortcuts} #### In visual mode -| Shortcut | Description | -|--------------|---------------------------| -| `d` _\|_ `x` | Delete selection _(Cut)_ | -| `s` | Replace selection | -| `y` | Yank selection _(Copy)_ | + +| Shortcut | Description | +| ------------ | :----------------------- | +| `d` _\|_ `x` | Delete selection _(Cut)_ | +| `s` | Replace selection | +| `y` | Yank selection _(Copy)_ | + {.shortcuts} - - ### Repeating + | Shortcut | Description | -|----------|---------------------------------------------| +| -------- | :------------------------------------------ | | `.` | Repeat last command | | `;` | Repeat latest `f`, `t`, `F` or `T` | | `,` | Repeat latest `f`, `t`, `F` or `T` reversed | | `&` | Repeat last `:s` | | `@:` | Repeat a command-line command | + {.shortcuts} - - ### Visual mode + | Shortcut | Description | -|-------------|-------------------------| +| ----------- | :---------------------- | | `v` | Enter visual mode | | `V` | Enter visual line mode | | `` | Enter visual block mode | @@ -199,39 +211,35 @@ G gg H M L k j ▶ print("to") {.shortcuts} - - ### Macros + | - | - | -|-------|-----------------------| +| ----- | :-------------------- | | `qi` | Record macro `i` | | `q` | Stop recording macro | | `@i` | Run macro `i` | | `7@i` | Run macro `i` 7 times | | `@@` | Repeat last macro | + {.shortcuts} You can save macro for any letters not just `i` - -Vim Operators ---------- +## Vim Operators ### Usage {.secondary} | Shortcut | Description | -|----------|--------------| +| -------- | :----------- | | `d` | w | | Operator | Motion | Combine [operators](#available-operators) with [motions](#motions) to use them - - - ### Available Operators {.row-span-2} + | Shortcut | Description | -|----------|---------------------------------| +| -------- | :------------------------------ | | `d` | Delete | | `y` | Yank _(copy)_ | | `c` | Change _(delete then insert)_ | @@ -244,11 +252,10 @@ Combine [operators](#available-operators) with [motions](#motions) to use them | `<` | Indent left | | `!` | Filter through external program | - ### Examples {.row-span-2} | Combination | Description | -|----------------------|---------------------------------------| +| -------------------- | :------------------------------------ | | `d`d | Delete current line | | `d`j | Delete two lines | | `d`w | Delete to next word | @@ -261,46 +268,43 @@ Combine [operators](#available-operators) with [motions](#motions) to use them | gg`d`G | Delete a complete document | | gg`=`G | Indent a complete document | | gg`y`G | Copy a whole document | + {.show-header} - - ### Counts + ``` [count] [count] ``` + --- | Combination | Description | -|------------------|----------------------------| +| ---------------- | :------------------------- | | 2`d`d | Delete `2` lines | | 6`y`y | Copy `6` lines | | `d`3w | Delete `3` words | | `d`5j | Delete `5` lines downwards | | `>`4k | Indent `4` lines upwards | - - -Vim Text objects ------------- +## Vim Text objects ### Usage {.secondary} -| Shortcut | Description | - | -|----------|-----------------------------------------------------------|--------------| -| `v` |         i _/_ a | p | -| Operator | inner _/_ around | Text object | + + +| Shortcut | Description | - | +| -------- | :------------------------------------: | :----------- | +| `v` | i _/_ a | p | +| Operator | inner _/_ around | Text object | Operate with an [operator](#available-operators) inner or around text blocks - - - ### Text objects {.row-span-2} | Shortcut | Description | -|------------------------------------------------------|----------------------------------------| +| ---------------------------------------------------- | :------------------------------------- | | p | Paragraph | | w | Word | | W | WORD
_(surrounded by whitespace)_ | @@ -314,23 +318,20 @@ Operate with an [operator](#available-operators) inner or around text blocks See `:help text-objects` - - ### Delete | Shortcut | Description | -|-----------------------------|---------------------------------------| +| --------------------------- | :------------------------------------ | | `d`iw | Delete inner word | | `d`is | Delete inner sentence | | `d`i" | Delete in quotes | | `d`a" | Delete in quotes _(including quotes)_ | | `d`ip | Delete a paragraph | - - ### Selections + | Shortcut | Description | -|-----------------------------------------------------|-------------------------------------------| +| --------------------------------------------------- | :---------------------------------------- | | `v`i" | Select inner quotes "`...`{.underline}" | | `v`a" | Select quotes `"..."`{.underline} | | `v`i[ | Select inner brackets [`...`{.underline}] | @@ -339,11 +340,10 @@ See `:help text-objects` | `v`ip | Select inner paragraph | | `v`ipip | Select more paragraph | - ### Misc | Shortcut | Description | -|-----------------------------|--------------------------------------| +| --------------------------- | :----------------------------------- | | `c`iw | Change inner word | | `c`i" | Change inner quotes | | `c`it | Change inner tags (HTML) | @@ -351,17 +351,12 @@ See `:help text-objects` | `y`ip | Yank inner paragraph | | `y`ap | Yank paragraph _(including newline)_ | - - - -Vim Multiple files -------------- - - +## Vim Multiple files ### Buffers + | - | - | -|------------|----------------------------------| +| ---------- | :------------------------------- | | `:e file` | Edit a file in a new buffer | | `:bn` | Go to the next buffer | | `:bp` | Go to the previous buffer | @@ -375,30 +370,29 @@ Vim Multiple files | `:wn` | Write file and move to next | | `:tab ba` | Edit all buffers as tabs | - - ### Windows -| - | - | -|----------------------|-----------------------------| -| `` `s` | Split window | -| `` `v` | Split window vertically | -| `` `w` | Switch windows | -| `` `q` | Quit a window | -| `` `T` | Break out into a new tab | -| `` `x` | Swap current with next | -| `` `-` _/_ `+` | Decrease/Increase height | -| `` `<` _/_ `>` | Decrease/Increase width | -| `` `|` | Max out the width | -| `` `=` | Equally high and wide | -| `` `h` _/_ `l` | Go to the left/right window | -| `` `j` _/_ `k` | Go to the up/down window | + +| - | - | +| ----------------------- | :-------------------------- | +| `` `s` | Split window | +| `` `v` | Split window vertically | +| `` `w` | Switch windows | +| `` `q` | Quit a window | +| `` `T` | Break out into a new tab | +| `` `x` | Swap current with next | +| `` `-` _/_ `+` | Decrease/Increase height | +| `` `<` _/_ `>` | Decrease/Increase width | +| `` \| | Max out the width | +| `` `=` | Equally high and wide | +| `` `h` _/_ `l` | Go to the left/right window | +| `` `j` _/_ `k` | Go to the up/down window | + {.shortcuts} - - ### Tabs + | Shortcut | Description | -|----------------|-----------------------------------| +| -------------- | :-------------------------------- | | `:tabe [file]` | Edit file in a new tab | | `:tabf [file]` | Open if exists in new tab | | `:tabc` | Close current tab | @@ -410,25 +404,22 @@ Vim Multiple files | `:tabn` | Go to next tab | | `:tabp` | Go to previous tab | - #### Normal mode + | Shortcut | Description | -|----------|-------------------------------| +| -------- | :---------------------------- | | `gt` | Go to next tab | | `gT` | Go to previous tab | | `2gt` | Go to tab number `2` | + {.shortcuts} - - - - -Vim Search and Replace ------------------- +## Vim Search and Replace ### Search + | - | - | -|----------|-------------------------------------| +| -------- | :---------------------------------- | | `/foo` | Search forward | | `/foo\c` | Search forward _(case insensitive)_ | | `?foo` | Search backward | @@ -437,11 +428,11 @@ Vim Search and Replace | `N` | Previous match | | `*` | Search for current word forward | | `#` | Search for current word backward | + {.shortcuts} - - ### Replace LINE + ```vim :[range]s/{pattern}/{str}/[flags] ``` @@ -449,7 +440,7 @@ Vim Search and Replace --- | | | -|-------------------|----------------------------------| +| ----------------- | :------------------------------- | | `:s/old/new` | Replace first | | `:s/old/new/g` | Replace all | | `:s/\vold/new/g` | Replace all with [regex](/regex) | @@ -458,6 +449,7 @@ Vim Search and Replace | `:2,6s/old/new/g` | Replace between lines `2`-`6` | ### Replace FILE + ```vim :%s/{pattern}/{str}/[flags] ``` @@ -465,18 +457,17 @@ Vim Search and Replace --- | | | -|-------------------|----------------------------------| +| ----------------- | :------------------------------- | | `:%s/old/new` | Replace first | | `:%s/old/new/g` | Replace all | | `:%s/old/new/gc` | Replace all _(Confirm)_ | | `:%s/old/new/gi` | Replace all _(ignore case)_ | | `:%s/\vold/new/g` | Replace all with [regex](/regex) | - - ### Ranges {.row-span-2} + | - | - | -|---------|-------------------| +| ------- | :---------------- | | `%` | Entire file | | `’<,’>` | Current selection | | `5` | Line `5` | @@ -487,17 +478,16 @@ Vim Search and Replace | `,3` | Next `3` lines | | `-3,` | Forward `3` lines | - - - ### Global command {.row-span-2} + ```vim :[range]g/{pattern}/[command] ``` + --- | | | -|--------------|------------------------------------| +| ------------ | :--------------------------------- | | `:g/foo/d` | Delete lines containing `foo` | | `:g!/foo/d` | Delete lines not containing `foo` | | `:g/^\s*$/d` | Delete all blank lines | @@ -506,35 +496,31 @@ Vim Search and Replace | `:g/^/m0` | Reverse a file | | `:g/^/t.` | Duplicate every line | - - ### Inverse :g + ```vim :[range]v/{pattern}/[command] ``` + --- | | | -|------------|------------------------------------------------------------| +| ---------- | :--------------------------------------------------------- | | `:v/foo/d` | Delete lines not containing `foo`
_(also `:g!/foo/d`)_ | - - - ### Flags | - | - | -|-----|---------------------------| +| --- | :------------------------ | | `g` | Replace all occurrences | | `i` | Ignore case | | `I` | Don't ignore case | | `c` | Confirm each substitution | - - ### Substitute expression (magic) + | - | - | -|---------------|----------------------------------| +| ------------- | :------------------------------- | | `&` _\|_ `\0` | Replace with the whole matched | | `\1`...`\9` | Replace with the group 0-9 | | `\u` | Uppercase next letter | @@ -544,9 +530,8 @@ Vim Search and Replace | `\e` | End of `\u`, `\U`, `\l` and `\L` | | `\E` | End of `\u`, `\U`, `\l` and `\L` | - - ### Examples {.col-span-2} + ```c {.wrap} :s/a\|b/xxx\0xxx/g # Modifies "a b" to "xxxaxxx xxxbxxx" :s/test/\U& file/ # Modifies "test" to "TEST FILE" @@ -566,11 +551,7 @@ Vim Search and Replace :g//,//d # Delete inclusively from to ``` - - -Vimdiff -------- - +## Vimdiff ### Usage {.secondary} @@ -580,32 +561,32 @@ $ vimdiff file1 file2 [file3] $ vim -d file1 file2 [file3] ``` - - ### Editing {.row-span-2} + ``` :[range]diffget [bufspec] :[range]diffput [bufspec] ``` + --- + | Shortcut | Description | -|---------------------|-------------------------| +| ------------------- | :---------------------- | | `do` _/_ `:diffget` | Obtain (get) difference | | `dp` _/_ `:diffput` | Put difference | | `:dif` | Re-scan differences | | `:diffo` | Switch off diff mode | | `:1,$+1diffget` | Get all differences | | `ZQ` | Quit without changes | + {.shortcuts} See: [Ranges](#ranges) - - ### Folds {.row-span-2} | Shortcut | Description | -|---------------|------------------------------| +| ------------- | :--------------------------- | | `zo` _/_ `zO` | Open | | `zc` _/_ `zC` | Close | | `za` _/_ `zA` | Toggle | @@ -615,32 +596,26 @@ See: [Ranges](#ranges) | `zm` | Fold more _(foldlevel += 1)_ | | `zr` | Fold less _(foldlevel -= 1)_ | | `zx` | Update folds | + {.shortcuts} - - ### Jumping +
| Shortcut | Description | -|----------|---------------------| +| -------- | :------------------ | | `]c` | Next difference | | `[c` | Previous difference | + {.shortcuts} - - - - - -Miscellaneous -------------- - +## Miscellaneous ### Case {.row-span-2} | Shortcut | Description | -|----------------|-------------------------| +| -------------- | :---------------------- | | `vU` | _Uppercase_ character | | `vu` | _Lowercase_ character | | `~` | _Toggle case_ character | @@ -652,28 +627,25 @@ Miscellaneous | `V~` _/_ `g~~` | _Toggle case_ line | | `gggUG` | _Uppercase_ all text | | `ggguG` | _Lowercase_ all text | -| `ggg~G` | _Toggle case_ all text | +| `ggg~G` | _Toggle case_ all text | + {.shortcuts} - - - ### Jumping | Shortcut | Description | -|----------|--------------------------| +| -------- | :----------------------- | | `` | Go back to previous | | `` | Go forward | | `gf` | Go to file in cursor | | `ga` | Display hex, ascii value | + {.shortcuts} - - - ### Misc command-lines {.row-span-2} + | - | - | -|----------------|--------------------------------------------| +| -------------- | :----------------------------------------- | | `:h` | Help open help view | | `:edit!` | Reload current file | | `:2,8m0` | Move lines `2`-`8` to `0` | @@ -684,32 +656,31 @@ Miscellaneous | `:set nopaste` | disable Insert Paste sub-mode | | `:cq` | Exiting with an error
_(aborting Git)_ | - ### Navigating -| Shortcut | Description | -|--------------------------|---------------------------| -| `%` | Nearest/matching `{[()]}` | -| `[(` _\|_ `[{` | Previous `(` or `{` | -| `])` _\|_ `]{` | Next `)` or `}` | -| `[m` | Previous method start | -| `[M` | Previous method end | -{.shortcuts} +| Shortcut | Description | +| -------------- | :------------------------ | +| `%` | Nearest/matching `{[()]}` | +| `[(` _\|_ `[{` | Previous `(` or `{` | +| `])` _\|_ `]{` | Next `)` or `}` | +| `[m` | Previous method start | +| `[M` | Previous method end | +{.shortcuts} ### Counters | Shortcut | Description | -|----------|-----------------| +| -------- | :-------------- | | `` | Increase number | | `` | Decrease number | -{.shortcuts} +{.shortcuts} ### Tags {.row-span-2 .col-span-2} | Shortcut | Description | -|----------------------|-------------------------------------------------| +| -------------------- | :---------------------------------------------- | | `:tag Classname` | Jump to first definition of Classname | | `` | Jump to definition | | `g]` | See all definitions | @@ -717,105 +688,100 @@ Miscellaneous | ` ` | Back/forward | | `:tselect Classname` | Find definitions of Classname | | `:tjump Classname` | Find definitions of Classname (auto-select 1st) | + {.shortcuts} - ### Formatting + | - | - | -|---------|----------------------------------| +| ------- | -------------------------------- | | `:ce 8` | Center lines between `8` columns | | `:ri 4` | Right-align lines at `4` columns | | `:le` | Left-align lines | See `:help formatting` - - - ### Marks {.col-span-2 .row-span-4} -| Shortcut | Description | -| --- | --- | -| \`^ | Last position of cursor in insert mode | +| Shortcut | Description | +| ------------------- | :--------------------------------------------------- | +| \`^ | Last position of cursor in insert mode | | \`. | Last change in current buffer | | \`" | Last exited current buffer | | \`0 | In last file edited | -| '' | Back to line in current buffer where jumped from | -| \`\` | Back to position in current buffer where jumped from | +| '' | Back to line in current buffer where jumped from | +| \`\` | Back to position in current buffer where jumped from | | \`[ | To beginning of previously changed or yanked text | | \`] | To end of previously changed or yanked text | | \`< | To beginning of last visual selection | | \`> | To end of last visual selection | - -| `ma` | Mark this cursor position as `a` | +| `ma` | Mark this cursor position as `a` | | \`a | Jump to the cursor position `a` | -| `'a` | Jump to the beginning of the line with position `a` | -| d'a | Delete from current line to line of mark `a` | +| `'a` | Jump to the beginning of the line with position `a` | +| d'a | Delete from current line to line of mark `a` | | d\`a | Delete from current position to position of mark `a` | -| c'a | Change text from current line to line of `a` | +| c'a | Change text from current line to line of `a` | | y\`a | Yank text from current position to position of `a` | +| `:marks` | List all current marks | +| `:delm a` | Delete mark `a` | +| `:delm a-d` | Delete marks `a`, `b`, `c`, `d` | +| `:delm abc` | Delete marks `a`, `b`, `c` | -| `:marks` | List all current marks | -| `:delm a` | Delete mark `a` | -| `:delm a-d` | Delete marks `a`, `b`, `c`, `d` | -| `:delm abc` | Delete marks `a`, `b`, `c` | {.shortcuts} - - ### Calculator | Shortcut | Description | -|------------------|------------------| -| `` `=` 7*7 | Shows the result | +| ---------------- | :--------------- | +| `` `=` 7\*7 | Shows the result | | `` `=` 10/2 | Shows the result | + {.shortcuts} Do this in INSERT mode - - - ### Shell + | - | - | -|--------------|--------------------------------| +| ------------ | :----------------------------- | | `:!` | Interpret Shell Command | | `:r!` | Read in output of shell | | `:r!date` | Insert date | | `:!!date` | Replace current line with date | - - - ### Command line | Shortcut | Description | -|--------------|-------------------------------------------| +| ------------ | :---------------------------------------- | | `` | Insert current word into the command line | | `"` | Paste from " register | | `` | Auto-completion of path in insert mode | + {.shortcuts} - ### Tricks + Remove duplicate lines + ```shell script :sort | %!uniq -u ``` + To number the lines in the file + ```shell script :%!cat -n ``` + Copy whole doc to clipboard + ```shell script :%w !pbcopy # Mac OS X :%w !xclip -i -sel c # GNU/Linux :%w !xsel -i -b # GNU/Linux ``` - -Also see --------- +## Also see - [Devhints](https://devhints.io/vim) _(devhints.io)_ - [Vim cheatsheet](https://vim.rtorr.com/) _(vim.rotrr.com)_