| 208 | === File Editing === |
| 209 | |
| 210 | The vi editor is installed on almost every Unix. Linux will very often install vim (vi |
| 211 | improved) which is similar. Every system administrator should know vi(m), because it is |
| 212 | an easy tool to solve problems. |
| 213 | |
| 214 | '''vi Commands and Tips''' |
| 215 | {{{ |
| 216 | Open: |
| 217 | |
| 218 | vi filename (fn=filename) |
| 219 | vi -r filename Recover a file from a crashed session |
| 220 | vi + filename Place the cursor on last line of file. |
| 221 | vi +n filename Place the cursor on line "n" of file. |
| 222 | vi +/pat filename Place cursor on line with first occurrence of "pat"tern |
| 223 | |
| 224 | Close: |
| 225 | |
| 226 | :w Write the file to disk. Don't exit. |
| 227 | :w! Write the file to disk even if read/only. |
| 228 | :wq Write the file to disk and exit. |
| 229 | :wq! Write the file to disk even if read/only and quit. |
| 230 | :q Quit the file (only if no changes). |
| 231 | :q! Quit the file even if changes. |
| 232 | |
| 233 | Movement: |
| 234 | |
| 235 | A Move to end of line, change to insert mode. |
| 236 | h Move 1 space backwards (back/left arrow). |
| 237 | j Move down 1 line (down arrow). |
| 238 | k Move up 1 line (up arrow). |
| 239 | l Move 1 space forwards (forward/right arrow) |
| 240 | w Move cursor to start of next word. |
| 241 | W Same as "w". |
| 242 | b Move cursor to start of previous word. |
| 243 | B Same as "b". |
| 244 | :n Go to line number "n" in the file. |
| 245 | |
| 246 | Editing: |
| 247 | |
| 248 | i Enter in to input mode. |
| 249 | o Add a line below cursor and enter in to input mode. |
| 250 | x Delete character (del key in some cases). |
| 251 | D Delete line from right of cursor to end of line. |
| 252 | dd Delete entire line. |
| 253 | u Undo last edit or restore current line. |
| 254 | yy Yank current line. |
| 255 | p Put yanked text before the cursor. |
| 256 | |
| 257 | Searching: |
| 258 | |
| 259 | /pattern Search for "pattern" in the file going forwards. |
| 260 | ?pattern Search for "pattern" in the file going backwards. |
| 261 | n Find the next occurrence of pattern found forwards. |
| 262 | N Find next occurrence of pattern found backwards. |
| 263 | |
| 264 | Copy/Cut and Paste |
| 265 | <NUM>yyp Copy n lines to buffer, paste below cursor |
| 266 | <NUM>yyP Copy n lines to buffer, paste above cursor |
| 267 | <NUM>ddp Cut n lines and copy to buffer, paste below cursor |
| 268 | <NUM>ddP Cut n lines and copy to buffer, paste above cursor |
| 269 | }}} |
| 270 | |
| 271 | ==== Practice Using vi ==== |
| 272 | |
| 273 | '''Remember The vi editor uses "modes"''' |
| 274 | |
| 275 | The easiest thing to do if you get confused in vi is to press the ESCape key a couple of times and start over with what you were doing. Log in to your VM and... |
| 276 | {{{ |
| 277 | $ cd |
| 278 | $ vi temp.txt |
| 279 | }}} |
| 280 | vi wil create the file “temp.txt” for you. Press the "i" key to switch to input mode. |
| 281 | {{{ |
| 282 | Type something like, "VI is great! I think I'll be using vi from now on instead of Word” |
| 283 | Press <ENTER> to add lines. |
| 284 | Type some more text |
| 285 | }}} |
| 286 | Save the file that you are in. To do this do: |
| 287 | |
| 288 | Press the ESCape key for command mode Type “:wq” then hit Enter to save and quit the file (notice the “:” before the “wq”). |
| 289 | |
| 290 | Copy a large file to your home directory so that you can play around with some more vi commands. We'll copy over your /etc/sysctl.conf file for this exercise. To do this do: |
| 291 | {{{ |
| 292 | $ cd |
| 293 | $ cp /etc/sysctl.conf sysctl.conf.bak |
| 294 | }}} |
| 295 | Edit the file, but let's start at the bottom of the file: |
| 296 | {{{ |
| 297 | $ vi + sysctl.conf.bak |
| 298 | }}} |
| 299 | Go to the first line of the file. Notice the colon (“:”) before the “1”. |
| 300 | {{{ |
| 301 | :1 <ENTER> |
| 302 | }}} |
| 303 | Go to line 10, add a new line, and add in some text: |
| 304 | {{{ |
| 305 | :10 <ENTER> |
| 306 | Press the “o” key |
| 307 | }}} |
| 308 | Add the following text: |
| 309 | {{{ |
| 310 | ## |
| 311 | ## A sample comment |
| 312 | ## |
| 313 | }}} |
| 314 | Delete the three lines you just created: |
| 315 | |
| 316 | Move to the first line of new text |
| 317 | Press the ESCape key |
| 318 | Press “dd” to delete a line, repeat until the text is gone |
| 319 | |
| 320 | Save the file, but don’t exit. |
| 321 | {{{ |
| 322 | :w |
| 323 | press <ENTER> |
| 324 | }}} |
| 325 | Practice copying and pasting text. |
| 326 | |
| 327 | Go to line 12, copy 3 lines of text, go to the bottom of the file, place the text there: |
| 328 | {{{ |
| 329 | ESC (go to command mode) |
| 330 | :12 <ENTER> (go to line 12 of the file) |
| 331 | 3yy (“yank” 3 lines of text and place in copy buffer) |
| 332 | G (go to the end of the file) |
| 333 | p (place the contents of the copy buffer here) |
| 334 | }}} |
| 335 | If want to undo this you would type (in command mode): |
| 336 | {{{ |
| 337 | u |
| 338 | }}} |
| 339 | Go to the top of the file, replace all occurrences of “ipv4” with “ipv6”, but prompt for each change: |
| 340 | {{{ |
| 341 | ESC |
| 342 | :1 <ENTER> |
| 343 | :%s/ipv4/ipv6/gc |
| 344 | }}} |
| 345 | Say “yes” or “no” to a few prompts then escape from this mode by pressing ctrl-c and . |
| 346 | |
| 347 | Go to line 1, search for “kernel”, move to the end of the line, add some text: |
| 348 | {{{ |
| 349 | ESC |
| 350 | :1 <ENTER> |
| 351 | /kernel |
| 352 | SHIFT-A |
| 353 | “text here” |
| 354 | ESC |
| 355 | }}} |
| 356 | Now let’s exit from the file and not save the few changes we’ve made. |
| 357 | {{{ |
| 358 | :q! <ENTER> |
| 359 | }}} |
| 360 | |