VI, a screen-oriented text editor

 

vi

Introduction

(from wikipedia)

vi is a screen-oriented text editor originally created for the Unix operating system.

The original code for vi was written by Bill Joy in 1976, as the visual mode for a line editor called ex that Joy had written with Chuck Haley.Bill Joy’s ex 1.1 was released as part of the first BSD Unix release in March, 1978. It was not until version 2.0 of ex, released as part of Second Berkeley Software Distribution in May, 1979 that the editor was installed under the name vi (which took users straight into ex’s visual mode), and the name by which it is known today.

The name vi is derived from the shortest unambiguous abbreviation for the command visual in ex; the command in question switches the line editor ex to visual mode.

Trivial : A 2009 survey of Linux Journal readers found that vi was the most widely used text editor among respondents, beating gedit, the second most widely used editor by nearly a factor of two (36% to 19%).

 

1) Generalities

(loosely based on this VI tutorial)

  • Start using VI : To start vi just type vi at the operating system prompt. You will see a screen with a column of tildes (~) down the left side of the screen. This signifies an empty workspace. To edit a file, just include the filename after it, e.g. vi filename1. You will see the text of the file you included. Vi is now in command mode. The most basic command to enter insert mode is i which lets you insert text to the left of the cursor.

 

  • Insertion mode : Let’s begin with a brief description of the insert mode because it is very straight forward. In insert mode the characters you type are inserted into your document. You can use the backspace key to delete any typing mistakes you have made on the current input line. The escape key (<esc>) takes you out of insert mode and back to the command mode. If you are ever in doubt about what mode you are in, just press <esc> a few times until vi starts complaining. You will then know that you are in the command mode.

 

  • Command mode : This is where you do everything that isn’t done in insert mode. In command mode the same keys that caused letters to appear on your screen in insert mode now represent totally different functions. Rather than go into a detailed discussion of the 100 or so commands, this section contains a list of the more popular commands. A more comprehensive list of vi commands may be found in the Vi reference section.

 

  • Visual mode : The visual mode can be handy from times to times. To enter the visual mode just press the “v” key.

 

 

2) Commands examples

The following examples are to be used in command mode.

2.1 Options while opening a file

  • Open <file> directly at a line identified by its number
    Note that if you omit the <line_number> the file will be open straight to its last line.

    vi +<line_number> <file>
  • Open <file> directly at a line identified by a <pattern>
    vi +/<pattern> <file>
  •  Print the current file name and information
    [CTRL] + [g]

 

2.2 Substitution

  • General syntax
    :s/old/new/
  • Substitute a string matching <pattern1> by <pattern2>, globally (every occurence within the current file)
    :%s/<pattern1>/<pattern2>/g

    or using the ex global command

    :g/<pattern1>/s//<pattern2>/g
  • Not sure about the changes, want to confirm before any changes ? use the c option
    :%s/<pattern1>/<pattern2>/gc

    the c option also works with the ex global command

    :g/<pattern1>/s//<pattern2>/gc

 

2.3 View or filter

With those tips just type “:q” in command mode to return to the original file (with no filter)

  • View “only uncommented” lines
    :g!/^#

    or the equivalent to g!

    :v/^#
  • View only “not empty” lines
    :g!/^$ #

    or with the same as above (“v” instead of “g!”)

    :v/^$

 

2.4 Deletion

  • General syntax
  • Using a <pattern>
    :g/<pattern>/d
  • Using a line number
    :<line_number>d
  • Using a range(from line n°5 to 9 here) of <line_number>
    :[5-9]d
  • Delete every commented line (when lines are commented using the dash character “#”)
    :g/^#/d
  • Delete every empty lines
    :g/^$/d

    Note : To get only the “uncommented” and “not empty” lines, just combine the above two commands. Also you may want to put the result of these two commands in a new file, the w <new_file_name> may do this:

    :w <new_file_name>

 

2.5 Search

  • Search for a string matching <pattern> within the actual file
    :/<pattern>
  • Search for the exact word currently under the prompt (by exact i mean “whole word“)
    *

 

2.6 Tabs navigation

Tabs navigation may dramaticaly improve yiour vi habits, giveit a try and you will love it for sure! you xill find in this section basics and less basics commands to use tabs navigation under vi/vim.

  • Open <file_1> and <file_2>in vi using tab mode
    vi -p <file_1> <file_2>
  • Close a tab
    :tabc # c for close

    Note : you can also close a tab by exiting the edited file using :q or :q! if required or ZZ etc…

  • Navigate through opened tabs
    :tabn # go to next tab (n for next)
    :tabp # go to previous tab (p for previous)
    :tabn <N> # jump to next tab number <N>
    :tabr # jump to firdt tab (r for rewind)
    :tabl # jump to last tab (l for last)
  • Show a list of tabs (with the corresponding file)
    :tabs

 

To close a tab, use :tabc. To switch to the next tab, use :tabn, and to switch to the previous tab, use :tabp (short for tabnext and tabprevious respectively). You can also jump over tabs by using :tabn 2, which will move to the second next tab. To jump to the first tab, use :tabr (tabrewind) and to jump to the last tab use :tabl (tablast). Finally, :tabs will give you a list of open tabs.

 

2.7 Indentation

When it comes to coding, the indentation is nothing but a life or death question to me, and vi can be very powerful in this domain : But i am still learning, i will share here every new tips i find useful.

  • In normal mode :
    • enter >> to indent the current line or << to unindent
    • you can add an “address” in front of the >> operator, to apply the move to certain lines : 5>> would indent 5 lines at once
    • don’t forget the . which repeat the last command, using this you can indent more : 5>>.. would indent 5 lines to 3 levels of indentation
  • In insertion mode
    • [Ctrl] + T indent
    • [Ctrl] + D unindent

     

 

 

3) VI moves tips

Please note that all the following command are to be used in command mode.

3.1 Move to first line of file

There are quite some different way to directly go to the first line of a file within vi, here a list of my favourite :

  • gg
  • :1 or :0
  • 1G

3.2 Move to last line of file

Same as above but for the last line, the best one is :

  1. G ([SHIFT]+g

 

 

More “vi” posts

 

Resources

  1. Full of VI tips
  2. Vi tips wiki
  3. wikipedia
  4. Vi reference
  5. Good Vi tutorial

1 If the file does not exist, it will be created.

Tagged on: , , ,

2 thoughts on “VI, a screen-oriented text editor

  1. 2r-arne

    2.2 Substitution
    Substitute a string matching by , in all lines between line number and
    :,s///g

    Substitute only first occurrence in each line of a string matching by , in all lines between line number and
    :,s///

    2.3 View or filter
    Make current line as the first line on screen
    z

    Refresh your screen (garbled output on tty)
    Ctrl+L

    4. External commands
    Backup copy the file your editing
    :!cp -p % %.bck

    You might find better editors, but VI is commonly available and by this simple fact… a necessity to learn – at least to a basic level. Grasping more of the powerful commands in VI makes you like it as well as being more efficient in your work.

    The statement that VI is a screen-oriented text editor is a bit overkill (unless you compare it to Notepad)… I haven’t seen a truly screen-oriented editor since the 80s … when using PED from Norsk data.
    This was a truly screen-oriented editor which did not read into memory the entire file (which often could cause a memory overflow making you unable to open it), but only kept the number of lines on screen in memory. Also being able to edit the file by working on vertical sections/columns of the screen, was something available in those old “transaction based” programs.
    I still haven’t found any VI implementations that give me these two “simple” features ;)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site supports SyntaxHighlighter via WP SyntaxHighlighter. It can highlight your code.
How to highlight your code: Paste your code in the comment form, select it and then click the language link button below. This will wrap your code in a <pre> tag and format it when submitted.