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