From de8c6af157868618908c563fbddf25103cd55f26 Mon Sep 17 00:00:00 2001 From: Randy8080 Date: Thu, 14 Jan 2021 20:09:36 +0800 Subject: [PATCH] Optimized vim cheat sheet --- regex.md | 1 + vim.md | 889 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 593 insertions(+), 297 deletions(-) diff --git a/regex.md b/regex.md index f70132b..657cb77 100644 --- a/regex.md +++ b/regex.md @@ -25,6 +25,7 @@ This is a quick cheat sheet to getting started with regular expressions. - [Regex in PHP](#regex-in-php) _(quickref.me)_ - [Regex in Java](#regex-in-java) _(quickref.me)_ - [Regex in MySQL](#regex-in-mysql) _(quickref.me)_ +- [Regex in Vim](/vim#search-and-replace) _(quickref.me)_ - [Regex in Emacs](/emacs#search) _(quickref.me)_ - [Online regex tester](https://regex101.com/) _(regex101.com)_ {.cols-2} diff --git a/vim.md b/vim.md index af9eb0f..e40e0d1 100644 --- a/vim.md +++ b/vim.md @@ -17,24 +17,25 @@ intro: | Getting started {.cols-3} --------------- -### Visualization +### Motion Diagrams {.row-span-2} ```bash ▼/▶ Cursor ▽/▷ Target ``` ---- +#### Left-right motions ```bash -┌───────────── 0 -│ ┌────────── ^ $ ──────────────┐ -│ │ ┌─────── Fo fe ────────┐ │ -│ │ │┌────── To te ───────┐│ │ -│ │ ││ ┌──── ge w ───┐ ││ │ -│ │ ││ │ ┌── b e ─┐ │ ││ │ -│ │ ││ │ │ ┌h l┐ │ │ ││ │ -▽ ▽ ▽▽ ▽ ▽ ▽▼ ▼▽ ▽ ▽ ▽▽ ▽ +┌───────────── | +├───────────── 0 $ ──────────────┐ +│ ┌────────── ^ fe ────────┐ │ +│ │ ┌─────── Fo te ───────┐│ │ +│ │ │┌────── To 30| ───┐ ││ │ +│ │ ││ ┌──── ge w ───┐ │ ││ │ +│ │ ││ │ ┌── b e ─┐ │ │ ││ │ +│ │ ││ │ │ ┌h l┐ │ │ │ ││ │ +▽ ▽ ▽▽ ▽ ▽ ▽▼ ▼▽ ▽ ▽ ▽ ▽▽ ▽ echo "A cheatsheet from quickref.me" ``` ---- +#### Up-down motions ```bash - SCREEN 1 START ┌─┬─────────▷ #!/usr/bin/python @@ -53,401 +54,679 @@ G gg H M L k j ▶ print("to") └──────────────▷ print("SCREEN 2") ``` -### Navigating {.row-span-3} +### Motions {.row-span-2} -| Shortcut | Description | -| --- | --- | -| `h` `j` `k` `l` | Arrow keys | -| `` _\|_ `` | Half-page up/down | -| `` _\|_ `` | Page up/down | +| Shortcut | Description | +|--------------------------------|-------------------| +| `h` _\|_ `j` _\|_ `k` _\|_ `l` | Arrow keys | +| `` _/_ `` | Half-page up/down | +| `` _/_ `` | Page up/down | {.shortcuts} -#### Words - -| Shortcut | Description | -| --- | --- | -| `b` _\|_ `w` | Previous/next word | -| `ge` _\|_ `e` | Previous/next end of word | +#### Words{.left-text} +| Shortcut | Description | +|---------------|---------------------------| +| `b` _/_ `w` | Previous/Next word | +| `ge` _/_ `e` | Previous/Next end of word | {.shortcuts} #### Line -| Shortcut | Description | -| --- | --- | -| `0` _(zero)_ | Start of line | -| `^` | Start of line _(after whitespace)_ | -| `$` | End of line | +| Shortcut | Description | +|----------------------|-----------------------------| +| `0` _(zero)_ _/_ `$` | Start/End of line | +| `^` | Start of line _(non-blank)_ | {.shortcuts} #### Character -| Shortcut | Description | -| --- | --- | -| `fe` | Go forward to character `e` | -| `Fo` | Go backward to character `o` | +| 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` | First line | -| `G` | Last line | -| `:n` | Go to line `n` | -| `nG` | Go to line `n` | +| Shortcut | Description | +|--------------|----------------------------| +| `gg` _/_ `G` | First/Last line | +| `:n` _\|_ `nG` | Go to line `n` | +| `}` _/_ `{` | Next/Previous empty line | {.shortcuts} #### Window -| Shortcut | Description | -| --- | --- | -| `zz` | Center this line | -| `zt` | Top this line | -| `zb` | Bottom this line | -| `H` | Move to top of screen | -| `M` | Move to middle of screen | -| `L` | Move to bottom of screen | -{.shortcuts} - -#### Search - -| Shortcut | Description | -| --- | --- | -| `n` | Next matching search pattern | -| `N` | Previous match | -| `*` | Next whole word under cursor | -| `#` | Previous whole word under cursor | -{.shortcuts} - -#### Tab pages - -| Shortcut | Description | -| --- | --- | -| `:tabedit [file]` | Edit file in a new tab | -| `:tabfind [file]` | Open file if exists in new tab | -| `:tabclose` | Close current tab | -| `:tabs` | List all tabs | -| `:tabfirst` | Go to first tab | -| `:tablast` | Go to last tab | -| `:tabn ` | Go to next tab | -| `:tabp ` | Go to previous tab | - - -### Editing - -| Shortcut | Description | -| --- | --- | -| `a` | Append | -| `A` | Append from end of line | -| `i` | Insert | -| `o` | Next line | -| `O` | Previous line | - -| `s` | Delete char and insert | -| `S` | Delete line and insert | -| `C` | Delete until end of line and insert | - -| `r` | Replace one character | -| `R` | Enter Replace mode | - -| `u` | Undo changes | -| `` | Redo changes | +| Shortcut | Description | +|-------------------------|-----------------------------| +| `H` _/_ `M`_/_ `L` | Top/Middle/Bottom of screen | +| `zt` _/_ `zz` _/_ `zb` | Top/Center/Bottom this line | {.shortcuts} -### Exiting - -| Shortcut | Description | -| -------------- | -------------------------------- | -| `:qa` | Close all files | -| `:qa!` | Close all files, abandon changes | - -| `:w` | Save | -| `:wq` _\|_ `:x` | Save and close file | - -| `:q` | Close file | -| `:q!` | Close file, abandon changes | - -| `ZZ` | Save and quit | -| `ZQ` | Quit without checking changes | +### 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 | +| `s` _/_ `S` | Delete char/line and insert | +| `C` | Change to end of line | +| `gi` | Insert at last insert point | +| `Esc` _\|_ `` | Exit insert mode | {.shortcuts} -### Clipboard -| Shortcut | Description | -| --- | --- | -| `x` | Delete character | - -| `dd` | Delete line _(Cut)_ | -| `yy` | Yank line _(Copy)_ | - -| `p` | Paste | -| `P` | Paste before | - -| `"*p` _\|_ `"+p` | Paste from system clipboard | -| `"*y` _\|_ `"+y` | Paste to system clipboard | +### Saving and Exiting +| Shortcut | Description | +|---------------------------|---------------------------------| +| `:w` | Save | +| `:q` | Close file | +| `:wqa` | Save and quit on all tabs | +| `:wq` _\|_ `:x` _\|_ `ZZ` | Save and close file | +| `:q!` _\|_ `ZQ` | Close file, ignore changes | +| `:qa` | Close all files | +| `:qa!` | Close all files, ignore changes | +| `:w new.txt` | Save as `new.txt` | +| `:w !sudo tee %` | Write file readonly | {.shortcuts} -### Visual mode -| Shortcut | Description | -| --- | --- | -| `v` | Enter visual mode | -| `V` | Enter visual line mode | -| `` | Enter visual block mode | + +### 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 | +| `D` | Delete to end of line _(Cut)_ | +| `dw` | Delete word _(Cut)_ | +| `dd` | Delete line _(Cut)_ | +| `ddp` | Swap two lines | +| `yy` | Yank line _(Copy)_ | +| `"*p` _\|_ `"+p` | Paste from system clipboard | +| `"*y` _\|_ `"+y` | Paste to system clipboard | {.shortcuts} #### In visual mode +| 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 | -| --- | --- | -| `d` _\|_ `x` | Delete selection | -| `s` | Replace selection | -| `y` | Yank selection _(Copy)_ | +|-------------|-------------------------| +| `v` | Enter visual mode | +| `V` | Enter visual line mode | +| `` | Enter visual block mode | +| `ggVG` | Select all text | +| `>` _/_ `<` | Shift text right/left | + {.shortcuts} -### Exiting insert mode -| Shortcut | Description | -| --- | --- | -| `Esc` _\|_ `` | Exit insert mode | -| `` | Exit insert mode, and abort current command | +### Macros +| - | - | +|-------|-----------------------| +| `qi` | Record macro `i` | +| `q` | Stop recording macro | +| `@i` | Run macro `i` | +| `7@i` | Run macro `i` 7 times | +| `@@` | Repeat last macro | {.shortcuts} -See [Operators](#operators) for other things you can do. - - - - - - Operators {.cols-3} --------- ### Usage {.secondary} -Operators let you operate in a range of text (defined by *motion*). These are performed in normal mode. -| Shortcut | Description | -| --- | --- | -| `d` | `w` | -| Operator | Motion | +| Shortcut | Description | +|----------|--------------| +| `d` | w | +| Operator | Motion | -### Operators list +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)_ | - -| `>` | Indent right | -| `<` | Indent left | -| `=` | Autoindent | - -| `g~` | Swap case | +| `p` | Paste | +| `=` | Formats code | +| `g~` | Toggle case | | `gU` | Uppercase | | `gu` | Lowercase | - +| `>` | Indent right | +| `<` | Indent left | | `!` | Filter through external program | -{.shortcuts} -See `:help operator` -### Examples +### Examples {.row-span-2} -Combine operators with *motions* to use them. -{.setup} +| Combination | Description | +|----------------------|-----------------------------| +| `d`d | Delete current line | +| `d`j | Delete two lines | +| `d`w | Delete to next word | +| `d`b | Delete to beginning of word | +| `d`fa | Delete until `a` char | +| `d`/hello | Delete until `hello` | +| `c`c | Change current line | +| `y`y | Copy current line | +| `>`j | Indent 2 lines | +| 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 | -| Shortcut | Description | -| --- | --- | -| `d`_d_ | Delete current line | -| `d`_w_ | Delete to next word | -| `d`_b_ | Delete to beginning of word | -| _2_`dd` | Delete 2 lines | -| `d`_ip_ | Delete a text object _(inside paragraph)_ | -| `d` | Delete selection _(in visual mode)_ | -See `:help motion.txt` Text objects {.cols-3} ------------ ### Usage {.secondary} -Text objects let you operate (with an *operator*) in or around text blocks (*objects*). -{.setup} +| Shortcut | Description | - | +|----------|-----------------------------------------------------------|--------------| +| `v` |         i _/_ a | p | +| Operator | inner _/_ around | Text object | + +Operate with an [operator](#available-operators) inner or around text blocks + + -| Shortcut | Description | -| ----------- | --------- | -| `v` | `i` | `p` | -| Operator | [i]nside or [a]round | Text object | -{.css-breakdown} ### Text objects {.row-span-2} -| Shortcut | Description | -| --- | --- | -| `p` | Paragraph | -| `w` | Word | -| `s` | Sentence | +| Shortcut | Description | +|------------------------------------------------------|----------------------------------------| +| p | Paragraph | +| w | Word | +| W | WORD
_(surrounded by whitespace)_ | +| s | Sentence | +| [ ( { \< | A [], (), or {} block | +| ] ) } \> | A [], (), or {} block | +| ' " \` | A quoted string | +| b | A block [( | +| B | A block in [{ | +| t | A HTML tag block | -| `[` `(` `{` `<` | A [], (), or {} block | -| `'` `"` \` | A quoted string | +See `:help text-objects` -| `b` | A block [( | -| `B` | A block in [{ | -| `t` | A XML tag block | + + +### 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}] | +| `v`a[ | Select brackets `[...]`{.underline} | +| `v`iw | Select inner word | +| `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) | +| `c`ip | Change inner paragraph | +| `y`ip | Yank inner paragraph | +| `y`ap | Yank paragraph _(including newline)_ | + + + + +Multiple files {.cols-3} +------------- + + + +### Buffers +| - | - | +|-------------|----------------------------------| +| `:e file` | Edit a file in a new buffer | +| `:bn` | Go to the next buffer | +| `:bp` | Go to the previous buffer | +| `:bd` | Remove file from buffer list | +| `:b 5` | Open buffer #5 | +| `:b file` | Go to a buffer by file | +| `:ls` | List all open buffers | +| `:sp file` | Open and split window | +| `:vs file` | Open and vertically split window | +| `:hid` | Hide this buffer | +| `: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 | {.shortcuts} -### Examples {.row-span-2} -| Shortcut | Description | -| --- | --- | -| `vip` | Select paragraph | -| `vipipipip` | Select more | -| `yip` | Yank inner paragraph | -| `yap` | Yank paragraph (including newline) | +### Tabs +| Shortcut | Description | +|--------------------|-----------------------------------| +| `:tabe [file]` | Edit file in a new tab | +| `:tabf [file]` | Open if exists in new tab | +| `:tabc` | Close current tab | +| `:tabo` | Close other tabs | +| `:tabs` | List all tabs | +| `:tabr` | Go to first tab | +| `:tabl` | Go to last tab | +| `:tabm 0` | Move to position `0` | +| `:tabn` | Go to next tab | +| `:tabp` | Go to previous tab | -| `dip` | Delete inner paragraph | -| `cip` | Change inner paragraph | + +#### Normal mode +| Shortcut | Description | +|----------|-------------------------------| +| `gt` | Go to next tab | +| `gT` | Go to previous tab | +| `2gt` | Go to tab number `2` | {.shortcuts} -See [Operators](#operators) for other things you can do. - -### Diff -``` -vimdiff file1 file2 [file3] + + +Search and Replace {.cols-3} +------------------ + +### Search +| - | - | +|----------|-------------------------------------------------| +| `/foo` | Search forward | +| `/foo\c` | Search forward _(case insensitive)_ | +| `?foo` | Search backward | +| `/\v\d+` | Search with [regex](/regex) | +| `n` | Next matching search pattern | +| `N` | Previous match | +| `*` | Search for current word forward | +| `#` | Search for current word backward | +{.shortcuts} + + + +### Replace LINE +```vim +:[range]s/{pattern}/{str}/[flags] ``` -See differences between files. - +--- -Misc {.cols-3} ----- +| | | +|-------------------|---------------------------------| +| `:s/old/new` | Replace first | +| `:s/old/new/g` | Replace all | +| `:s/\vold/new/g` | Replace all with [regex](/regex)| +| `:s/old/new/gc` | replace all _(Confirm)_ | +| `:s/old/new/i` | Ignore case replace first | +| `:2,6s/old/new/g` | Replace between lines `2`-`6` | -### Folds {.row-span-3} +### Replace FILE +```vim +:%s/{pattern}/{str}/[flags] +``` + +--- + +| | | +|-------------------|-----------------------------| +| `:%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` | +| `5,10` | Lines `5` to `10` | +| `$` | Last line | +| `2,$` | Lines `2` to Last | +| `.` | Current line | +| `,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 containing `foo` | +| `:g!/foo/d` | Delete lines not containing `foo` | +| `:g/^\s*$/d` | Delete all blank lines | +| `:g/foo/t$` | Copy lines containing `foo` to EOF | +| `:g/foo/m$` | Move lines containing `foo` to EOF | +| `:g/^/m0` | Reverse a file | + + + +### 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 | +| `\U` | Uppercase following characters | +| `\l` | Lowercase next letter | +| `\L` | Lowercase following characters | +| `\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" +:s/\(test\)/\U\1\e file/ # Modifies "test" to "TEST file" +:s/\v([abc])([efg])/\2\1/g # Modifies "af fa bg" to "fa fa gb" +:s/\v\w+/\u\0/g # Modifies "bla bla" to "Bla Bla" +:s/\v([ab])|([cd])/\1x/g # Modifies "a b c d" to "ax bx x x" +:%s/.*/\L&/ # Modifies "HTML" to "html" +:s/\v<(.)(\w*)/\u\1\L\2/g # Make every first letter of a word uppercase +:%s/^\(.*\)\n\1/\1/ # Remove duplicate lines +:%s/<\/\=\(\w\+\)\>/\U&/g # Convert HTML-Tags to uppercase +:g/^pattern/s/$/mytext # Find and append text to the end +:g/pattern/normal @q # Run a macro on matching lines +/^\(.*\)\(\r\?\n\1\)\+$ # View the duplicates lines +:v/./,/./-j # Compress blank lines into a blank line +:g//,//d # Delete inclusively from to +``` + + + +Vimdiff {.cols-3} +------- + + +### Usage {.secondary} + +```shell script + +$ 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 | - +| `zo` _/_ `zO` | Open | +| `zc` _/_ `zC` | Close | +| `za` _/_ `zA` | Toggle | | `zv` | Open folds for this line | - | `zM` | Close all | | `zR` | Open all | - | `zm` | Fold more _(foldlevel += 1)_ | | `zr` | Fold less _(foldlevel -= 1)_ | - | `zx` | Update folds | {.shortcuts} -Uppercase ones are recursive (eg, `zO` is open recursively). -### Navigation -| Shortcut | Description | -| --- | --- | -| `%` | Nearest/matching `{[()]}` | -| `[(` `[{` `[<` | Previous `(` or `{` or `<` | -| `])` | Next | +### Jumping +
-| `[m` | Previous method start | -| `[M` | Previous method end | +| Shortcut | Description | +| --- | --- | +| `]c` | Next difference | +| `[c` | Previous difference | {.shortcuts} -### Case -| Shortcut | Description | -| --- | --- | -| `~` | Toggle case (Case => cASE) | -| `gU` | Uppercase | -| `gu` | Lowercase | -| `gUU` | Uppercase current line (also `gUgU`) | -| `guu` | Lowercase current line (also `gugu`) | + + +Miscellaneous {.cols-3} +------------- + + +### Case {.row-span-2} + +| Shortcut | Description | +|----------------|-------------------------| +| `vU` | _Uppercase_ character | +| `vu` | _Lowercase_ character | +| `~` | _Toggle case_ character | +| `viw` `U` | _Uppercase_ word | +| `viw` `u` | _Lowercase_ word | +| `viw` `~` | _Toggle case_ word | +| `VU` _/_ `gUU` | _Uppercase_ line | +| `Vu` _/_ `guu` | _Lowercase_ line | +| `V~` _/_ `g~~` | _Toggle case_ line | +| `gggUG` | _Uppercase_ all text | +| `ggguG` | _Lowercase_ all text | +| `ggg~G` | _Toggle case_ all text | {.shortcuts} -Do these in visual or normal mode. + + + +### 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` | +| `:noh` | Clear search highlights | +| `:sort` | Sort lines | +| `:ter` | Open a terminal window | +| `:set paste` | Enable Insert Paste sub-mode | +| `: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} ### Counters | Shortcut | Description | | --- | --- | -| `` | Increment number | -| `` | Decrement | +| `` | Increase number | +| `` | Decrease number | {.shortcuts} -### Windows - -| Shortcut | Description | -| --- | --- | -| `z{height}` | Resize pane to `{height}` lines tall | - - -### Text alignment {.row-span-1} - - :center [width] - :right [width] - :left - -See `:help formatting` - - -### Calculator - -| Shortcut | Description | -| --- | --- | -| `=128/2` | Shows the result of the division : '64' | - -Do this in insert mode. - ### Tags {.row-span-2 .col-span-2} | Shortcut | Description | | --- | --- | | `:tag Classname` | Jump to first definition of Classname | - | `` | Jump to definition | | `g]` | See all definitions | -| `` | Go back to last tag | -| ` ` | Back/forward | - +| `` | Go back to last tag | +| ` ` | Back/forward | | `:tselect Classname` | Find definitions of Classname | | `:tjump Classname` | Find definitions of Classname (auto-select 1st) | {.shortcuts} -### Exiting with an error - :cq - :cquit +### Formatting +| - | - | +|-----------------|---| +| `:ce 8` | Center lines between `8` columns | +| `:ri 4` | Right-align lines at `4` columns | +| `:le` | Left-align lines | -Works like `:qa`, but throws an error. Great for aborting Git commands. +See `:help formatting` -### Jumping -| Shortcut | Description | -| --- | --- | -| `` | Go back to previous location | -| `` | Go forward | -| `gf` | Go to file in cursor | -{.shortcuts} - -### Marks {.col-span-2 .row-span-2} +### Marks {.col-span-2 .row-span-4} | Shortcut | Description | | --- | --- | @@ -476,41 +755,57 @@ Works like `:qa`, but throws an error. Great for aborting Git commands. | `:delm abc` | Delete marks `a`, `b`, `c` | {.shortcuts} -### Misc -| Shortcut | Description | -| --- | --- | -| `.` | Repeat last command | -| `]p` | Paste under the current indentation level | -| `:set ff=unix` | Convert Windows line endings to Unix line endings | +### Calculator + +| Shortcut | Description | +| --- | --- | +| `` `=` 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 | +| `` | Insert current word into the command line | +| `"` | Paste from " register | +| `` | Auto-completion of path in insert mode | {.shortcuts} - -### Spell checking {.col-span-3} - -| Shortcut | Description | -| --- | --- | -| `:set spell spelllang=en_us` | Turn on US English spell checking | -| `]s` | Move to next misspelled word after the cursor | -| `[s` | Move to previous misspelled word before the cursor | -| `z=` | Suggest spellings for the word under/after the cursor | -| `zg` | Add word to spell list | -| `zw` | Mark word as bad/mispelling | -| `zu` / `C-X (Insert Mode)` | Suggest words for bad word under cursor from spellfile | -{.shortcuts} - -See `:help spell` +### 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