<p>Let's continue our rapid command-covering pace. In command-mode, you can jump to a particularline by typing <c>G</c>. To jump to the first line of a file, type <c>1G</c>. Note that <c>G</c> is capitalized.</p>

243

<p>If you want to jump to the next occurence of a particular text pattern, type <c>/&lt;regexp&gt;</c> and hit <c>enter</c>. Replace

244

&lt;regexp&gt; with the regular expression you're looking for. If you don't know how to use regular expressions, don't

245

fret -- typing <c>/foo</c> will move to the next occurence of <i>foo</i>. The only thing you'll need to watch out for is when

246

you want to refer to the literal <i>^</i>, <i>.</i>, <i>$</i> or <i>\</i> characters. Prefix these characters with a backslash (<i>\</i>), and you'll

247

be set. For example, <c>/foo\.gif</c> will search for the next occurence of "<i>foo.gif</i>".</p>

commands out in your very own vi editor. You can also type <c>//</c> to repeat the last search.</p>

250

251

</body>

252

</section>

253

</chapter>

254

<chapter>

255

<title>Saving and Editing</title>

256

<section>

257

<title>Save and save as...</title>

258

<body>

259

<p>We've covered how you can use the <i>ex</i> command <c>:q</c> to quit from vi. If you want to save your

260

changes, type <c>:w</c>. If you want to save your changes to another file, type <c>:w filename.txt</c> to

261

save as <e>filename.txt</e>. If you want to save and quit, type <c>:x</c> or <c>:wq</c>.</p>

262

<p>In vim (and other advanced vi editors, like elvis)<c>:w</c>, you can have multiple buffers open at once. To open a file into a new window, type <c>:sp filename.txt</c>. <e>filename.txt</e> will appear open for editing in a new split window. To switch between windows, type <c>&lt;CTR&gt;w&lt;CTR&gt;w</c> (control-w twice). Any <c>:q</c>, <c>:q!</c>, <c>:w</c> and <c>:x</c> commands that you enter will only be applied to the currently-active window.</p>

263

</body>

264

</section>

265

266

<section>

267

<title>Simple edits</title>

268

<body>

269

<p>Now, it's time to start learning some of the simple editing commands. The commands that

270

we'll cover here are considered "simple" because the commands keep you in command mode. The

271

more complex editing commands automatically put you into insert mode -- a mode that allows

272

you to enter literal data from the keyboard. We'll cover those in a bit.</p>

273

<p>For now, try moving over some characters and hitting <c>x</c> repeatedly. You'll see that <c>x</c>

274

will delete the current character under the cursor. Now, move to the middle of the paragraph

275

somewhere in your text file, and hit <c>J</c> (capitalized). You'll see that the <c>J</c> command tells

276

vi to join the next line to the end of the current line. Now, move over a character and hit

277

<c>r</c>, and then type in a new character; you'll see that the original character has been replaced.

278

Finally, move to any line in the file and

279

type <c>dd</c>. You'll see that <c>dd</c> deletes the current line of text.</p>

280

281

</body>

282

</section>

283

<section>

284

<title>Repeating and deleting</title>

285

<body>

286

<p>You can repeat any editing command by hitting the <c>.</c> key. If you experiment, you'll see that

287

typing <c>dd...</c> will delete 4 lines, and <c>J......</c> will join four lines. As usual, vi provides with

288

another handy shortcut.

289

</p>

290

<p>To delete text, you can also use the <c>d</c> command combined with any movement command. For example,

291

<c>dw</c> will delete from the current position to the beginning of the next word; <c>d)</c> will delete up until the

292

end of the next sentence, and <c>d}</c> will delete the remainder of the paragraph. Experiment with the

293

<c>d</c> command and the other editing commands until you're comfortable with them.</p>

294

</body>

295

</section>

296

<section>

297

<title>Undo!</title>

298

<body>

299

<p>Now that we're experimenting with deletion, it would be a good time to learn how to undo any changes.

300

By pressing <c>u</c>, the original version of vi allowed you to undo the last edit only. However, modern versions

301

of vi like vim will allow you to repeatedly press <c>u</c> to continue to undo changes to your file. Try combining

302

some <c>d</c> and <c>u</c> commands together.

303

</p>

304

</body>

305

</section>

306

307

<section>

308

<title>Updating the cheat sheet</title>

309

<body>

310

<p>Time to update the cheat sheet! After adding all the commands we've covered so far, your

<li><urilink="http://www.vim.org/">The vim homepage</uri> is the place to go for all your vim needs.</li>

499

<li>If you're looking for a good, old-fashioned book, <urilink="http://www.oreilly.com/catalog/vi6/">Learning the vi Editor, 6th Edition</uri> would be an excellent choice. Contains good coverage of vi and vi clones.</li>