Day 6 - Basic Commands of Linux (vim, grep)

Day 6 - Basic Commands of Linux (vim, grep)

  • Vim -> It can be used to edit all kinds of plain text. It is especially useful for editing programs. There are a lot of enhancements above Vi: multi level undo, multi windows and buffers, syntax highlighting, command line editing, filename completion, on-line help, visual selection, etc.

  • Shift + :e[file] – Opens a [file] that you want to open. Here [file] is the filename that you want to open.

  • Esc + :w – Save the file but do not exit.

  • Esc + :q! – To quit from the file without first saving that you were working on.

  • Esc + :wq – To save the file and exit from vim.

To install vim on Debian based Linux like ubuntu run the command:

$ sudo apt-get install vim

To open a file in vim editor just write the file name after the vim command in the terminal as follows:

Then the file will be opened.

Let’s write some content in to write data we need to go in insert mode. To go into write mode type i. After that, we can write any data in it.

Save and Exit:;

We have written the data into a file now the task is to save and close the file to do that first exit from insert mode by pressing the Esc key. To write a command first type semicolon  (   :   )   and then type the command wq!  or x! (both do the same thing) And then hit ENTER.

Exit without saving the file:

To exit from vim without saving changes type :q!

Text Editing: Deletion

We provide x key in command mode to delete the character under the cursor. Move the cursor to the character which has to delete and press Esc key and then press the x key. Character under the cursor will be deleted.

Text Editing: Insertion

We have edited some text files before by using the i key. There are Four keys used for the insertion of text. Just type the key into the normal mode in vim.

i -> This key is used to put the cursor before the current position.

a -> This key is used to put the cursor after the current position.

o -> This key is used to put the cursor below the line.

O -> This key is used to puts the cursor above the line.

Deletion Commands: Always use the Esc key to go into normal mode and use the insertion, deletion keys, and other keys.

To delete the word move the cursor to the beginning of the word and use dw command in normal mode.

To delete 2 words use the command d2w.

To delete the line move cursor to the beginning of the line and use d$ command in normal mode

Undo and Redo:

To undo press u key in normal mode

To redo use the ctrl+r key in normal mode in vim.

  • grep(global regular expression print) -> The grep filter finds a file for a specific character pattern and shows every line that includes that pattern.

Syntax: grep [option] pattern [files]

Case insensitive search : The -i option enables to search for a string case insensitively in the given file

Displaying the count of number of matches : We can find the number of lines that matches the given string/pattern

Display the file names that matches the pattern : We can just display the files that contains the given pattern.

Checking for the whole words in a file : By default, grep matches the given string/pattern even if it is found as a substring in a file. The -w option to grep makes it match only the whole words.

Displaying only the matched pattern : By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option.

Show line number while displaying the output using grep -n : To show the line number of file with the line matched.

Inverting the pattern match : You can display the lines that are not matched with the specified search string pattern using the -v option.

Matching the lines that start with a string : The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.

Matching the lines that end with a string : The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.

Print n specific lines from a file: -A prints the searched line and n lines after the result, -B prints the searched line and n lines before the result, and -C prints the searched line and n lines after and before the result.

Syntax: $grep -A[NumberOfLines(n)] [search] [file]

$grep -B[NumberOfLines(n)] [search] [file]

$grep -C[NumberOfLines(n)] [search] [file]

Search recursively for a pattern in the directory: -R prints the searched pattern in the given directory recursively in all the files.

syntax: $grep -R [Search] [directory]

Displaying the string match line with number

If you want to print more than one word


Thanks for reading to the end; I hope you gained some knowledge.❤️🙌

PARAMVEER SINGH -_-