Thursday, October 21, 2010

Creating, Editing, and Viewing Text Files with Vim Editor


Vi / Vim Editor


Please note: This post is not completed yet.



 In this post I will show you how to:

  • create a text file or view an existing text file for editing
  • insert new lines / blank lines
  • navigate through the pages of a text file and move around it 
  • go to a specific line number 
  • search for a specific piece of text or information in a text file while it is opened
  • copy, cut, and paste text
  • set certain features on / off

Introduction

Vim is the improved or enhanced version of Vi Text Editor. It is available in most Unix and Linux Operating Systems by default. Its beauty lies in the fact that we can use it in both GUI (Graphical User Interface) and CLI (Command Line Interface) modes.

The Vim Editor can be used for a variety of purposes:
  • to create and edit plain text files
  • to write scripts or codes in a particular Computer Programming Language such as Perl and C
  • to write web pages in HTML  
Its code colorization or syntax highlighting also helps in recognizing keywords and constants (I will cover them in some other post) written in Shell Script or PHP or any other scripting language.

Normally vi would use vim. Please see this note:

$ whatis vim
vim                  (1)  - Vi IMproved, a programmers text editor
$ type vi
vi is aliased to `vim'
$ type vim
vim is /usr/bin/vim
 Vim Editor has these three modes:
  • Escape Mode 
    • This is the default mode in which Vim opens a named file. In the Escape Mode we can move around the file and perform deletion, copying, and pasting of specific text or lines.
  • Insert Mode 
    • This is the mode which is used for editing a text file. 
  • Command Mode
    • This mode is useful for saving an opened file, exiting from Vim, searching for a piece of text in the file, and for environment settings such as turning Line Numbering On or Off.
 How to create a text file or view an existing text file for editing?

Command: vi or vim

Usage:

$ whatis vim
vim                  (1)  - Vi IMproved, a programmers text editor

Examples: 
$ vi helloWorld
or

$ vim helloWorld
A window or view opens and at the bottom the following line is displayed:

"helloWorld" [New File]                                       0,0-1         All
Note:
If the named file existed then it would be opened or else a new file would be created as shown above.

To begin tpying in, hit i or Shift + i 

To save your work and exit from the Vim Editor, hit Esc and Shift + : and type wq and hit the Enter Key. 

To display the contents of the text file we have just created, we can use the cat command: 

$ cat helloWorld
Hello World!
How to insert a new / blank line?

Open a text file:

-bash-2.05b# vi hellWorld


|Vim is an enhanced version of Vi.
It is found on almost all Unix and Linux system.
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"hellWorld" 2L, 83C                                           1,1           All

To insert a blank line below the current line, first make sure that we are in the Escape mode. Hit Esc. Now hit o to insert a new line as shown below:

Vim is an enhanced version of Vi.
|
It is found on almost all Unix and Linux system.
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
-- INSERT --                                                  2,1           All

The cursor is placed on the second line and the INSERT mode is opened. We cab begin typing here.

To insert a blank line above the current line, hit Esc to close the INSERT mode and return to Escape mode. Now hit O (Shift + o) as shown below:

Vim is an enhanced version of Vi.
|
It can be used for editing a text file easily.
It is found on almost all Unix and Linux system.
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
-- INSERT --                                                  2,1           All
 Once again we are in the INSERT mode and the cursor is placed on the newly created / inserted blank line.

Save your work by hitting Shift + : and typing w and then hitting the Enter Key. To quit from the Vi editor, hit Shift + : and type q and then hit the Enter Key.


To save and quite in one go following this procedure: Shift + :wq




No comments:

Post a Comment