Linux Admin – Using the vi / vim Text Editor

Linux Admin - Using the vi / vim Text Editor

This Topic is about 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 −

KeyAction
jMove down one line
kMove up one line
lMove to the left on character
hMove to the right one character

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

CommandAction
GGo to the end of the file
ggGo to the beginning of the file
xDelete the selected character
uUndo the last modifications
EnterJump forward by lines
ddDelete the entire line
?Search for a string
/Proceed to the next search occurrence

Please try the following tasks in vim, to become familiar with it.

  • Search for the string “test”, then first 5 occurrences
  • Move to the beginning of the document after finding the first 5 occurrences of “test”
  • Go to line 100 using enter
  • Delete the entire word using “x”
  • Undo the deletions using “u”
  • Delete the entire line using “dd”
  • 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 2 − Press shift+:
  • Step 3 − type w myfile.txt:w and hit Enter
  • Step 4 − Press shift+:
  • 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]#

Linux File Input/Output Redirection

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.

STDOUT

[root@centosLocal centos]# cat output.txt  
Hello, 
I am coming from Standard output or STDOUT. 
[root@centosLocal centos]#

STDIN

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

NumberFile DescriptorCharacter
0standard input<
1standard output>
2standard error
append stdout>>
assign redirection&
pipe stdout|

In this topic we learned about Linux Admin – Using the vi / vim Text Editor. To know more : Click Here.

This Post Has 2 Comments

Leave a Reply