Upload
nikola-stevanovic
View
65
Download
0
Embed Size (px)
Citation preview
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.
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
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
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
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.
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).