0% found this document useful (0 votes)
30 views14 pages

WEEK 1 Objective

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views14 pages

WEEK 1 Objective

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

WEEK 1 Objective: Getting familiar with the programming environment on the computer and writing

the first program

Suggested Experiments/Activities:

Tutorial 1: Problem-solving using Computers.

Lab1: Familiarization with programming environment

1Q: Basic Linux environment and its editors like Vi, Vim & Emacs etc

Ans: Linux Text Editors:

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.

Mostly Used Text Editors in Linux

 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

(here I am using a txt file)

After opening the file it would look something like this :

Modes in the Vi text editor

There are three modes in the vi text editor, they are :

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.

# In Normal Mode, press 'v' to enter Visual Mode.

This is some text you can select.

Navigation in VI:

1. Moving the Cursor:

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.

2. Jumping to the Beginning or End of the Line:

Use 0 to go to the beginning of a line and $ to jump to the end.

# 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.

3. Jumping to Specific Lines:

To move to a particular line, type :n, where n is the line number.

# In Normal Mode, ':n' takes you to line number 'n'.

This is line 1.

This is line 2.

This is line 3.

To go to line 2, you’d type :2 and press Enter.

Editing Text

1. Inserting Text:

In Insert Mode, type your text as needed.

# In Insert Mode, type your text.


This is some text you can edit. [You are in Insert Mode]

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.

# In Normal Mode, 'x' deletes the character under the cursor.

This is some t xt you can edit. [Cursor on 'e']

# 'dd' deletes the entire line.

This line will be deleted.

# 'D' deletes from the cursor position to the end of the line.

This line is deleted. [Cursor on 'i']

3. Copying and Pasting:

To copy text, use yy. To paste, press p to paste below the cursor or P to paste above the cursor.

# In Normal Mode, 'yy' copies the current line.

This is a line to copy.

# 'p' pastes the copied line below the cursor.

This is a line to copy.

This is the pasted line.

# 'P' pastes the copied line above the cursor.

This is the pasted line.

This is a line to copy.

Saving and Quitting

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.

# In Normal Mode, ':w' saves the file.

:w

# ':w filename' saves the file with a new name.

: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' saves changes and quits.

:wq

# ':q!' quits without saving changes.

:q!

Searching

Searching for Text:

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.

# In Normal Mode, '/' initiates a search.

/SearchText

# Press 'Enter' to find the next occurrence.

# Press 'n' to repeat the search.

Undo and Redo

Undoing:

To undo your last action, press u in Normal Mode.

# In Normal Mode, 'u' undoes the last action.

This is some text. [Undoing last action]

Redoing:

To redo an undone action, press Ctrl + r.

# In Normal Mode, 'Ctrl' + 'r' redoes an undone action.

This is some text. [Redoing last undone action].

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.

Basic Vim Commands

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).

Basic VIM Commands

 1. Create/Edit a file

 2. Changing modes in the Vim editor

 3. Quit the Vim editor

 4. Save the changes

 5. Navigation in Vim editor

 6. Delete a line

 7. Search and Replace

 8. Display Line Numbers

 9. Hide Line numbers

 10. Syntax Highlighting

1. Create/Edit a file

To create/edit a file in the Vim editor run the below command

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

Creating Files in Vim

2. Changing modes in the Vim editor

There are Six modes in Vim editor.

 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.

Entering in 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

:q Quit the editor

:q! Quit without saving changes i.e. discard changes.

:wq Save the changes and quit the editor

Example/Screenshot:
Quitting Vim Editor

4. Save the changes

To save the changes to the file in the Vim editor enter command mode and use the following
command,

Comman
d Description

:w Write to file called [file_name] (save as).

:w! Overwrite to file called [file_name] (save as forcefully).

:wq Save the changes and quit the editor

5. Navigation in Vim editor

We have covered the navigation in Vim editor in a detailed article at GFG which you can check here.

Comman
d Description

k Moves the cursor up one line.

j Moves the cursor down one line.

h Moves the cursor to the left one-character position.

l Moves the cursor to the right one-character position.

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.

To delete a range of lines use the following command,

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.

7. Search and Replace

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

8. Display Line Numbers

To display absolute line numbers use any of the following commands,

Syntax:

:set number
or
:set nu

Example/Screenshot:

Displaying Line Number

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.

9. Hide Line numbers

To hide absolute line numbers use any of the following commands,

Syntax:

:set nonumber
or
:set nonu

Example/Screenshot:

Hiding Line Number

To hide relative line numbers use any of the following commands,


Syntax:

:set norelativenumber
Alternatively,
:set nornu

We have covered a detailed article on this at GFG which you can check out here.

10. Syntax Highlighting

To Enable syntax highlighting type the below command and press Enter.

Syntax:

:syntax on

Screenshot:

Enabling Syntax Highlighting

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.

You might also like