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