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