0% found this document useful (0 votes)
20 views9 pages

Linux Unit 3

The VI editor is a widely used text editor in Linux, known for its availability across distributions and user-friendly interface. It operates in two main modes: command mode for executing commands and insert mode for adding text. The document also covers various editing commands, navigation, saving, and closing files, as well as Linux filters and piping commands for processing text data.

Uploaded by

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

Linux Unit 3

The VI editor is a widely used text editor in Linux, known for its availability across distributions and user-friendly interface. It operates in two main modes: command mode for executing commands and insert mode for adding text. The document also covers various editing commands, navigation, saving, and closing files, as well as Linux filters and piping commands for processing text data.

Uploaded by

999426141b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is the VI editor?

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

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

vi Editor 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

How to use vi editor


To launch the VI Editor -Open the Terminal (CLI) and type
vi <filename_NEW> or <filename_EXISTING>

And if you specify an existing file, then the editor would open it for you
to edit. Else, you can create a new file.
VI Editing commands
• i – Insert at cursor (goes into insert mode)
• a – Write after cursor (goes into insert mode)
• A – Write at the end of line (goes into insert mode)
• ESC – Terminate insert mode
• u – Undo last change
• U – Undo all changes to the entire line
• o – Open a new line (goes into insert mode)
• dd – Delete line
• 3dd – Delete 3 lines.
• D – Delete contents of line after the cursor
• C – Delete contents of a line after the cursor and insert new text.
Press ESC key to end insertion.
• dw – Delete word
• 4dw – Delete 4 words
• cw – Change word
• x – Delete character at the cursor
• r – Replace character
• R – Overwrite characters from cursor onward
• s – Substitute one character under cursor continue to insert
• S – Substitute entire line and begin to insert at the beginning of
the line
• ~ – Change case of individual character

Note: You should be in the “command mode” to execute these


commands. VI editor is case-sensitive so make sure you type the
commands in the right letter-case.

Make sure you press the right command otherwise you will end up
making undesirable changes to the file. You can also enter the insert
mode by pressing a, A, o, as required.
Moving within a file
• k – Move cursor up
• j – Move cursor down
• h – Move cursor left
• l – Move cursor right

You need to be in the command mode to move within a file. The


default keys for navigation are mentioned below else; You can also
use the arrow keys on the keyboard.

Saving and Closing the file


• Shift+zz – Save the file and quit
• :w – Save the file but keep it open
• :q! – Quit vi and do not save changes
• :wq – Save the file and quit

You should be in the command mode to exit the editor and save
changes to the file.
Filters in Linux
Last Updated : 13 Jul, 2021


Filters are programs that take plain text(either stored in a file or produced by
another program) as standard input, transforms it into a meaningful format,
and then returns it as standard output. Linux has a number of filters. Some of
the most commonly used filters are explained below:
1. cat : Displays the text of the file line by line.
Syntax:
cat [path]

2. head : Displays the first n lines of the specified text files. If the number of
lines is not specified then by default prints first 10 lines.
Syntax:
head [-number_of_lines_to_print] [path]

3. tail : It works the same way as head, just in reverse order. The only
difference in tail is, it returns the lines from bottom to up.
Syntax:
tail [-number_of_lines_to_print] [path]
4. sort : Sorts the lines alphabetically by default but there are many options
available to modify the sorting mechanism. Be sure to check out the main
page to see everything it can do.
Syntax:
sort [-options] [path]

5. uniq : Removes duplicate lines. uniq has a limitation that it can only
remove continuous duplicate lines(although this can be fixed by the use of
piping). Assuming we have the following data.
Syntax:
uniq [options] [path]

You can see that applying uniq doesn’t remove any duplicate lines, because
uniq only removes duplicate lines which are together.
When applying uniq to sorted data, it removes the duplicate lines because,
after sorting data, duplicate lines come together.
6. wc : wc command gives the number of lines, words and characters in the
data.
Syntax:
wc [-options] [path]

In above image the wc gives 4 outputs as:


• number of lines
• number of words
• number of characters
• path
7. grep : grep is used to search a particular information from a text file.
Syntax:
grep [options] pattern [path]
Below are the two ways in which we can implement grep.

8. tac : tac is just the reverse of cat and it works the same way, i.e., instead
of printing from lines 1 through n, it prints lines n through 1. It is just reverse
of cat command.
Syntax:
tac [path]

9. sed : sed stands for stream editor. It allows us to apply search and
replace operation on our data effectively. sed is quite an advanced filter and
all its options can be seen on its man page.
Syntax:
sed [path]
The expression we have used above is very basic and is of the
form ‘s/search/replace/g’

In the above image, we can clearly see that Scooby is replaced by Scrapy.
10. nl : nl is used to number the lines of our text data.
Syntax:
nl [-options] [path]

It can clearly be seen in the above image that the lines have been numbered

Piping in Linux
The pipe is used to combine two or more commands, and in this, the output
of one command acts as input to another command, and this command’s
output may act as input to the next command, and so on.

Syntax:
command_1 | command_2 | command_3 | .... | command_N
Example of Piping in Unix or Linux
1. List all files and directories and give them as input to `grep`
command using piping in Linux
ls | grep file.txt

2. List all files and directories and give them as input to `more`
commands using piping in Linux.
$ ls -l | more
3. Sort a list of files by size using piping in Linux
ls -l sort -k 5

4. Use sort and uniq command to sort a file and print unique
values using piping in Linux
$ sort record.txt | uniq

5. Use head and tail to print lines in a particular range in a file.


$ cat sample2.txt | head -7 | tail -5
6. Use ls and find to list and print all lines matching a particular
pattern in matching files.
$ ls -l | find ./ -type f -name "*.txt" -exec grep "program" {} \;

7. Use cat, grep, tee and wc command to read the particular


entry from user and store in a file and print line count.
$ cat result.txt | grep "Rajat Dua" | tee file2.txt | wc -l

8.How can I redirect the output of a piped command to file in


Unix or Linux?
We can use redirection operator `>` to redirect the output of a piped
command.
For Example:
If i have a file name `file.txt` and want to redirect it to a file name `geeks.txt`.
ls | grep 'file' > geeks.txt

You might also like