Debian Codegraph @

runtime/tutor/tutor2

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