Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

Chandigarh College of Engineering and Technology

(Degree Wing)

Operating System Practical (Code:- CS-453)


Practical No:- 7

Submitted By Submitted To
Ravi Verma Dr. Dheerendra Singh
Roll No: CO19352 Professor
CSE 4th Semester
Aim: Learning and working with Vi Editor
Introduction
The VI editor is the most popular and classic text editor in the Linux family. Below, are some
reasons which make it a widely used editor –
1. It is available in almost all Linux Distributions
2. It works the same across different platforms and Distributions
3. It is user-friendly. Hence, millions of Linux users love it and use it for their editing needs

There are advanced versions of the vi editor available, and the most popular one is VIM which is
Vi Improved. Some of the other ones are Elvis, Nvi, Nano, and Vile. It is wise to learn vi because
it is feature-rich and offers endless possibilities to edit a file.
● The vi editor is the most popular and commonly used Unix text editor
● It is usually available in all Linux Distributions.
● It works in two modes, Command and Insert
● Command mode takes the user commands, and the Insert mode is for editing text
● You should know the commands to work on your file easily
● Learning to use this editor can benefit you in creating scripts and editing files.
● It's usually available on all flavors of Unix system.
● Its implementations are very similar across the board.
● It requires very few resources.
● It is more user-friendly than other editors such as the ed or the ex.

Operation modes
1. Command mode
The vi editor opens in this mode, and it only understands commands
In this mode, you can, move the cursor and cut, copy, paste the text
This mode also saves the changes you have made to the file
Commands are case-sensitive. You should use the right letter case.
2. Insert mode
This mode is for inserting text in the file.
You can switch to the Insert mode from the command mode by pressing 'i' on the
keyboard. Once you are in Insert mode, any key would be taken as an input for the file on which
you are currently working. To return to the command mode and save the changes you have made
you need to press the Esc key.

3. Last Line Mode(Escape Mode)


Line Mode is invoked by typing a colon [:], while vi is in Command Mode. The cursor will jump
to the last line of the screen and vi will wait for a command. This mode enables you to perform
tasks such as saving files, executing commands.

Vi Editor Functions
1. Inserting text: - After creating a new file or opening an already existing file, you can
write text in it by first shifting to insert mode by hitting the i key. You will notice that the
vi command itself generally does not appear on the screen. You can type a few lines and
press <return> at the end of each line. For longer lines, vi does not word wrap the text; it
breaks the line at the edge of the screen.
2. Moving cursor across a file: - We can usually move the cursor across text in your files
with the up, down, left and right arrow keys of your keyboard. In case you are using a
remote terminal and the arrow keys fail to behave in the desired manner, you can use the
following keys as substitutes: Move left: h, Move right: l, Move up: k, Move down: j.
3. Selecting text: - We can select a text for copying, cutting and pasting only in command
mode. For selecting text, position the cursor at the left or right side of the text you want to
select and press the v key. You can then move the cursor to select the text.
4. Copying text: - Once you have selected the text, as described above, you can press y to
copy the text and x to cut the text. In order to paste the text, position the cursor at the
desired location and press the p key to paste your copied or cut text. Remember you
should be in the command mode to perform these operations.
5. Deleting text: - In order to delete a character from your file, move the cursor until it is on
the incorrect letter and then type x key. To remove more characters, say three, type 3x.
6. Saving and quitting a file: - In the command mode type :wq and press enter to write the
file to disk and quit vi. The command :w will only write the file to the disk and :q will
quit vi without saving the file. You can also split this command for example, type :w and
press enter to write the file to disk without quitting or type :q to quit vi without saving the
file. When you have made changes to your file and quit without saving your changes, vi
will give you a warning. You can ignore the warning by using the command :q! And
press enter.
7. Switching between modes: -
Then you first create or open a file in Vi, you are in Command mode. Although it seems
that you can type here, you can’t. To write anything, you need to shift to Insert mode by
hitting the i key. In order to shift back to Command mode you simply need to hit the esc
key.
Starting vi editor
1. To launch vi editor in command-line interface
Syntax
vi filename

After executing the command


2. To open vi editor in read mode
Syntax
vi -r filename

After executing the command


Exiting vi editor
1. Save and continue editing the file in vi editor
Syntax
:w

After executing the command


2. Exiting vi editor with saving the file
Syntax
:wq

After executing the command


3. Exit without saving the file
Syntax
:q!

After executing the command


4. Quit the vi editor
Synatx
:q

After executing the command


5. Save as the file with a new name
Syntax
:w filename

After executing the command


6. Save and quit the file in vi editor
Syntax
ZZ

After executing the command


Enter Insert Mode
1. Start typing before the current character
Syntax
i

After executing the command


2. Start typing at the start of the current line
Syntax
I

After executing the command


3. Start typing after the current character
Syntax
a

After executing the command


4. Start typing at the end of current line
Syntax
A

After executing the command


5. Start typing on a new line after the current line
Syntax
o

After executing the command


6. Start typing on a new line before the current line
Syntax
O

After executing the command


Moving in the file
1. To move down
Syntax
j

After executing the command


2. To move up
Syntax
k

After executing the command


3. To move left
Syntax
h

After executing the command


4. To move right
Syntax
l

After executing the command


5. Move one word forward
Syntax
w

After executing the command


6. Move one word backward
Syntax
b

After executing the command


7. Move specified number of words forward
Syntax
<n>w

After executing the command


Deletion in vi editor
1. Delete the current character
Syntax
x

After executing the command


2. Delete the character before the cursor
Syntax
X

After executing the command


3. Replace the current character
Syntax
r

After executing the command


4. Delete the current line
Syntax
dd

After executing the command


5 .Delete the current line from current character to the end of the line
Syntax
D

After executing the command


6. delete from the current line to the end of the file
Syntax
dG

After executing the command


7. Delete one word
Syntax
dw

After executing the command


8. Delete specified number of words
Syntax
<n>dw

After executing the command


Repeat and Undo
1. Undo the last command
Syntax
u

After executing the command


2. Repeat the last command
Syntax
.

After executing the command


Command to cut, copy and paste
1. Delete a line
Syntax
dd

After executing the command


2. (yank yank) copy a line
Syntax
yy
3. Paste after the current line
Syntax
p
After executing the command
4. Paste before the current line
Syntax
P

After executing the command


5. Copy one word
Syntax
yw
After executing the command
6. Paste word
Syntax
pw
After executing the command
Alternatives of Vi Editor
1. Gedit
Gedit is a general-purpose GUI based text editor and is installed by default text editor on Gnome
desktop environment. It is simple to use, highly pluggable and a powerful editor with the
following features:
1. Support for UTF-8
2. Use of configurable font size and colors
3. Highly customizable syntax highlighting
4. Undo and redo functionalities
5. Reverting of files
6. Remote editing of files
7. Search and replace text
8. Clipboard support functionalities and many more

2.Nano Editor
Nano is an easy to use text editor, especially for both new and advanced Linux users. It enhances
usability by providing customizable key binding.
Nano has the following features:
1. Highly customizable key bindings
2. Syntax highlighting
3. Undo and redo options
4. Full line display on the standard output
5. Pager support to read from standard input

3.GNU Emacs
Emacs is a highly extensible and customizable text editor that also offers an interpretation of the
Lisp programming language at its core. Different extensions can be added to support text editing
functionalities.
Emacs has the following features:

1. User documentation and tutorials


2. Syntax highlighting using colors even for plain text.
3. Unicode supports many natural languages.
4. Various extension including mail and news, debugger interface, calendar, and many more

4.Kate/Kwrite
Kate is a feature-rich and highly pluggable text editor that comes with KDesktop Environment
(KDE). The Kate project aims at the development of two main products that are: KatePart and
Kate.
KatePart is an advanced text editor component included in many KDE applications that may
require users to edit text whereas Kate is a multiple document interface (MDI) text, editor.
The following are some of its general features:
1. Extensible through scripting
2. Encoding support such as Unicode mode
3. Text rendering in bi-directional mode
4. Line ending support with auto-detection functionalities

5.Sublime Text Editor


Sublime Text is a powerful cross-platform source code editor with a Python programming
interface. It supports many programming languages and markup languages, and features can be
added by users with plugins, mostly community-built and supported under free-software
licenses.
Advantage
1. Vi is universally available on Unix systems. It has been around so long in a stable form
that it is essentially bug free. Many clones have been written for other kinds of
computers.
2. Vi has many powerful commands that utilize just the alphanumeric keys -- it does not
require special function keys.
3. Vi is a small program that does not require a lot of system memory or CPU time. It works
very fast, even on large files.
4. While vi is not programmable, it has a simple way to let other Unix programs, such as the
sort utility, work on selected portions of your file. This adds the functionality of all those
programs to the editor.
5. Vi is completely terminal device independent. It will work with any kind of terminal.
A system file describes the capabilities and control sequences of each kind of terminal for
vi. All the program needs to know is what type of terminal you have. When you log in, if
pangea cannot figure out what kind of terminal you have, it will prompt you to specify a
terminal type. The most common type is the vt100, which most modern terminals and PC
communications software emulate.

Disadvantages
1. Take time to get to use to it especially for people who haven’t worked on terminal or
linux before.
2. Complex command to write to do some advanced things inside editor
3. Case and spaces sensitive: - sometimes waste a lot of time even if we put a space extra
4. High effort to customize - I like to say that configuring Vim is a never-ending effort.
There’s always something that can be tweaked or improved.
5. Poor support for external tooling - Many plugins depend on optional Python and Lua
features, which may or may not be included in whatever binaries are available in the
machine.
6. Poor Feature Discoverability - Though basic features like syntax checking,
autocompletion, and file management are all available out of the box or with minimal
configuration, this is not obvious. The help is very well written but it’s hard to guess
where can the documentation for these features can be found.
7. Script does not run due to spaces many time so we have to write commands again and
again to enter or exit the vi editor and check the running of program.

Conclusion
We can now insert content into a file, move the file around, remove content and undo it then save
and exit. We can now do basic editing in vi. There are lots of vi cheat sheets available online so
you can take your time in learning all of the various commands and concepts. In this practical,
we explored the interesting world of Linux vi editor. After reviewing basic concepts, we learned
how to search for text.
Vim is a really powerful editor and now you can get your hands dirty with it. Mastering Vim will
really improve your productivity. Using Vim you can solve many complex tasks with few
commands. We can refer following documentation to know more about Vim.

You might also like