blob: eaeef4003547e41bb34a196ae7fd6cfa702832c1 [file] [log] [blame]
Paul Desmond Parker17c71da2024-11-03 20:47:53 +01001===============================================================================
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= =
5= C h a p t e r - T w o =
6= =
7===============================================================================
8
9 Hic Sunt Dracones: if this is your first exposure to vim and you
10 intended to avail yourself of the introductory chapter, kindly type
11 :q<enter> and try again.
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: THE NAMED REGISTERS
18
19
20 ** Store two yanked words concurrently and then paste them **
21
22 1. Move the cursor to the line below marked --->
23
24 2. Navigate to any point on the word 'Edward' and type "ayiw
25
26MNEMONIC: into register(") named (a) (y)ank (i)nner (w)ord
27
28 3. Navigate forward to the word 'cookie' (fk or 2fc or $2b or /co<enter>)
29 and type "byiw
30
31 4. Navigate to any point on the word 'Vince' and type ciw<C-r>a<ESC>
32
33MNEMONIC: (c)hange (i)nner (w)ord with <contents of (r)egister> named (a)
34
35 5. Navigate to any point on the word 'cake' and type ciw<C-r>b<ESC>
36
37---> a) Edward will henceforth be in charge of the cookie rations
38 b) In this capacity, Vince will have sole cake discretionary powers
39
40NOTE: Delete also works into registers, i.e. "sdiw will delete the word under
41 the cursor into register s.
42
43REFERENCE: Registers :h registers
44 Named Registers :h quotea
45 Motion :h motion.txt<enter> /inner<enter>
46 CTRL-R :h insert<enter> /CTRL-R<enter>
47
48~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49
50 Lesson 2.1.2: THE EXPRESSION REGISTER
51
52
53 ** Insert the results of calculations on the fly **
54
55 1. Move the cursor to the line below marked --->
56
57 2. Navigate to any point on the supplied number
58
59 3. Type ciw<C-r>=60*60*24<enter>
60
61 4. On the next line, enter insert mode and add today's date with
62 <C-r>=system('date')<enter>
63
64NOTE: All calls to system are OS dependent, e.g. on Windows use
65 system('date /t') or :r!date /t
66
67---> I have forgotten the exact number of seconds in a day, is it 84600?
68 Today's date is:
69
70NOTE: the same can be achieved with :pu=system('date')
71 or, with fewer keystrokes :r!date
72
73REFERENCE: Expression Register :h quote=
74
75~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76
77 Lesson 2.1.3: THE NUMBERED REGISTERS
78
79
80 ** Press yy and dd to witness their effect on the registers **
81
82 1. Move the cursor to the line below marked --->
83
84 2. yank the zeroth line, then inspect registers with :reg<enter>
85
86 3. delete line 0. with "cdd, then inspect registers
87 (Where do you expect line 0 to be?)
88
89 4. continue deleting each successive line, inspecting :reg as you go
90
91NOTE: You should notice that old full-line deletions move down the list
92 as new full-line deletions are added
93
94 5. Now (p)aste the following registers in order; c, 7, 4, 8, 2. i.e. "7p
95
96---> 0. This
97 9. wobble
98 8. secret
99 7. is
100 6. on
101 5. axis
102 4. a
103 3. war
104 2. message
105 1. tribute
106
107NOTE: Whole line deletions (dd) are much longer lived in the numbered registers
108 than whole line yanks, or deletions involving smaller movements
109
110REFERENCE: Numbered Registers :h quote0
111
112
113~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114
115 Lesson 2.1.4: THE BEAUTY OF MARKS
116
117
118 ** Code monkey arithmetic avoidance **
119
120NOTE: a common conundrum when coding is moving around large chunks of code.
121 The following technique helps avoid number line calculations associated
122 with operatins like "a147d or :945,1091d a or even worse using
123 i<C-r>=1091-945<enter> first
124
125 1. Move the cursor to the line below marked --->
126
127 2. Go to the first line of the function and mark it with ma
128
129NOTE: exact position on line is NOT important!
130
131 3. Navigate to the end of the line and then the end of the code block
132 with $%
133
134 4. Delete the block into register a with "ad'a
135
136MNEMONIC: into register(") named (a) put the (d)eletion from the cursor to the
137 LINE containing mark(') (a)
138
139 5. Paste the block between BBB and CCC "ap
140
141NOTE: practice this operation multiple times to become fluent ma$%"ad'a
142
143---> AAA
144 function itGotRealBigRealFast() {
145 if ( somethingIsTrue ) {
146 doIt()
147 }
148 // the taxonomy of our function has changed and it
149 // no longer makes alphabetical sense in it's current position
150
151 // imagine hundreds of lines of code
152
153 // naively you could navigate to the start and end and record or
154 // remember each line number
155 }
156 BBB
157 CCC
158
159NOTE: marks and registers do not share a namespace, therefore register a is
160 completely independent of mark a. This is not true of registers and
161 macros.
162
163REFERENCE: Marks :h marks
164 Mark Motions :h mark-motions (difference between ' and `)
165
166~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
167
168 Lesson 2.1 SUMMARY
169
170
171 1. To store (yank, delete) text into, and retrieve (paste) from, a total of
172 26 registers (a-z)
173 2. Yank a whole word from anywhere within a word: yiw
174 3. Change a whole word from anywhere within a word: ciw
175 4. Insert text directly from registers in insert mode: (C-r)a
176
177 5. Insert the results of simple arithmetic operations: (C-r)=60*60<enter>
178 in insert mode
179 6. Insert the results of system calls: (C-r)=system('ls -1')
180 in insert mode
181
182 7. Inspect registers with :reg
183 8. Learn the final destination of whole line deletions: dd in the numbered
184 registers, i.e. descending from register 1 - 9. Appreciate that whole
185 line deletions are preserved in the numbered registers longer than any
186 other operation
187 9. Learn the final destination of all yanks in the numbered registers and
188 how ephemeral they are
189
190 10. Place marks from command mode m[a-zA-Z0-9]
191 11. Move line-wise to a mark with '
192
193~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
194
195 This concludes chapter two of the Vim Tutor. It is a work in progress.
196
197 This chapter was written by Paul D. Parker.
198
199~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~