WEEK 1 Objective
WEEK 1 Objective
Suggested Experiments/Activities:
1Q: Basic Linux environment and its editors like Vi, Vim & Emacs etc
For Linux users, text editors are essential tools that play a crucial role in various tasks, from
coding and writing to system administration and configuration management. Linux offers a
wide range of text editors, catering to different preferences and use cases. In this article, we
will delve into the world of Linux text editors, exploring their features, strengths, and popular
choices among the community.
Vi Text Editor
Vim Editor
Nano Editor
Kate Editor
Sublime Editor
Atom Editor
Emacs Editor
Vi Text Editor:
Vi is an old but popular text editor for Linux and other Unix systems. It works differently than most
modern text editors. Instead of just typing directly, Vi has different “modes” for different tasks. One
mode is for moving the cursor around and making edits. Another mode is for inserting new text.
There are also modes for running commands. While Vi can be tricky to learn at first with all its modes
and keyboard shortcuts, many experienced programmers love using Vi because it allows them to edit
files very quickly and efficiently without using a mouse or menus once they get the hang of it. Vi is
extremely lightweight and available on virtually every Unix system, making it a reliable choice
Basic vi Commands
Text editors are essential tools for any programmer or computer enthusiast. One popular text editor
that has been around for decades and is still widely used is vi (pronounced “vee-eye”). vi is a
terminal-based text editor that offers powerful editing capabilities once you understand its basic
commands.
In this article, we will explore the fundamental vi commands that will help you navigate, edit, and
save your text files efficiently. Whether you’re a beginner or an experienced user looking to refresh
your knowledge, this guide will provide you with a solid foundation in using vi.
VI COMMANDS
Below is a list of some of the most used VI commands. To start using vi, open the terminal and type
the below line:
vi filename
Here “filename” is the name of the file that you want to edit. Suppose the file is GeeksForGeeks. The
command will look something like this :
vi GeeksForGeeks.txt
1. Normal
2. Insert Mode
3. Visual Mode
Normal Mode:
Normal Mode is the default mode when you open a file. In this mode, you can navigate and perform
various operations on text.
Insert Mode:
To enter Insert Mode and start typing or editing text, press i (for insert before the cursor), I (for insert
at the beginning of the line), a (for insert after the cursor), or A (for insert at the end of the line).
Visual Mode
Visual Mode allows you to select text. You can enter Visual Mode by pressing v.
Navigation in VI:
In Normal Mode, you can move the cursor using the arrow keys or the h, j, k, and l keys, which
correspond to left, down, up, and right, respectively.
# In Normal Mode, '0' takes you to the start, and '$' to the end.
This is the start of the line. $ This is the end of the line.
This is line 1.
This is line 2.
This is line 3.
Editing Text
1. Inserting Text:
2. Deleting Text:
In Normal Mode, use x to delete the character under the cursor, dd to delete the entire line, or D to
delete from the cursor position to the end of the line.
# 'D' deletes from the cursor position to the end of the line.
To copy text, use yy. To paste, press p to paste below the cursor or P to paste above the cursor.
Saving:
To save your changes, press : in Normal Mode, followed by w and then press Enter. Alternatively, you
can use :w filename to save the file with a different name.
:w
:w newfilename.txt
Quitting:
To exit vi, type :q and press Enter. If you’ve made changes and want to save them while quitting,
use :wq. If you want to quit without saving changes, use :q!.
# In Normal Mode, ':q' quits vi.
:q
:wq
:q!
Searching
In Normal Mode, press / followed by the text you want to search for. Press Enter to find the next
occurrence, or n to repeat the search in the forward direction.
/SearchText
Undoing:
Redoing:
Vim Editor
Vim is a popular text editor program used on Linux and other Unix operating systems. It is an
improved and updated version of the old vi editor. While vim looks basic, it is actually a very
powerful tool for editing files efficiently, especially for programmers and developers. What makes
vim unique is that it has different “modes” for different tasks like navigating files, editing text, and
running commands. This modal approach with keyboard shortcuts allows very fast and precise text
editing once you learn it. Though vim has a learning curve at first, many Linux users prefer it over
regular text editors because it provides more control and capabilities through its modes and key
combinations. Vim also supports adding extra features through plugins.
In this article, we will cover some basic commands in the Vim editor. First, we look at what Linux and
VIM editors are and why we use them, followed by basic commands in Vim editor starting from
creating/editing a file, different modes, quitting the editor, saving the changes to the file, navigation
in the editor, deleting lines, displaying and hiding line numbers, search and replace, and syntax
highlighting along with the syntax of commands and screenshots.
Linux
Linux was developed by Linus Torvalds in 1991 as a hobby project. It is an open-source (source code
that can be used by anyone freely) kernel that is most popular and widely used in the industry as well
as in personal systems. There are various operating systems based on the Linux kernel, some of the
popular Linux distributions are Ubuntu, CentOS, Red Hat, Debian, and Kali Linux.
VIM Editor
Vi Editor is a widely used text editor in Unix/Linux systems and is known for its efficiency and
flexibility. Vi editor was developed in 1976 by Bill Joy and later in 1991, an improved version of Vi
editor was released which is known as VI Improved (VIM).
1. Create/Edit a file
6. Delete a line
1. Create/Edit a file
Syntax:
vim filename
Example:
vim new.txt
Replace [filename] with the name of the file you want to create or edit. In this case, the filename
is new.txt
Editing/Insert Mode: To make any changes in a file in Vim editor user has to first
enter editing/insert mode, Press ‘i’ or ‘a’ to enter editing/insert mode.
Command Mode: To run any command in Vim editor user has to enter command mode, if
you are in editing/insert mode Press Esc then ‘:’ followed by the command. For example: set
the number
3. Quit the Vim editor
To quit the Vim editor enter command mode and use the following command,
Comman
d Description
Example/Screenshot:
Quitting Vim Editor
To save the changes to the file in the Vim editor enter command mode and use the following
command,
Comman
d Description
We have covered the navigation in Vim editor in a detailed article at GFG which you can check here.
Comman
d Description
6. Delete a line
To delete a single line Press the Esc key if you are in insert/editing mode, go to the file you want to
delete Press ‘dd’ and then the line gets deleted.
Syntax:
:[start],[end]d
Example::3,5d In this command the editor will delete the lines from 3 to 5 including the extremes.
Example Screenshot:
Deleting a Line
We have covered a detailed article on this at GFG which you can check out here.
To find and replace words in the vim editor we use substitute or:s command syntax of the command
is as follows:
:[range]s/{pattern}/{string}/[flags] [count]
The command searches the pattern in [range] lines and replaces the [pattern] with [string].
If [range] is not mentioned then the command will replace the words in the current line
with [string] only.
Example:
Searching and Replacing
Syntax:
:set number
or
:set nu
Example/Screenshot:
To display relative line numbers: In relative line numbers the current line is shown as 0 and the lines
above and below are incremented by 1,
Syntax:
:set relativenumber
or
:set rnu
Example/Screenshot:
Displaying Relative Line Number
We have covered a detailed article on this at GFG which you can check out here.
Syntax:
:set nonumber
or
:set nonu
Example/Screenshot:
:set norelativenumber
Alternatively,
:set nornu
We have covered a detailed article on this at GFG which you can check out here.
To Enable syntax highlighting type the below command and press Enter.
Syntax:
:syntax on
Screenshot:
To disable syntax highlighting type the below command and press Enter.
Syntax:
:syntax off
Screenshot:
Disabling Syntax Highlighting
Emacs Editor
Emacs is a popular text editor choice on Linux systems. It comes pre-installed or can be easily
installed via package managers on most Linux distributions. Emacs integrates smoothly with the
command-line interface of Linux, making it well-suited for terminal-based editing and remote use
over SSH. The customizability of Emacs through its own Lisp language allows tapping into numerous
community extensions tailored for Linux users’ needs like coding and system administration.
Although it has a steep learning curve initially, Emacs’ broad capabilities and ability to extensively
personalize the editing environment make it a powerful tool for Linux power users willing to invest
time in mastering it.