OSDN Git Service

Regular updates
[twpd/master.git] / vim.md
1 ---
2 title: Vim
3 category: Vim
4 layout: 2017/sheet
5 tags: [Featured]
6 updated: 2020-07-05
7 weight: -10
8 intro: |
9  [Vim](http://www.vim.org/) is a very efficient text editor. This reference was made for Vim 8.0.   
10  For shortcut notation, see `:help key-notation`.
11 ---
12
13 Getting started
14 ---------------
15 {: .-three-column}
16
17 ### Exiting
18 {: .-prime}
19
20 | Shortcut       | Description                      |
21 | -------------- | -------------------------------- |
22 | `:q`           | Close file                       |
23 | `:qa`          | Close all files                  |
24 | ---            | ---                              |
25 | `:w`           | Save                             |
26 | `:wq` _/_ `:x` | Save and close file              |
27 | ---            | ---                              |
28 | `ZZ`           | Save and quit                    |
29 | `ZQ`           | Quit without checking changes    |
30 {: .-shortcuts}
31
32 ### Navigating
33
34 | Shortcut            | Description       |
35 | ---                 | ---               |
36 | `h` `j` `k` `l`     | Arrow keys        |
37 | `<C-U>` _/_ `<C-D>` | Half-page up/down |
38 | `<C-B>` _/_ `<C-F>` | Page up/down      |
39 {: .-shortcuts}
40
41 #### Words
42
43 | Shortcut     | Description               |
44 | ---          | ---                       |
45 | `b` _/_ `w`  | Previous/next word        |
46 | `ge` _/_ `e` | Previous/next end of word |
47 {: .-shortcuts}
48
49 #### Line
50
51 | Shortcut     | Description                        |
52 | ---          | ---                                |
53 | `0` _(zero)_ | Start of line                      |
54 | `^`          | Start of line _(after whitespace)_ |
55 | `$`          | End of line                        |
56 {: .-shortcuts}
57
58 #### Character
59
60 | `fc`  | Go forward to character `c`  |
61 | `Fc`  | Go backward to character `c` |
62 {: .-shortcuts}
63
64 #### Document
65
66 | Shortcut    | Description              |
67 | ---         | ---                      |
68 | `gg`        | First line               |
69 | `G`         | Last line                |
70 | `:{number}` | Go to line `{number}`    |
71 | `{number}G` | Go to line `{number}`    |
72 | `{number}j` | Go down `{number}` lines |
73 | `{number}k` | Go up `{number}` lines   |
74 {: .-shortcuts}
75
76 #### Window
77
78 | Shortcut | Description              |
79 | ---      | ---                      |
80 | `zz`     | Center this line         |
81 | `zt`     | Top this line            |
82 | `zb`     | Bottom this line         |
83 | `H`      | Move to top of screen    |
84 | `M`      | Move to middle of screen |
85 | `L`      | Move to bottom of screen |
86 {: .-shortcuts}
87
88 #### Search
89
90 | Shortcut  | Description                         |
91 | ---       | ---                                 |
92 | `n`       | Next matching search pattern        |
93 | `N`       | Previous match                      |
94 | `*`       | Next whole word under cursor        |
95 | `#`       | Previous whole word under cursor    |
96 {: .-shortcuts}
97
98 #### Tab pages
99
100 | Shortcut              | Description                     |
101 | ---                   | ---                             |
102 | `:tabedit [file]`     | Edit file in a new tab          |
103 | `:tabfind [file]`     | Open file if exists in new tab  |
104 | `:tabclose`           | Close current tab               |
105 | `:tabs`               | List all tabs                   | 
106 | `:tabfirst`           | Go to first tab                 |
107 | `:tablast`            | Go to last tab                  |
108 | `:tabn    `           | Go to next tab                  |
109 | `:tabp    `           | Go to previous tab              |
110
111 ### Editing
112
113 | Shortcut | Description                         |
114 | ---      | ---                                 |
115 | `a`      | Append                              |
116 | `A`      | Append from end of line             |
117 | `i`      | Insert                              |
118 | `o`      | Next line                           |
119 | `O`      | Previous line                       |
120 | ---      | ---                                 |
121 | `s`      | Delete char and insert              |
122 | `S`      | Delete line and insert              |
123 | `C`      | Delete until end of line and insert |
124 | ---      | ---                                 |
125 | `r`      | Replace one character               |
126 | `R`      | Enter Replace mode                  |
127 | ---      | ---                                 |
128 | `u`      | Undo changes                        |
129 | `<C-R>`  | Redo changes                        |
130 {: .-shortcuts}
131
132 ### Exiting insert mode
133
134 | Shortcut          | Description |
135 | ---               | ---         |
136 | `Esc` _/_ `<C-[>` | Exit insert mode |
137 | `<C-C>`           | Exit insert mode, and abort current command |
138 {: .-shortcuts}
139
140 ### Clipboard
141
142 | Shortcut        | Description                 |
143 | ---             | ---                         |
144 | `x`             | Delete character            |
145 | ---             | ---                         |
146 | `dd`            | Delete line _(Cut)_         |
147 | `yy`            | Yank line _(Copy)_          |
148 | ---             | ---                         |
149 | `p`             | Paste                       |
150 | `P`             | Paste before                |
151 | ---             | ---                         |
152 | `"*p` _/_ `"+p` | Paste from system clipboard |
153 | `"*y` _/_ `"+y` | Paste to system clipboard   |
154 {: .-shortcuts}
155
156 ### Visual mode
157
158 | Shortcut | Description             |
159 | ---      | ---                     |
160 | `v`      | Enter visual mode       |
161 | `V`      | Enter visual line mode  |
162 | `<C-V>`  | Enter visual block mode |
163 {: .-shortcuts}
164
165 #### In visual mode
166
167 | Shortcut    | Description             |
168 | ---         | ---                     |
169 | `d` _/_ `x` | Delete selection        |
170 | `s`         | Replace selection       |
171 | `y`         | Yank selection _(Copy)_ |
172 {: .-shortcuts}
173
174 See [Operators](#operators) for other things you can do.
175
176 ### Find & Replace
177
178 | Shortcut      | Description                            |
179 | ---           | ---                                    |
180 | :%s/foo/bar/g | Replace foo with bar in whole document |
181
182 Operators
183 ---------
184 {: .-three-column}
185
186 ### Usage
187 {: .-prime}
188
189 Operators let you operate in a range of text (defined by *motion*). These are performed in normal mode.
190 {: .-setup}
191
192 | `d`      | `w`    |
193 | Operator | Motion |
194 {: .-css-breakdown}
195
196 ### Operators list
197
198 | Shortcut | Description                     |
199 | ---      | ---                             |
200 | `d`      | Delete                          |
201 | `y`      | Yank _(copy)_                   |
202 | `c`      | Change _(delete then insert)_   |
203 | ---      | ---                             |
204 | `>`      | Indent right                    |
205 | `<`      | Indent left                     |
206 | `=`      | Autoindent                      |
207 | ---      | ---                             |
208 | `g~`     | Swap case                       |
209 | `gU`     | Uppercase                       |
210 | `gu`     | Lowercase                       |
211 | ---      | ---                             |
212 | `!`      | Filter through external program |
213 {: .-shortcuts}
214
215 See `:help operator`
216
217 ### Examples
218
219 Combine operators with *motions* to use them.
220 {: .-setup}
221
222 | Shortcut               | Description                               |
223 | ---                    | ---                                       |
224 | `d`_d_                 | _(repeat the letter)_ Delete current line |
225 | `d`_w_                 | Delete to next word                       |
226 | `d`_b_                 | Delete to beginning of word               |
227 | _2_`dd`                | Delete 2 lines                            |
228 | `d`_ip_                | Delete a text object _(inside paragraph)_ |
229 | _(in visual mode)_ `d` | Delete selection                          |
230
231 See: `:help motion.txt`
232
233 Text objects
234 ------------
235 {: .-three-column}
236
237 ### Usage
238 {: .-prime}
239
240 Text objects let you operate (with an *operator*) in or around text blocks (*objects*).
241 {: .-setup}
242
243 | `v`      | `i`                  | `p`         |
244 | Operator | [i]nside or [a]round | Text object |
245 {: .-css-breakdown}
246
247 ### Text objects
248
249 | Shortcut               | Description           |
250 | ---                    | ---                   |
251 | `p`                    | Paragraph             |
252 | `w`                    | Word                  |
253 | `s`                    | Sentence              |
254 | ---                    | ---                   |
255 | `[` `(` `{` `<`        | A [], (), or {} block |
256 | `'` `"` <code>`</code> | A quoted string       |
257 | ---                    | ---                   |
258 | `b`                    | A block [(            |
259 | `B`                    | A block in [{         |
260 | `t`                    | A XML tag block       |
261 {: .-shortcuts}
262
263 ### Examples
264
265 | Shortcut    | Description                        |
266 | ---         | ---                                |
267 | `vip`       | Select paragraph                   |
268 | `vipipipip` | Select more                        |
269 | ---         | ---                                |
270 | `yip`       | Yank inner paragraph               |
271 | `yap`       | Yank paragraph (including newline) |
272 | ---         | ---                                |
273 | `dip`       | Delete inner paragraph             |
274 | `cip`       | Change inner paragraph             |
275 {: .-shortcuts}
276
277 See [Operators](#operators) for other things you can do.
278
279 ### Diff
280  
281 | Shortcut                             | Description                              |
282 | ---                                  | ---                                      |
283 | `gvimdiff file1 file2 [file3]`       | See differences between files, in HMI    |
284  
285
286 Misc
287 ----
288
289 ### Folds
290
291 | Shortcut      | Description                  |
292 | ---           | ---                          |
293 | `zo` _/_ `zO` | Open                         |
294 | `zc` _/_ `zC` | Close                        |
295 | `za` _/_ `zA` | Toggle                       |
296 | ---           | ---                          |
297 | `zv`          | Open folds for this line     |
298 | ---           | ---                          |
299 | `zM`          | Close all                    |
300 | `zR`          | Open all                     |
301 | ---           | ---                          |
302 | `zm`          | Fold more _(foldlevel += 1)_ |
303 | `zr`          | Fold less _(foldlevel -= 1)_ |
304 | ---           | ---                          |
305 | `zx`          | Update folds                 |
306 {: .-shortcuts}
307
308 Uppercase ones are recursive (eg, `zO` is open recursively).
309
310 ### Navigation
311
312 | Shortcut            | Description                |
313 | ---                 | ---                        |
314 | `%`                 | Nearest/matching `{[()]}`  |
315 | `[(` `[{` `[<`      | Previous `(` or `{` or `<` |
316 | `])`                | Next                       |
317 | ---                 | ---                        |
318 | `[m`                | Previous method start      |
319 | `[M`                | Previous method end        |
320 {: .-shortcuts}
321
322 ### Jumping
323
324 | Shortcut | Description                  |
325 | ---      | ---                          |
326 | `<C-O>`  | Go back to previous location |
327 | `<C-I>`  | Go forward                   |
328 | ---      | ---                          |
329 | `gf`     | Go to file in cursor         |
330 {: .-shortcuts}
331
332 ### Counters
333
334 | Shortcut | Description      |
335 | ---      | ---              |
336 | `<C-A>`  | Increment number |
337 | `<C-X>`  | Decrement        |
338 {: .-shortcuts}
339
340 ### Windows
341
342 | `z{height}<Cr>` | Resize pane to `{height}` lines tall |
343
344 ### Tags
345
346 | Shortcut              | Description                                     |
347 | ---                   | ---                                             |
348 | `:tag Classname`      | Jump to first definition of Classname           |
349 | ---                   | ---                                             |
350 | `<C-]>`               | Jump to definition                              |
351 | `g]`                  | See all definitions                             |
352 | `<C-T>`               | Go back to last tag                             |
353 | `<C-O> <C-I>`         | Back/forward                                    |
354 | ---                   | ---                                             |
355 | `:tselect Classname`  | Find definitions of Classname                   |
356 | `:tjump Classname`    | Find definitions of Classname (auto-select 1st) |
357 {: .-shortcuts}
358
359 ### Case
360
361 | Shortcut | Description                          |
362 | ---      | ---                                  |
363 | `~`      | Toggle case (Case => cASE)           |
364 | `gU`     | Uppercase                            |
365 | `gu`     | Lowercase                            |
366 | ---      | ---                                  |
367 | `gUU`    | Uppercase current line (also `gUgU`) |
368 | `guu`    | Lowercase current line (also `gugu`) |
369 {: .-shortcuts}
370
371 Do these in visual or normal mode.
372
373 ### Marks
374
375 | Shortcut           | Description                                          |
376 | ---                | ---                                                  |
377 | <code>`^</code>    | Last position of cursor in insert mode               |
378 | <code>`.</code>    | Last change in current buffer                        |
379 | <code>`"</code>    | Last exited current buffer                           |
380 | <code>`0</code>    | In last file edited                                  |
381 | <code>''</code>    | Back to line in current buffer where jumped from     |
382 | <code>``</code>    | Back to position in current buffer where jumped from |
383 | <code>`[</code>    | To beginning of previously changed or yanked text    |
384 | <code>`]</code>    | To end of previously changed or yanked text          |
385 | <code>`&lt;</code> | To beginning of last visual selection                |
386 | <code>`&gt;</code> | To end of last visual selection                      |
387 | ---                | ---                                                  |
388 | `ma`               | Mark this cursor position as `a`                     |
389 | <code>`a</code>    | Jump to the cursor position `a`                      |
390 | `'a`               | Jump to the beginning of the line with position `a`  |
391 | <code>d'a</code>   | Delete from current line to line of mark `a`         |
392 | <code>d`a</code>   | Delete from current position to position of mark `a` |
393 | <code>c'a</code>   | Change text from current line to line of `a`         |
394 | <code>y`a</code>   | Yank text from current position to position of `a`   |
395 | ---                | ---                                                  |
396 | `:marks`           | List all current marks                               |
397 | `:delm a`          | Delete mark `a`                                      |
398 | `:delm a-d`        | Delete marks `a`, `b`, `c`, `d`                      |
399 | `:delm abc`        | Delete marks `a`, `b`, `c`                           |
400 {: .-shortcuts}
401
402 ### Misc
403
404 | Shortcut   | Description                                       |
405 | ---        | ---                                               |
406 | `.`        | Repeat last command                               |
407 | `]p`       | Paste under the current indentation level         |
408 | ---        | ---                                               |
409 | `:set ff=unix` | Convert Windows line endings to Unix line endings |
410 {: .-shortcuts}
411
412 ### Command line
413
414 | Shortcut     | Description                               |
415 | ---          | ---                                       |
416 | `<C-R><C-W>` | Insert current word into the command line |
417 | `<C-R>"`     | Paste from " register                     |
418 | `<C-X><C-F>` | Auto-completion of path in insert mode    |
419 {: .-shortcuts}
420
421 ### Text alignment
422
423     :center [width]
424     :right [width]
425     :left
426
427 See `:help formatting`
428
429 ### Calculator
430
431 | Shortcut      | Description                               |
432 | ---           | ---                                       |
433 | `<C-R>=128/2` | Shows the result of the division : '64'   |
434
435 Do this in insert mode.
436
437 ### Exiting with an error
438
439     :cq
440     :cquit
441
442 Works like `:qa`, but throws an error. Great for aborting Git commands.
443
444
445 ### Spell checking
446
447 | Shortcut                     | Description                                             |
448 | ---                          | ---                                                     |
449 | `:set spell spelllang=en_us` | Turn on US English spell checking                       |
450 | `]s`                         | Move to next misspelled word after the cursor           |
451 | `[s`                         | Move to previous misspelled word before the cursor      |
452 | `z=`                         | Suggest spellings for the word under/after the cursor   |
453 | `zg`                         | Add word to spell list                                  |
454 | `zw`                         | Mark word as bad/mispelling                             |
455 | `zu` / `C-X (Insert Mode)`   | Suggest words for bad word under cursor from spellfile  |
456 {: .-shortcuts}
457
458 See `:help spell`
459
460
461 Also see
462 --------
463
464 - [Vim cheatsheet](https://vim.rtorr.com/) _(vim.rotrr.com)_
465 - [Vim documentation](http://vimdoc.sourceforge.net/htmldoc/) _(vimdoc.sourceforge.net)_
466 - [Interactive Vim tutorial](http://openvim.com/) _(openvim.com)_