VI EDITOR
The VI Editor is a simple tool used on Linux or Unix computers to
write and edit text. It works in the terminal, which is just a black
screen where you type commands. VI doesn’t use a mouse, you
control everything with the keyboard. It has two main modes. One is
called Insert mode, where you can type and edit your text. The other
is Command mode, where you can give instructions like saving your
work, deleting text, or closing the editor.
MODES OF VI EDITOR
The VI Editor is different from regular text editors like Notepad
because it uses a mode-based system to perform different tasks.
This means that the keyboard works differently depending on the
mode you are in. Understanding these modes is important to use VI
effectively. The three main modes in VI are Command Mode, Insert
Mode, and Last Line Mode (also known as Ex Mode).
1. Command Mode:
When you first open a file in VI, it starts in Command Mode. In this
mode, you cannot type text directly. Instead, you use your keyboard
to give commands to the editor. These commands allow you to move
the cursor around the file, delete lines, copy and paste text, search
for specific words, and more. You don’t need to use a mouse
everything is controlled using keyboard shortcuts, which can be very
fast once you get used to them.
2. Insert Mode:
If you want to type or edit text, you must switch to Insert Mode. You
can enter this mode by pressing key like i (insert). In Insert Mode, you
can type text just like in any regular text editor. Once you are done
typing, you press the Esc key to return to Command Mode. This
switching between Command Mode and Insert Mode is one of the key
features that make VI different and more powerful than simple
editors.
3. Last Line or Escape Mode
The third mode is called Last Line Mode or Ex Mode, which is used
for more advanced operations. You can enter this mode from
Command Mode by pressing the colon key : . In Last Line Mode, you
can type commands at the bottom of the screen, such as :w to save
the file, :q to quit, or :wq to save and quit at the same time. You can
also use this mode to search for text, replace words, or open another
file.
HOW TO OPEN VI EDITOR
To open vi editors, we just need to type the command mentioned
below.
vi file_name
Here, file_name this is the file name we want to create or to open the
pre-existing file.
VARIOUS COMMANDS AND WORKING WITH VI EDITOR
Moving within a File (Navigation) in Vi Editor:
To move around within a file without affecting text must be in
command mode (press Esc twice). Here are some of the commands
that can be used to move around one character at a time.
Commands Description
k Moves the cursor up one line.
j Moves the cursor down one line.
Moves the cursor to the left one-character
h
position.
Moves the cursor to the right one-character
l
position.
o Positions cursor at beginning of line.
$ Positions cursor at end of line.
W Positions cursor to the next word.
B Positions cursor to previous work.
( Positions cursor to beginning of current sentence.
) Positions cursor to beginning of next sentence.
H Move to top of screen.
M Move to middle of screen.
L Move to bottom of screen.
Control Command (Scrolling) in vi Editor:
There are the following useful commands which can be used along
with the Control Key. These commands are helpful in saving time by
navigating quickly in a file without manually scrolling.
Command Description
CTRL+d moves the screen down by half a page.
CTRL+f moves the screen down by a full page.
CTRL+u moves the screen up by half a page.
CTRL+b moves the screen up by a full page.
CTRL+e moves the screen up by one line.
CTRL+y moves the screen down by one line.
CTRL+I redraw the screen.
Inserting and Replacing text in Vi Editor:
To edit the file, we need to be in the insert mode. There are many ways
to enter insert mode from the command mode.
Command Description
i Inserts text before current cursor location
a Insert text after current cursor location
A Insert text at the end of current line
Creates a new line for text entry below cursor
o
location and switches to insert mode.
Creates a new line for text entry above cursor
O
location and switches to insert mode.
Replaces single character under the cursor with any
s
number of characters and switches to insert mode.
Overwrites text from the cursor to the right, without
R
switching to insert mode.
Deleting Characters and Lines in Vi Editor:
Here is the list of important commands which can be used to delete
characters and lines in an opened file.
Command Description
`X`
Deletes the character before the cursor location.
(Uppercase)
`x`
Deletes the character at the cursor location.
(Lowercase)
Deletes from the current cursor location to the
`Dw`
next word
Deletes from current cursor position to the
`d^`
beginning of the line.
Deletes from current cursor position to the end
`d$`
of the line.
`Dd` Deletes the line the cursor is on.
Copy and Paste in Vi editor in Linux:
Copy lines or words from one place and paste them in another place
by using the following commands.
Commands Description
yy Copies the current line.
9yy Yank current line and 9 lines below.
p Puts the copied text after the cursor.
P Put the yanked text before the cursor.
Save and Exit in Vi Editor in Linux:
Need to press [Esc] key followed by the colon (:) before typing the
following commands:
Commands Description
q Quit
Quit without saving changes i.e. discard
q!
changes.
r [file_name] Read data from file called [file_name]
wq Write and quit (save and exit).
w Write to file called [file_name] (save as).
Overwrite to file called [file_name] (save as
w!
forcefully).
Runs shell commands and returns to Command
!cmd
mode.
SOME EXAMPLES RELATED ON VI EDITOR
1. Deleting Text
• Delete a single character:
Press x in Command Mode.
(It deletes the character under the cursor.)
• Delete a word:
Press dw in Command Mode.
(It deletes from the current cursor position to the end of the
word.)
2. Copying Text
• Copy (yank) a single line:
Press yy in Command Mode.
(It copies the whole current line.)
• Copy three lines:
Press 3yy in Command Mode.
(It copies the current line and the next two lines.)
3. Pasting Text
• Paste the copied text below the current line:
Press p in Command Mode.
• Paste the copied text above the current line:
Press P in Command Mode.
4. Cursor Navigation
• Move left: Press h
• Move right: Press l
• Move up: Press k
• Move down: Press j
5. Insert a blank space
• Insert a blank line below the current line:
Press o (lowercase letter o).
This opens a new empty line below and switches to Insert Mode
so you can start typing immediately.
• Insert a blank line above the current line:
Press O (uppercase letter O).
This opens a new empty line above and switches to Insert Mode.
GCC COMPILER
GCC, which stands for GNU Compiler Collection, is a powerful tool
used to convert the code that programmers write in languages like C,
C++, and others into a language that a computer’s processor can
understand and execute. Think of it like a translator that takes your
instructions written in human-readable programming languages and
turns them into the machine’s own language of zeros and ones. This
process is called "compiling." GCC is widely used because it is free,
open-source, and works on many different types of computers and
operating systems. It helps developers create software that runs
efficiently and correctly on various devices, making it one of the most
popular and trusted compilers in the programming world.
VARIOUS OPTIONS OF GCC COMPILER
The GCC (GNU Compiler Collection) is a command-line tool in
Linux used to compile C programs. It has many options (also called
flags) that control how the code is compiled. These options can help
you compile, debug, optimize, or generate specific types of output.
Here are the various options that are available in Linux System:
1. -o (Lower-Case) option:
The –o option, which allows you to specify the name of the output file
(the executable). By default, GCC names the output file as a.out,
but with -o, you can change it.
For example, the command gcc hello.c -o myprogram compiles
the source code hello.c and creates an executable file named
myprogram instead of the default name.
2. -c option:
The -c option is used when you want to compile a program but not
link it into an executable yet. This is especially useful when working
with multi-file programs. It creates an object file (.o) that contains
compiled code but isn’t ready to run.
For example, gcc -c hello.c produces a file named hello.o,
which can later be linked with other object files to create a complete
program.
3. -Wall option:
The -Wall option stands for “warn all” and is used to display all the
common warning messages during compilation. These warnings
help detect potential errors or bad coding practices.
For instance, if you write gcc -Wall hello.c -o hello, the
compiler will show you helpful messages about things that might not
be incorrect but are likely to cause bugs.
4. -g option:
When you want to debug your program using a debugger such as gdb,
the -g option is essential. It includes debugging information in the
executable, which allows gdb to show line numbers, variable names,
and other useful details while stepping through the code.
For example, gcc -g hello.c -o hello enables you to run and
debug your compiled program more effectively.
5. -O (Upper-Case) option:
The -O option is used for optimizing the performance of your code.
GCC supports different levels of optimization: -O (basic), -O1, -O2,
and -O3 (most aggressive). These options make your compiled code
run faster or use less memory.
For example, using gcc -O2 hello.c -o hello tells the compiler
to optimize the code for speed without increasing the size or
complexity too much.
6. -std option:
The -std option specifies which version of the C language standard
you want to use when compiling. This ensures your code follows the
rules and features of that version.
For example, gcc -std=c99 hello.c -o hello will compile the
code according to the C99 standard. You can also use -std=c11 for
the C11 standard or others like gnu99.
7. -E option:
The -E option stops the compilation process after the preprocessing
stage. Preprocessing includes things like expanding macros and
including header files. This is helpful for seeing what the code looks
like before it goes through actual compilation.
For example, gcc -E hello.c will display the preprocessed output
in the terminal, showing how all #include and #define directives
are handled.
8. -S option:
The -S option is used when you want to see the assembly code
generated from your C program. It stops the process after compiling
and creates a (.s) file with the assembly instructions.
For example, gcc -S hello.c will generate hello.s, which is
useful if you're interested in understanding how high-level code is
converted to low-level machine instructions.
9. -I option:
The -I option is helpful when your program uses header files that are
not in the default include directories. This option allows you to
specify a custom folder where GCC should look for these header files.
For example, gcc -I./include hello.c -o hello tells the
compiler to search for header files inside the include folder.
10. -L and -l option:
The options -L and -l are used together when your program depends
on external libraries that are stored in custom directories. The -L
option specifies the path to the folder where the library is located,
and -l specifies the name of the library (without the lib prefix and
file extension).
For example, gcc hello.c -L./libs -lmylib -o hello links
the program with a library file like libmylib.so or libmylib.a
located in the ./libs directory.
SOME EXAMPLES RELATED ON GCC COMPILER
Here are some examples below:
a.) Create only object files from test.c files
Use the -c option
This compiles the source code but does not link, producing only the
object file (.o).
Command: gcc -c test.c
Result: Creates test.o — a compiled object file that can be linked
later with other files.
b.) Create executable files from test.c files
Use the -o option
This compiles and links the source code to produce a final
executable file with a custom name.
Command: gcc test.c -o myprogram
Result: Creates an executable file named myprogram. Without -o, it
creates a default executable named a.out.
c.) Create executable files from object files
Link an object file to produce an executable
When you already have test.o, you can link it to generate an
executable.
Command: gcc test.o -o myprogram
Result: Links the object file and creates myprogram as the final
executable.
d.) Debug the program and show warnings
Use both -g and -Wall options
-g adds debugging info for use with tools like gdb.
-Wall shows all common warnings to help catch errors.
Command: gcc -g -Wall test.c -o myprogram
Result: Compiles test.c, shows warnings, and produces a debug-
enabled executable myprogram.