robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Nathan Zengrefactor(tutor): use inline comments instead of json file #39714dbdd73e846

master
10.3 KiB288 linesraw
1# Welcome to the Neovim Tutorial
2
3# Chapter 2
4
5  Hic Sunt Dracones: if this is your first exposure to vim and you
6  intended to avail yourself of the introductory chapter, kindly type
7  on the command line of the Vim editor
8~~~ cmd
9        :Tutor vim-01-beginner
10~~~
11  Or just open the [first chapter](@tutor:vim-01-beginner) of the tutor at the link.
12
13  The approximate time required to complete this chapter is 8-10 minutes,
14  depending upon how much time is spent with experimentation.
15
16
17# Lesson 2.1.1: MASTERING TEXT OBJECTS
18
19   ** Operate on logical text blocks with precision using text objects **
20
21  1. Practice word operations:
22     - Place cursor on any word in the line below
23     - Type `diw`{normal} to delete INNER word (word without surrounding space)
24     - Type `daw`{normal} to delete A WORD (including trailing whitespace)
25     - Try with other operators: `ciw`{normal} (change), `yiw`{normal} (yank),
26        `gqiw`{normal} (format)
27
28Practice on: "Vim's", (text_object), and 'powerful' words here. [[-1]]
29
30  2. Work with bracketed content:
31     - Put cursor inside any () {} [] <> pair below
32     - Type `di(`{normal} or `dib`{normal} (delete inner bracket)
33     - Type `da(`{normal} or `dab`{normal} (delete around brackets)
34     - Try same with `i"`{normal}/`a"`{normal} for quotes, `it`{normal}/`at`{normal}  for HTML/XML tags
35
36Test cases: {curly}, [square], <angle>, and "quoted" items. [[-1]]
37
38  3. Paragraph and sentence manipulation:
39     - Use `dip`{normal} to delete inner paragraph (cursor anywhere in paragraph)
40     - Use `vap`{normal} to visually select entire paragraph
41     - Try `das`{normal} to delete a sentence (works between .!? punctuation)
42
43  4. Advanced combinations:
44     - `ciwnew<ESC>`{normal} - Change current word to "new"
45     - `ciw"<CTRL-R>-"<ESC>`{normal} - Wrap current word in quotes
46     - `gUit`{normal}        - Uppercase inner HTML tag content
47     - `va"p`{normal}        - Select quoted text and paste over it
48
49Final exercise: (Modify "this" text) by [applying {various} operations]< [[-1]]
50
51
52# Lesson 2.1.2: THE NAMED REGISTERS
53
54** Store two yanked words concurrently and then paste them **
55
56  1. Move the cursor to the line below marked ✓
57
58  2. Navigate to any point on the word 'Edward' and type `"ayiw`{normal}
59
60**MNEMONIC**: *into register(") named (a) (y)ank (i)nner (w)ord*
61
62  3. Navigate forward to the word 'cookie' (`fk`{normal} or `3fc`{normal}
63     or `$2b`{normal} or `/co`{normal} `<ENTER>`{normal}) and type `"byiw`{normal}
64
65  4. Navigate to any point on the word 'Vince' and type `ciw<CTRL-R>a<ESC>`{normal}
66
67**MNEMONIC**: *(c)hange (i)nner (w)ord with <contents of (r)egister> named (a)*
68
69  5. Navigate to any point on the word 'cake' and type `ciw<CTRL-R>b<ESC>`{normal}
70
71a) Edward will henceforth be in charge of the cookie rations [[-1]]
72b) In this capacity, Vince will have sole cake discretionary powers [[b) In this capacity, Edward will have sole cookie discretionary powers]]
73
74NOTE: Delete also works into registers, i.e. `"sdiw`{normal} will delete
75      the word under the cursor into register s.
76
77REFERENCE: [Registers](registers)
78           [Named Registers](quotea)
79           [Motion](text-objects)
80           [CTRL-R](i_CTRL-R)
81
82
83# Lesson 2.1.3: THE EXPRESSION REGISTER
84
85** Insert the results of calculations on the fly **
86
87  1. Move the cursor to the line below marked ✗
88
89  2. Navigate to any point on the supplied number
90
91  3. Type `ciw<CTRL-R>=`{normal}60\*60\*24 `<ENTER>`{normal}
92
93  4. On the next line, enter insert mode and add today's date with 
94     `<CTRL-R>=`{normal}`system('date')`{vim} `<ENTER>`{normal}
95
96NOTE: All calls to system are OS dependent, e.g. on Windows use 
97      `system('date /t')`{vim}   or  `:r!date /t`{vim}
98
99I have forgotten the exact number of seconds in a day, is it 84600? [[I have forgotten the exact number of seconds in a day, is it 86400]]
100Today's date is:  [[-1]]
101
102NOTE: the same can be achieved with `:pu=`{normal}`system('date')`{vim}
103      or, with fewer keystrokes `:r!date`{vim}
104
105REFERENCE: [Expression Register](quote=)
106
107
108# Lesson 2.1.4: THE NUMBERED REGISTERS
109
110** Press `yy`{normal} and `dd`{normal} to witness their effect on the registers **
111
112  1. Move the cursor to the line below marked ✓
113
114  2. Yank the line starting with "0.", then inspect registers with `:reg`{vim} `<ENTER>`{normal}
115
116  3. Delete line 0 with `"cdd`{normal}, then inspect registers
117     (Where do you expect line 0 to be?)
118
119  4. Continue deleting each successive line, inspecting `:reg`{vim} as you go
120
121NOTE: You should notice that old full-line deletions move down the list
122      as new full-line deletions are added
123
124  5. Now (p)aste the following registers in order; c, 7, 4, 8, 2. i.e. `"7p`{normal}
125
1260. This [[-1]]
1279. wobble
1288. secret
1297. is
1306. on
1315. axis
1324. a
1333. war
1342. message
1351. tribute
136
137
138NOTE: Whole line deletions (`dd`{normal}) are much longer lived in the
139      numbered registers than whole line yanks, or deletions involving
140      smaller movements
141
142REFERENCE: [Numbered Registers](quote0)
143
144
145# Lesson 2.1.5: SPECIAL REGISTERS
146
147 ** Use system clipboard and blackhole registers for advanced editing **
148
149 Note: Clipboard use requires a working clipboard provider for the current
150       windowing system on Linux. Check with `:checkhealth vim.provider`{vim}
151       and `:echo has('clipboard_working')`{vim}
152
153  1. Clipboard registers `+`{normal} and `*`{normal} :
154     - `"+y`{normal} - Yank to system clipboard (e.g. `"+yy`{normal} for current line)
155     - `"+p`{normal} - Paste from system clipboard
156     - `"*`{normal} is primary selection on X11 (middle-click), `"+`{normal} is clipboard
157
158Try: "+yy then paste into another application with Ctrl-V or Cmd+V [[-1]]
159
160  2. Blackhole register `_`{normal} discards text:
161     - `"_daw`{normal} - Delete word without saving to any register
162     - Useful when you don't want to overwrite your default `"`{normal} register
163     - Note this is using the "a Word" text object, introduced in a previous
164       lession
165     - `"_dd`{normal}  - Delete line without saving
166     - `"_dap`{normal} - Delete paragraph without saving
167     - Combine with counts: `3"_dw`{normal}
168
169Practice: "_diw on any word to delete it without affecting yank history [[-1]]
170
171  3. Combine with visual selections:
172     - Select text with V then `"+y`{normal}
173     - To paste from clipboard in insert mode: `<CTRL-R>+`{normal}
174     - Try opening another application and paste from clipboard
175
176  4. Remember:
177     - Clipboard registers work across different Vim instances
178     - Clipboard register is not always working
179     - Blackhole prevents accidental register overwrites
180     - Default `"`{normal} register is still available for normal yank/paste
181     - Named registers (a-z) remain private to each Vim session
182
183  5. Clipboard troubleshooting:
184     - Check support with `:echo has('clipboard_working')`{vim}
185     - 1 means available, 0 means not
186     - On Linux, may need xsel, xclip or wl-clipboard
187       (check `:checkhealth vim.provider`{vim} result)
188
189
190# Lesson 2.1.6: THE BEAUTY OF MARKS
191
192** Code monkey arithmetic avoidance **
193
194NOTE: a common conundrum when coding is moving around large chunks of code.
195      The following technique helps avoid number line calculations associated
196      with operations like `"a147d`{normal} or `:945,1091d a`{vim} or even worse
197      using `i<CTRL-R>=`{normal}1091-945 `<ENTER>`{normal} first
198
199  1. Move the cursor to the line below marked ✓
200
201  2. Go to the first line of the function and mark it with `ma`{normal}
202
203NOTE: exact position on line is NOT important!
204
205  3. Navigate to the end of the line and then the end of the code block 
206     with `$%`{normal}
207
208  4. Delete the block into register a with `"ad'a`{normal}
209
210**MNEMONIC**: *into register(") named (a) put the (d)eletion from the cursor to
211          the LINE containing mark(') (a)*
212
213  5. Paste the block between BBB and CCC `"ap`{normal}
214
215NOTE: practice this operation multiple times to become fluent `ma$%"ad'a`{normal}
216
217~~~ cmd
218AAA [[-1]]
219function itGotRealBigRealFast() {
220  if ( somethingIsTrue ) {
221    doIt()
222  }
223  // the taxonomy of our function has changed and it
224  // no longer makes alphabetical sense in its current position
225
226  // imagine hundreds of lines of code
227
228  // naively you could navigate to the start and end and record or
229  // remember each line number
230}
231BBB
232CCC
233~~~
234
235NOTE: marks and registers do not share a namespace, therefore register a is
236      completely independent of mark a. This is not true of registers and
237      macros.
238
239REFERENCE: [Marks](marks)
240           [Mark Motions](mark-motions)  (difference between ' and \`)
241
242
243# Lesson 2.1 SUMMARY
244
245  1. Text objects provide precision editing:
246     - `iw`{normal}/`aw`{normal} - inner/around word
247     - `i[`{normal}/`a[`{normal} - inner/around bracket
248     - `i"`{normal}/`a"`{normal} - inner/around quotes
249     - `it`{normal}/`at`{normal} - inner/around tag
250     - `ip`{normal}/`ap`{normal} - inner/around paragraph
251     - `is`{normal}/`as`{normal} - inner/around sentence
252
253  2. To store (yank, delete) text into, and retrieve (paste) from, a total of
254     26 registers (a-z) 
255  3. Yank a whole word from anywhere within a word: `yiw`{normal}
256  4. Change a whole word from anywhere within a word: `ciw`{normal}
257  5. Insert text directly from registers in insert mode: `<CTRL-R>a`{normal}
258
259  6. Insert the results of simple arithmetic operations:
260     `<CTRL-R>=`{normal}60\*60`<ENTER>`{normal} in insert mode
261  7. Insert the results of system calls:
262     `<CTRL-R>=`{normal}`system('ls -1')`{vim}`<ENTER>`{normal} in insert mode
263
264  8. Inspect registers with `:reg`{vim}
265  9. Learn the final destination of whole line deletions: `dd`{normal} in
266     the numbered registers, i.e. descending from register 1 - 9.  Appreciate
267     that whole line deletions are preserved in the numbered registers longer
268     than any other operation
269 10. Learn the final destination of all yanks in the numbered registers and
270     how ephemeral they are
271
272 11. Place marks from command mode `m[a-zA-Z0-9]`{normal}
273 12. Move line-wise to a mark with `'`{normal}
274
275 13. Special registers:
276     - `"+`{normal}/`"*`{normal} - System clipboard (OS dependent)
277     - `"_`{normal}    - Blackhole (discard deleted/yanked text)
278     - `"=`{normal}    - Expression register
279     - `"-`{normal}    - Small delete register
280
281
282# CONCLUSION
283
284  This concludes chapter two of the Vim Tutor. It is a work in progress.
285
286  This chapter was written by Paul D. Parker and Christian Brabandt.
287
288  Modified for vim-tutor-mode by Restorer.