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