How To Use Vim Editor

Learn how to use vim editor; how to open vim editor in Ubuntu. This post explains about the basic vim editor commands in Linux Ubuntu. Vim is an editor to create or edit a text file. Vim exists as a console application, and therefore use the command “vim” (without quotes) to launch Vim in a terminal emulator.

Modes in Vim

There are two modes in vim. One is the command mode and another is the insert mode. In the command mode, user can move around the file, delete text, etc. In the insert mode, user can insert text.

Changing Vim Mode

From command mode to insert mode type a/A/i/I/o/O

  • a Append text following current cursor position
  • A Append text to the end of current line
  • i Insert text before the current cursor position
  • I Insert text at the beginning of the cursor line
  • o Open up a new line following the current line and add text there
  • O Open up a new line in front of the current line and add text there

From insert mode to command mode type Esc (escape key)

Pressing the Esc key switches back to command mode. If you’re not sure what mode you’re in because you use a really old version of vi that doesn’t display an “INSERT” message, type Esc and you’ll be sure to return to command mode. It is possible that the system gives a little alert when you are already in command mode when hitting Esc, by beeping or giving a visual bell (a flash on the screen). This is normal behavior.

Please note that the following commands can be used with the vim to enable other modes and working console interface:

  • vim The “normal” way, everything is default.
  • ex Start in Ex mode. Go to Normal mode with the “:vi” command. Can also be done with the “-e” argument.
  • view Start in read-only mode. You will be protected from writing the files. Can also be done with the “-R” argument.
  • gvim gview The GUI version. Starts a new window. Can also be done with the “-g” argument.
  • evim eview The GUI version in easy mode. Starts a new window. Can also be done with the “-y” argument.

Working on Files

  • vi filename Creates a new file if it already does not exist, otherwise opens an existing file.
  • vi -R filename Opens an existing file in the read-only mode.
  • view filename Opens an existing file in the read-only mode.

Opening a New File

  • Type vim filename
  • Type i
  • Enter text
  • Hit Esc key
  • Type :wq

Editing a File

  • Type vim filename
  • Move around the file using h/j/k/l key or any appropriate command
  • Edit required text
  • Hit Esc key (exit from insert mode if you insert or replace text)
  • Type :wq

Moving through the text

Moving through the text is usually possible with the arrow keys. If not, try:

  • h to move the cursor to the left
  • l to move it to the right
  • k to move up
  • j to move down

Save and Exit

To save and quit the vi or vim editor with saving all the changes:

  • press Esc key if you are currently in insert or append mode .
  • Press : (colon). The cursor should reappear at the lower left corner of the screen beside a colon prompt.
  • Enter the following: q!

To learn the basics of vim editing, issue the following command in a terminal emulator:

vimtutor

How To Use Vim Editor originally posted on Source Digit – Latest Technology, Gadgets & Gizmos.

Tags: