Computer-Science

Using vi

VI

vi x.c : edit file β€œx.c”

1) mode

2) In the beginning we are at command mode.

처음 viλ₯Ό μ‹€ν–‰ν•˜λ©΄ command mode둜 μ§„ν–‰λœλ‹€.

command mode:

command mode -> insertion mode : i, a, o

command mode -> status-line mode: / or :

insertion mode

now you can type

insertion mode -> command mode: ESC key

status-line mode:

Exersise

1) Make a file with vi.

vi f1

(when you open a file with vi, sometimes you see Open, Edit, … etc. at the bottom of the screen, in that case just type β€œe” in order to start editing)

2) Start insertion with i key and type following.

Hello, this is my first session in the vi editor.

This is the second line of text.

3) Return to command mode with esc key which is located at the top left position of your keyboard. Save and exit with :wq

esc :wq

4) Reopen the file

vi f1

img/img4.png

5) Use j, k, h, l to move the cursor around. Move the cursor to the word β€œfirst”. Use x key to delete the word β€œfirst”. The result should be as follows.

Hello, this is my session in the vi editor.

This is the second line of text.

img/img5.png

6) Insert β€œthird” as follows.

img/img6.png

7) Add a new line as follows. Use o key.

Hello, this is my third session in the vi editor.

Insert a new line here.

This is the second line of text.

img

8) Change the beginning as follows. Use x and i key.

Hi there, this is my third session in the vi editor.

Insert a new line here.

This is the second line of text.

img

9) Add more at the end. Use a key.

Hi there, this is my third session in the vi editor. Adding more at the end.

Insert a new line here.

This is the second line of text.

img

10) Delete the last line. Use dd.

Hi there, this is my third session in the vi editor. Adding more at the end.

Insert a new line here.

img

11) Add few more lines. Use o.

Hi there, this is my third session in the vi editor. Adding more at the end.

Insert a new line here.

One more line.

Another line.

And yet another line.

img

12) Change the last line.

Hi there, this is my third session in the vi editor. Adding more at the end.

Insert a new line here.

One more line.

Another line.

And yet another but last line. And this is the end.

img

13) Copy and paste as follows. Use 2yy to copy two lines at the current cursor; move the cursor to another location and use p to paste them at that location.

Hi there, this is my third session in the vi editor. Adding more at the end.

One more line.

Another line.

Insert a new line here.

One more line.

Another line.

And yet another but last line. And this is the end.

img

14) Go to line 6 with :6 and make change as follows.

Hi there, this is my third session in the vi editor. Adding more at the end.

One more line.

Another line.

Insert a new line here.

One more line.

The 6th line.

And yet another but last line. And this is the end.

img

15) Write a simple c program and compile and run.

Write a program:

$ vi ex1.c

#include <stdio.h>
void main(){
   printf("hi there\n");
}

img

Compile: $ gcc –o ex1 ex1.c

Run:

$ ./ex1

hi there

img