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