8
Using vi

Using vi

Embed Size (px)

Citation preview

Page 1: Using vi

Using vi

Page 2: Using vi

2 Modes

Command Modevi starts in command mode. In command mode, everything you type is a command to do something, like:• move the cursor around• save, quit, open a new file• find and replace• copy and paste, etc.

Insert ModeWhile in insert mode, everything you type is inserted into the document you’re editing.

Page 3: Using vi

Command Mode Insert Mode

i - ...at the current cursor positionI - ...at the beginning of the current linea - ...at the next characterA - ...at the end of the current lineo - ...below the current lineO - ...above the current line... plus lots more ways to get into insert mode

EscCtrl-[Ctrl-c

Get into insert mode:

Get into command mode:

You switch between these two modes a lot

as you’re editing

Page 4: Using vi

Editing in command mode

• h j k l – Move cursor left, down, up, right• x – Delete a character• 5x – Delete 5 characters• dd – Delete a line• 5dd – Delete 5 lines• dj – Delete the line below the cursor• dk – Delete the line above the cursor• u - Undo

Page 5: Using vi

Copy and Paste

Every time you delete something in vi, it gets stored in a buffer that can then be pasted into the file with p or P.

To put something into the copy buffer without deleting it, you “yank” it using a variation of the y command.• yy – Yank the current line into the copy buffer

Page 6: Using vi

Search and Replace

• /search_string<Enter> - Search for “search_string” in the document. Type n to jump to the next search result if there’s more than 1

• :%s/aaa/a/g<Enter> - Replace all occurrences of “aaa” with “a”.

Search and replace commands use a regex language that supports wildcards, etc.

Page 7: Using vi
Page 8: Using vi

Saving, quitting, and opening files

• :w, :wq, :q, :q! – Save, save-quit, quit. Add a ! to any of these commands to force-save or force-quit.

• :e file.js – Start editing a new file• :sp file.js, :vsp file.js – Open a

new file in a split window, horizontally or vertically. To move between windows, type Ctrl-w then a direction key (h,j,k,l).