Lab 4
Lab 4
LAB MANUAL # 4
(SPRING 2023)
Objective(s) :
To understand working with VIM Editor.
Lab Tasks :
Task 1 : Familiarity with Vi Editor.
Task 2: Compiling and executing a C program in VIM
Task 3 : You can convert temperature from degrees Celsius to degrees Fahrenheit by multiplying
by 9/5 and adding 32. Write a program that allows the user to enter a floating-point
number representing degrees Celsius, and then displays the corresponding degrees
Fahrenheit.
Task 4 : Write and run a program that simulates a simple calculator. It reads two integers and a
remainder is printed.
Total 40 Signature
Note : Attempt all tasks and get them checked by your Instructor
Objective(s):
To understand working with VIM Editor.
Tool(s) used:
Ubuntu, VIM Editor
VI Editor
The Vi editor is a visual editor used to create and edit text, files, documents and programs. It displays
the content of files on the screen and allows a user to add, delete or change part of text. There are three
modes available in the Vi editor, they are
1. Command mode
2. Input (or) insert mode.
This command would open a display screen with 25 lines and with tilt (~) symbol at the
start of each line. The first syntax would save the file in the filename mentioned and for
the next the filename must be mentioned at the end.
Options: vi +n <filename> - this would point at the nth line (cursor pos).
To move the editor from command node to edit mode, you have to press the <ESC> key. For inserting
and replacing the following commands are used.
ESC a Command
This command is used to move the edit mode and start to append after the current
character.
Syntax: <ESC>
ESC A Command
This command is also used to append the file, but this command append at the end of
current line.
Syntax: <ESC> A
ESC i Command
This command is used to insert the text before the current cursor position.
Syntax: <ESC> i
ESC I Command
Syntax: <ESC> I
ESC o Command
This command is insert a blank line below the current line & allow insertion of
contents.
Syntax: <ESC> o
ESC O Command
This command is used to insert a blank line above & allow insertion of contents.
Syntax: <ESC> O
ESC r Command
This command is to replace the particular character with the given characters.
ESC R Command
This command is used to replace the particular text with a given text.
<ESC> S Command
Syntax: <ESC> S
Cursor Movement in Vi
<ESC> h
This command is used to move to the previous character typed. It is used to move to left
of the text. It can also use to move character by character (or) a number of characters.
Syntax:
This command is used to move to the right of the cursor (i.e.) to the next character. It can
also be used to move the cursor for a number of characters.
Syntax:
Syntax:
Syntax:
This command will move the cursor to the starting of next lines or a group of lines
mentioned.
Syntax:
● <ESC> enter
● <ESC> n enter
<ESC> + Command
Syntax:
● <ESC> +
● <ESC> n+
<ESC> - Command
Syntax:
● <ESC> -
● <ESC> n-
<ESC> 0
This command will bring the cursor to the beginning of the same current line.
Syntax: <ESC> 0
<ESC> $
This command will bring the cursor to the end of the current line.
Syntax: <ESC> $
<ESC> ^
Syntax: <ESC> ^
<ESC> b Command
This command is used to move back to the previous word (or) a number of words.
Syntax:
● <ESC>b
● <ESC>nb
<ESC> e Command
This command is used to move towards and replace the cursor at last character of the
Syntax:
● <ESC> e
● <ESC>ne
<ESC> w Command
Syntax:
● <ESC> w
● <ESC> nw
<ESC> x Command
Syntax:
● <ESC> x
● <ESC> nx
<ESC> X Command
Syntax:
● <ESC> X
● <ESC> nX
<ESC> dw Command
This command is to delete a single word or number of words to right of current cursor
DR. AHMAD HASSAN BUTT
DEPARTMENT OF COMPUTER SCIENCE, FCIT-PU, LAHORE
8 | Page
OPERATING SYSTEM LABORATORY MANUAL | CC-217-3L
position.
Syntax:
● <ESC> dw
● <ESC> ndw
db Command
This command is to delete a single word to the left of the current cursor position.
Syntax:
● <ESC> db
● <ESC> ndb
<ESC> dd Command
This command is used to delete the current line (or) a number of lines below the current
line.
Syntax:
● <ESC> dd
● <ESC> ndd
<ESC> d$ Command
This command is used to delete the text from current cursor position to last character of
current line.
Syntax: <ESC> d$
<ESC> w Command
Syntax: <ESC>w
<ESC> q! Command
Syntax: <ESC>:q!
<ESC> wq Command
This command quits the vi editor after saving the text in the mentioned file.
Syntax: <ESC>:wq
<ESC> x Command
Syntax: <ESC>:x
<ESC> q Command
This command would quit the window but it would ask for again to save the file.
Syntax: <ESC>: q
Open a simple text editor Vi, command line code editor. Create a new File – hello.c (.c extension
is used to indicate that it’s a C program). Then write a simple HELLO WORLD program and
save it.
#include <studio.h>
void main(void){
printf(“ Hello World!\n");
}
Compile the program
Install g++ Compiler from Synaptic manager or by writing the following commands in
terminal.
Task 3 You can convert temperature from degrees Celsius to degrees Fahrenheit by multiplying by 9/5
and adding 32. Write a program that allows the user to enter a floating-point number representing
degrees Celsius, and then displays the corresponding degrees Fahrenheit.
Task 4 Write and run a program that simulates a simple calculator. It reads two integers and a character.
If the character is a +, the sum is printed; if it is a -, the difference is printed; if it is a *, the product is
printed; if it is a /, the quotient is printed; and if it is a %, the remainder is printed.