<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>

242

<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

243

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

244

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

245

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

246

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>

249

250

</body>

251

</section>

252

</chapter>

253

<chapter>

254

<title>Saving and Editing</title>

255

<section>

256

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

257

<body>

258

<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

259

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

260

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

261

<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>

262

</body>

263

</section>

264

265

<section>

266

<title>Simple edits</title>

267

<body>

268

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

269

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

270

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

271

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

272

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

273

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

274

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

275

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

276

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

277

Finally, move to any line in the file and

278

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

279

280

</body>

281

</section>

282

<section>

283

<title>Repeating and deleting</title>

284

<body>

285

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

286

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

287

another handy shortcut.

288

</p>

289

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

290

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

291

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

292

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

293

</body>

294

</section>

295

<section>

296

<title>Undo!</title>

297

<body>

298

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

299

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

300

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

301

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

302

</p>

303

</body>

304

</section>

305

306

<section>

307

<title>Updating the cheat sheet</title>

308

<body>

309

<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>

498

<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>