Linux Admin - Using the vi / vim Text Editor

vim represents a newer, improved version of the vi text editor for Linux. vim is installed by default on CentOS 7, the most recent version of CentOS. However, some older and minimal base installs will only include the original vi by default.
The biggest difference between vi and vim are advanced ease-of-use features such as moving the cursor with the arrow keys. Where vim will allow the user to navigate a text file with the arrow keys, vi is restricted to using the "h", "j", "k", "l" keys, listed as follows.
vi text document navigation −
Using vim the same actions can be accomplished with the arrow keys on a standard English (and other common language) based qwerty, keyboard layout. Similarly, vi will often not interpret the numeric keypad on as well.
Mostly, these days, vi will be symlinked to vim. If you ever find it frustrating your arrow keys are doing things unexpected when pressed, try using your package manager to install vim.
vim uses the concept of modes when manipulating and opening files. The two modes we will focus on are −
normal − This is the mode vim uses when a file is first opened, and allows for entering commands
normal − This is the mode vim uses when a file is first opened, and allows for entering commands
insert − The insert mode is used to actually edit text in a file.
insert − The insert mode is used to actually edit text in a file.
Let's open a file in vim. We will use the CentOS default dictionary located at /usr/share/dict −
[root@localhost rdc]# cp /usr/share/dict/words

What you see is the text file opened in normalmode. Now practice navigating the document using the arrow keys. Also, try using the h,j,k and lkeys to navigate the document.
Vim expects us to send commands for file operations. To enable line number, use the colon key: shift+:. Your cursor will now appear at the bottom of the document. Type "set nu" and then hit enter.
:set nu

Now, we will always know where in the file we are. This is also a necessity when programming in vim. Yes! vim has the best syntax highlighting and can be used for making Ruby, Perl, Python, Bash, PHP, and other scripts.
Following table lists the most common commands in normal mode.
Please try the following tasks in vim, to become familiar with it.
Search for the string "test", then first 5 occurrences
Search for the string "test", then first 5 occurrences
Move to the beginning of the document after finding the first 5 occurrences of "test"
Move to the beginning of the document after finding the first 5 occurrences of "test"
Go to line 100 using enter
Go to line 100 using enter
Delete the entire word using "x"
Delete the entire word using "x"
Undo the deletions using "u"
Undo the deletions using "u"
Delete the entire line using "dd"
Delete the entire line using "dd"
Reconstruct the line using "u"
Reconstruct the line using "u"
We will pretend that we made edits on a critical file and want to be sure not to save any unintended changes. Hit the shift+: and type: q!. This will exit vim, discarding any changes made.
Now, we want to actually edit a file in vim: at the console type: vim myfile.txt
We are now looking at a blank text buffer in vim. Let's write something: say - hit "i".
vim is now in insert mode, allowing us to make edits to a file just like in Notepad. Type a few paragraphs in your buffer, whatever you want. Later, use the following steps to save the file −
Step 1 − Press the escape key
Step 1 − Press the escape key
Step 2 − Press shift+:
Step 2 − Press shift+:
Step 3 − type w myfile.txt:w and hit Enter
Step 3 − type w myfile.txt:w and hit Enter
Step 4 − Press shift+:
Step 4 − Press shift+:
Step 5 − Type q! and hit Enter
Step 5 − Type q! and hit Enter
We have just created a text-file named, myfile.txt and saved it −
[root@localhost]# cat myfile.txt  
this is my txt file.
[root@localhost]#

The pipe character "|", will take an output from the first command, passing it to the next command. This is known as Standard Output or stdout. The other common Linux redirector is Standard Input or stdin.
Following are two examples; first using the cat command putting the file contents to stdout. Second using cat to read a file with the standardinput redirector outputting its contents.
[root@centosLocal centos]# cat output.txt  
Hello, 
I am coming from Standard output or STDOUT. 
[root@centosLocal centos]#

[root@centosLocal centos]# cat < stdin.txt  
Hello, 
I am being read form Standard input, STDIN. 
[root@centosLocal centos]#

Now, let's "pipe" the stdout of cat to another command.
[root@centosLocal centos]# cat output.txt | wc -l 
2 
[root@centosLocal centos]#

Above, we passed cat'sstdout to wc for processing the pipe character. wc then processed the output from cat printing the line count of output.txt to the terminal. Think of the pipe character as a "pipe" passing output from one command, to be processed by the next command.
Following are the key concepts to remember when dealing with command redirection.

    57 Lectures 
     7.5 hours 

 Mamta Tripathi

    25 Lectures 
     3 hours 

 Lets Kode It

    14 Lectures 
     1.5 hours 

 Abhilash Nelson

    58 Lectures 
     2.5 hours 

 Frahaan Hussain

    129 Lectures 
     23 hours 

 Eduonix Learning Solutions

    23 Lectures 
     5 hours 

 Pranjal Srivastava, Harshit Srivastava
 Print
 Add Notes
 Bookmark this page