<<

Arvind Maskara 2/15/16

What is ?

The visual editor initially developed on . The vi editor (“vee eye”) Before vi the primary editor used on Unix was the line editor n User was able to see/edit only one line of the text NOTE: You will not be examined a The vi editor is a , not a text on the detailed usage of vi, but formatter (like MS Word) you should know the basics n You cannot set margins… n Center headings… n Set text as bold… Adapted by Dr. Andrew Vardy from www.wildbill.org/rose/Fall09/ch03.ppt supercomputingchallenge.org/98-99/stts-99/vi.ppt

Vi History Characteristics of vi

Originally written by Bill Joy in 1976. The vi editor is: is Bill Joy? n Very powerful n He co-founded Sun Microsystems in 1982 and served as chief scientist until 2003. n …But cryptic Joy's prowess as a computer The best way to learn vi commands is programmer is legendary, with an oft- to use them told anecdote that he wrote the vi editor So practice… in a weekend. Joy denies this assertion.

vi editor 1 Arvind Maskara 2/15/16

Vim equals Vi Starting vi

Most installations of vi actually use a First, see what version of vi is installed different program called on your system through “man vi”

n Vi Improved vi at the prompt

n http://www.vim.org After pressing enter the

n Charityware – donations accepted to help prompt disappears and you see tilde(~) children in Uganda through the ICCF characters on all the lines n Main author is Bram Moolenaar These tilde characters indicate that the line is blank

vi Window Display Vi is a Modal Editor There are (at least) three modes in vi Line one n Command (a.k.a. normal mode) text Line two n Input mode (a.k.a. insert, replace mode) Line three n Command-line mode (a.k.a. mode) ~ When you start vi by default it is in command mode Null lines ~ You enter the input mode through various commands ~ You the input mode by pressing the to get ~ back to the command mode ~ You can go to command-line mode from command mode ~ Line n EX cmd line with the “:” Command line (vim actually has another mode called “visual mode”)

vi editor 2 Arvind Maskara 2/15/16

How to exit from vi How to exit from vi (comand mode) First go to command mode :q is to exit, if you have not made n press Esc There is no harm in any changes to the file pressing Esc even if you are in :q! is the forced quit, it will discard command mode. Your terminal will the changes and quit just beep and/or or flash if you press Esc in command mode :wq is for save and Exit There are different ways to exit ZZ is for save and Exit (Note this command is when you are in the command uppercase) mode The ! Character forces over writes, etc. :wq!

Vi Modes Moving Around

Shell You can move around only when you are

ZZ or :wq vi filename in command mode Command Mode Arrow keys may work, but are not the i a o standard way of moving the cursor in vi The standard keys for moving are: Input Mode n h - for left

n l - for right n j - for down

n k - for up

vi editor 3 Arvind Maskara 2/15/16

Moving Around

w - to move one word forward b - to move one word backward $ - takes you to the end of line ^ - takes you to the first non-blank character of the line 0 - takes you to the begginning of line

Moving Around Moving Around

) - moves cursor to the next sentence Control-d scrolls the screen down (half } - move the cursor to the beginning of next screen) paragraph Control-u scrolls the screen up (half ( - moves the cursor backward to the screen) beginning of the current sentence Control-f scrolls the screen forward (full { - moves the cursor backward to the screen) beginning of the current paragraph % - moves the cursor to the matching Control-b scrolls the screen backward parentheses (full screen).

vi editor 4 Arvind Maskara 2/15/16

Entering text Entering text

To enter text in vi you should first switch n o - opens a new line after the current one to input mode and goes to insert mode

n To switch to input mode there are several n O - opens a new line before the current different commands one and goes to insert mode

n a - Append mode places the insertion point after the current character

n i - Insert mode places the insertion point before the current character

Editing text The change command

x - deletes the current character The change () commands delete the d - is the delete command but pressing only d text specified then change to input will not delete anything; you need to press a mode. second key cw - Change to end of word n dw - deletes to end of word n - deletes the current line cc - Change the current line n d0 - deletes to beginning of line There are many options There are many more keys to be used with delete command

vi editor 5 Arvind Maskara 2/15/16

Structure of vi commands and repeat command

vi commands can be prefixed by a u - undo the changes made by editing number indicating how many times to commands execute the command . (dot or period) repeats the last edit n command n 10j goes down by 10 lines n For example dd deletes a line, 5dd will delete five lines. This applies to almost all vi commands

Copy, and in vi Indenting Indenting code is crucial for good style! yy - (yank) copy current line to buffer Indent current line: >> nyy - Where n is number of lines Indent 4 lines: 4>> p - Paste the yanked lines from buffer Unindent: << to the line below Unindent 10 lines: 10<< P - Paste the yanked lines from buffer to the line above (the paste commands will also work after the dd or ndd command)

vi editor 6 Arvind Maskara 2/15/16

Ex Commands We have already seen the following: : [ range ] command [ args ... ] n :q exit without saving n The range can be: n :q! exit without saving or prompting wA number for that line (e.g. 7) n :wq is for (save) and exit wA pair of numbers for a range (e.g. 7,10) There are also text processing commands with the following syntax: w$: The last line w%: All lines n : [ range ] command [ args ... ] n The command can be: ws: Search and replace wg: Perform a global action wOthers…

Global Search and Replace General Global Actions :%s/oldstring/newstring/g The g command uses some existing vi This will change oldstring into newstring wherever command, but applies it globally (to the it occurs throughout the entire text: specified range of lines) n % - Means to try and apply this to all lines It has the following form: n s – Stands for “substitution” n :[range]g/pattern/command n g – Means to replace as many times as possible within the line (otherwise there is only replacement per line) e.g. Delete all lines that start with # n :g/^#/d

e.g. Delete all empty lines n :g/^$/d

vi editor 7 Arvind Maskara 2/15/16

Vi References Use a vi Cheat Sheet

The Vi Lovers Home Page http://thomer.com/vi/vi.html

The Editor War http://en.wikipedia.org/wiki/Editor_war

vi editor 8