cheatsheets/vim.md

474 lines
16 KiB
Markdown
Raw Permalink Normal View History

2015-04-21 05:56:07 +00:00
---
2017-08-24 07:51:01 +00:00
title: Vim
2015-11-24 04:48:01 +00:00
category: Vim
2017-08-28 15:14:30 +00:00
tags: [Featured]
2020-07-05 11:11:36 +00:00
updated: 2020-07-05
2017-08-30 10:44:02 +00:00
weight: -10
2014-06-07 15:07:14 +00:00
---
2012-03-16 06:14:31 +00:00
## Getting started
2017-08-24 07:51:01 +00:00
{: .-three-column}
### Introduction
[Vim](http://www.vim.org/) is a very efficient text editor. This reference was made for Vim 8.0.
For shortcut notation, see `:help key-notation`.
2017-08-24 14:28:58 +00:00
### Exiting
{: .-prime}
| Shortcut | Description |
| -------------- | -------------------------------- |
2023-01-27 10:22:23 +00:00
| `:q` | Close file |
2017-08-24 14:28:58 +00:00
| `:qa` | Close all files |
| --- | --- |
| `:w` | Save |
| `:wq` _/_ `:x` | Save and close file |
| --- | --- |
| `ZZ` | Save and quit |
| `ZQ` | Quit without checking changes |
{: .-shortcuts}
### Exiting insert mode
| Shortcut | Description |
| --- | --- |
| `Esc` _/_ `<C-[>` | Exit insert mode |
| `<C-C>` | Exit insert mode, and abort current command |
{: .-shortcuts}
## Editing
{: .-three-column}
2018-01-05 10:07:23 +00:00
2017-10-11 07:21:43 +00:00
### Editing
2017-08-24 07:51:01 +00:00
| Shortcut | Description |
| --- | --- |
| `a` | Append |
2020-06-23 14:27:25 +00:00
| `A` | Append from end of line |
2017-08-24 07:51:01 +00:00
| `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 |
| --- | --- |
2017-10-11 07:21:43 +00:00
| `r` | Replace one character |
| `R` | Enter Replace mode |
2018-11-07 22:04:50 +00:00
| --- | --- |
| `u` | Undo changes |
| `<C-R>` | Redo changes |
2017-08-24 07:51:01 +00:00
{: .-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 |
2017-08-24 07:51:01 +00:00
{: .-shortcuts}
### Visual mode
| Shortcut | Description |
| --- | --- |
| `v` | Enter visual mode |
| `V` | Enter visual line mode |
| `<C-V>` | Enter visual block mode |
2017-10-11 07:21:43 +00:00
{: .-shortcuts}
#### In visual mode
| Shortcut | Description |
| --- | --- |
2017-08-24 07:51:01 +00:00
| `d` _/_ `x` | Delete selection |
| `s` | Replace selection |
| `y` | Yank selection _(Copy)_ |
{: .-shortcuts}
2012-03-16 06:14:31 +00:00
See [Operators](#operators) for other things you can do.
2023-01-27 10:22:50 +00:00
### Find & Replace
| Shortcut | Description |
| --- | --- |
| :%s/foo/bar/g | Replace foo with bar in whole document |
## Navigating
{: .-three-column}
### Directions
| Shortcut | Description |
| --- | --- |
| `h` `j` `k` `l` | Arrow keys |
| `<C-U>` _/_ `<C-D>` | Half-page up/down |
| `<C-B>` _/_ `<C-F>` | Page up/down |
{: .-shortcuts}
### Words
| 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 |
{: .-shortcuts}
### Character
| `fc` | Go forward to character `c` |
| `Fc` | Go backward to character `c` |
{: .-shortcuts}
### Document
| Shortcut | Description |
| --- | --- |
| `gg` | First line |
| `G` | Last line |
| `:{number}` | Go to line `{number}` |
| `{number}G` | Go to line `{number}` |
| `{number}j` | Go down `{number}` lines |
| `{number}k` | Go up `{number}` lines |
{: .-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}
Operators
---------
{: .-three-column}
### Usage
{: .-prime}
2018-06-14 15:48:42 +00:00
Operators let you operate in a range of text (defined by *motion*). These are performed in normal mode.
{: .-setup}
| `d` | `w` |
| Operator | Motion |
{: .-css-breakdown}
### Operators list
| Shortcut | Description |
| --- | --- |
| `d` | Delete |
| `y` | Yank _(copy)_ |
| `c` | Change _(delete then insert)_ |
| --- | --- |
| `>` | Indent right |
| `<` | Indent left |
| `=` | Autoindent |
| --- | --- |
| `g~` | Swap case |
| `gU` | Uppercase |
| `gu` | Lowercase |
| --- | --- |
| `!` | Filter through external program |
2017-08-31 18:05:08 +00:00
{: .-shortcuts}
See `:help operator`
### Examples
Combine operators with *motions* to use them.
{: .-setup}
| Shortcut | Description |
| --- | --- |
| `d`_d_ | _(repeat the letter)_ 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)_ |
| _(in visual mode)_ `d` | Delete selection |
See: `:help motion.txt`
2014-11-06 08:36:56 +00:00
Text objects
------------
{: .-three-column}
2012-03-16 06:14:31 +00:00
2017-08-24 07:51:01 +00:00
### Usage
{: .-prime}
Text objects let you operate (with an *operator*) in or around text blocks (*objects*).
{: .-setup}
| `v` | `i` | `p` |
| Operator | [i]nside or [a]round | Text object |
{: .-css-breakdown}
### Text objects
| Shortcut | Description |
| --- | --- |
| `p` | Paragraph |
| `w` | Word |
| `s` | Sentence |
| --- | --- |
| `[` `(` `{` `<` | A [], (), or {} block |
| `'` `"` <code>`</code> | A quoted string |
| --- | --- |
| `b` | A block [( |
| `B` | A block in [{ |
| `t` | A XML tag block |
{: .-shortcuts}
### Examples
2017-08-24 07:51:01 +00:00
| Shortcut | Description |
| --- | --- |
| `vip` | Select paragraph |
| `vipipipip` | Select more |
| --- | --- |
| `yip` | Yank inner paragraph |
| `yap` | Yank paragraph (including newline) |
| --- | --- |
| `dip` | Delete inner paragraph |
| `cip` | Change inner paragraph |
2017-08-24 07:51:01 +00:00
{: .-shortcuts}
See [Operators](#operators) for other things you can do.
2015-04-14 19:41:46 +00:00
### Diff
| Shortcut | Description |
| --- | --- |
2020-07-05 10:57:28 +00:00
| `gvimdiff file1 file2 [file3]` | See differences between files, in HMI |
2012-11-15 22:06:20 +00:00
Misc
----
2012-11-15 21:57:56 +00:00
### 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 |
2017-08-24 07:51:01 +00:00
### Folds
| Shortcut | Description |
| --- | --- |
| `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 |
| --- | --- |
| `[m` | Previous method start |
| `[M` | Previous method end |
{: .-shortcuts}
2017-08-24 07:51:01 +00:00
### Jumping
| Shortcut | Description |
| --- | --- |
| `<C-O>` | Go back to previous location |
| `<C-I>` | Go forward |
2017-08-24 07:51:01 +00:00
| --- | --- |
| `gf` | Go to file in cursor |
2017-08-24 07:51:01 +00:00
{: .-shortcuts}
### Counters
| Shortcut | Description |
| --- | --- |
| `<C-A>` | Increment number |
| `<C-X>` | Decrement |
2017-10-11 07:21:43 +00:00
{: .-shortcuts}
2017-08-24 07:51:01 +00:00
### Windows
| `z{height}<Cr>` | Resize pane to `{height}` lines tall |
### Tags
| Shortcut | Description |
| --- | --- |
| `:tag Classname` | Jump to first definition of Classname |
| --- | --- |
| `<C-]>` | Jump to definition |
2017-08-24 07:51:01 +00:00
| `g]` | See all definitions |
| `<C-T>` | Go back to last tag |
| `<C-O> <C-I>` | Back/forward |
2017-08-24 07:51:01 +00:00
| --- | --- |
| `:tselect Classname` | Find definitions of Classname |
| `:tjump Classname` | Find definitions of Classname (auto-select 1st) |
{: .-shortcuts}
### Case
| Shortcut | Description |
| --- | --- |
| `~` | Toggle case (Case => cASE) |
| `gU` | Uppercase |
| `gu` | Lowercase |
2017-08-24 07:51:01 +00:00
| --- | --- |
| `gUU` | Uppercase current line (also `gUgU`) |
| `guu` | Lowercase current line (also `gugu`) |
2017-08-24 07:51:01 +00:00
{: .-shortcuts}
Do these in visual or normal mode.
2017-08-24 07:51:01 +00:00
### Marks
| Shortcut | Description |
| --- | --- |
| <code>`^</code> | Last position of cursor in insert mode |
| <code>`.</code> | Last change in current buffer |
| <code>`"</code> | Last exited current buffer |
| <code>`0</code> | In last file edited |
| <code>''</code> | Back to line in current buffer where jumped from |
| <code>``</code> | Back to position in current buffer where jumped from |
| <code>`[</code> | To beginning of previously changed or yanked text |
| <code>`]</code> | To end of previously changed or yanked text |
| <code>`&lt;</code> | To beginning of last visual selection |
| <code>`&gt;</code> | To end of last visual selection |
| --- | --- |
| `ma` | Mark this cursor position as `a` |
| <code>`a</code> | Jump to the cursor position `a` |
| `'a` | Jump to the beginning of the line with position `a` |
| <code>d'a</code> | Delete from current line to line of mark `a` |
| <code>d`a</code> | Delete from current position to position of mark `a` |
| <code>c'a</code> | Change text from current line to line of `a` |
| <code>y`a</code> | 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` |
2017-08-28 06:45:03 +00:00
{: .-shortcuts}
2017-08-24 07:51:01 +00:00
### Misc
| Shortcut | Description |
| --- | --- |
| `.` | Repeat last command |
| `]p` | Paste under the current indentation level |
2020-02-23 09:32:37 +00:00
| --- | --- |
2020-07-08 15:37:24 +00:00
| `:set ff=unix` | Convert Windows line endings to Unix line endings |
2017-08-28 06:45:03 +00:00
{: .-shortcuts}
2017-08-24 07:51:01 +00:00
### Command line
| Shortcut | Description |
| --- | --- |
| `<C-R><C-W>` | Insert current word into the command line |
| `<C-R>"` | Paste from " register |
| `<C-X><C-F>` | Auto-completion of path in insert mode |
2017-08-28 06:45:03 +00:00
{: .-shortcuts}
2017-08-24 07:51:01 +00:00
### Text alignment
2015-01-16 04:02:16 +00:00
2017-08-24 07:51:01 +00:00
:center [width]
:right [width]
:left
2015-04-14 19:41:46 +00:00
2017-08-24 07:51:01 +00:00
See `:help formatting`
2012-12-15 12:56:59 +00:00
2017-08-24 07:51:01 +00:00
### Calculator
2012-03-16 06:14:31 +00:00
| Shortcut | Description |
| --- | --- |
| `<C-R>=128/2` | Shows the result of the division : '64' |
2015-04-21 05:56:07 +00:00
2017-08-24 07:51:01 +00:00
Do this in insert mode.
2015-04-21 05:56:07 +00:00
2017-09-02 19:44:26 +00:00
### Exiting with an error
2017-09-02 20:02:36 +00:00
:cq
2017-09-02 19:44:26 +00:00
:cquit
Works like `:qa`, but throws an error. Great for aborting Git commands.
### Spell checking
| 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/misspelling |
| `zu` / `C-X (Insert Mode)` | Suggest words for bad word under cursor from spellfile |
{: .-shortcuts}
See `:help spell`
2017-10-11 07:13:00 +00:00
Also see
--------
2013-05-29 12:19:07 +00:00
2017-10-11 07:13:00 +00:00
- [Vim cheatsheet](https://vim.rtorr.com/) _(vim.rotrr.com)_
- [Vim documentation](http://vimdoc.sourceforge.net/htmldoc/) _(vimdoc.sourceforge.net)_
- [Interactive Vim tutorial](http://openvim.com/) _(openvim.com)_