Vim

From LQWiki
Jump to: navigation, search

Vim (Vi IMproved) is a contemporary version of the classic vi editor, with additional capabilites.

Introduction

All UNIX machines typically come with the vi text editor. However, vi lacks some more contemporary features (for example, syntax highlighting). This is where Vim comes in. If you know how to use vi, you'll find Vim to be the same except it has a number of useful features added to make editing easier. If you don't yet know how to use vi or Vim, and you didn't come from using ed, you'll find the learning curve quite steep (which means, it might take you a while to learn to achieve maximum proficiency).

Vim was originally written by (and is still primarily maintained by) Bram Moolenaar. It started as a personal desire to have a useful vi-clone for the Amiga, but eventually grew as more features were added. It now runs on a variety of platforms (including Unix work-a-likes, e.g. GNU/Linux, Mac OS X, and *BSD; Amiga; Windows; Mac "Classic"; and OS/2) and supports a variety of graphical toolkits (including GTK, QT, and Carbon) in addition to the command-line interface. VIM is also the latin word for "power" or "force".

Vim has a compatibility mode which produces an almost vi-compatible experience (the version of vi used seems to be one released with Sun OS 4.x, from Version 3.7 dated on 6/7/85). This is enabled by using the ":set compatible" ex-command or by running Vim with the -C option.

Tips

Learning

To learn Vim, open a console and call

vimtutor

A tutorial

While Vim is not as easy to learn initially as it is to learn nano, there are plenty advantages to using it. Some great features of Vim are copy, cut, and paste. Also there is code styling for programmers and there's also an undo/redo feature which is awesome.

There are three modes for Vim (That I know of right now). There is the visual mode, insert mode, and the command mode. Before I start breaking down the different modes let me say this: every time you delete or copy text in the command or visual mode, the deleted text is loaded into a special buffer. This buffer continues to hold the text until you delete more text/characters/lines in which case the buffer will be replaced with the newly deleted text. I say that because the buffer is also used for copying and pasting. So if you copy text and then delete some text, the next time you paste you'll be pasting the last text you deleted instead of what you copied.

When you first open a file with command vim myfile you start out in the command mode. If the file myfile exists then it will load myfile into the editor. If the file myfile does not exist then it will create a blank file with myfile as its save path which can be modified but myfile will not be created on your hard drive until you save the document.

Pressing the i key will put the editor into insert mode. Go ahead and put the editor into insert mode and type some random text you can play with. In fact you should type a couple of lines.

Pressing the Esc while in insert mode will take the editor out of insert mode and back into command mode. Now each letter on your keyboard has turned into a command which manipulates the document instead of keys which insert text into the document.

Pressing the v key puts the editor into visual mode. Visual mode is used to highlight text in the document so you can apply different commands just to that text. If you accidentally go into visual mode or you've highlighted text and don't want to do anything to it then press the v key to go back into command mode.

While in command mode, pressing the colon key (: ) opens the editors command line where you can type a command for the editor to manipulate the whole document and not just some text or a few lines. For example in the editors command line you can save the document, quit the document, quit without saving, undo change, and redo change to name a few. You can even combine commands to do them at the same time such as save and quit at the same time.

That's most of the explanation behind it. Now remember the three modes: command, insert, and visual. Everything is centralized around the command mode. Also remember the editor command line which is accessed when in the command mode. Now that I've explained all that here is what happens when pressing different keys in the different modes.

When in command mode press the colon (: ) to enter the editor command line. Here are different editor commands. command - explanation

 :w write/save the document
 :q quit Vim
 :q! quit Vim without saving
 :u undo
 :r redo
 :wq write/save the document and then quit Vim

If you accidentally enter the editor command line pressing Esc twice will put you back into command mode.

When you're in the command mode different keys have different functions. The commands are case sensitive.

command explanation
h move cursor left
j move cursor down
k move cursor up
l move cursor right
w move the cursor right and down the document accross each word instead of each character.
ZZ same as :wq. write/save the document and then quit Vim. Double tap the z key while holding the shift key (double click).
dd cuts/deletes an entire line of text. Double tab the d key (double click).
dw cuts/deletes a words of text
yy copies an entire line of text. Double tap the y key (double click).
p paste text after: if copied entire line then it will paste text after the current line. if copied a few characters then paste them after the current character.
P same as p but pastes text before the current character/line.
i enters editor into insert mode
o inserts a new line after the current line and then places the editor into insert mode.
u Undo the last edit. Same as :u.
v enters the editor into visual mode.
r replace. When you push a key after pressing r the letter the cursor is currently located on is replaced with the key pressed. You are still in command mode.
R replace. puts the editor into insert mode however it overwrites replacing each character if one already exists.
x delete a character

When you're in insert mode every key on the keyboard types text into the document like a normal text editor. When you wish to leave insert mode press the Esc key.

When you're in the visual mode you can highlight text and apply different commands to it. The commands are similar to command mode and are also case sensitive.

command explanation
h move cursor left (highlighting text)
j move cursor down (highlighting text)
k move cursor up (highlighting text)
l move cursor right (highlighting text)
y copy the highlighted text and enter back into command mode
d cut/delete the hightlighted text and enter back into command mode
p paste the text from the buffer replacing the highlighted text.

The following command opens myfile and automatically goes to line 23.

vim +23 myfile

It's a bit of a task remembering all those commands. The best way to learn them is to try to use Vim regularly referring to those commands for the different modes. There's a lot more commands and tricks you can do in Vim. Those are just the basic ones.

Interactive tutorial

If you installed Vim on your GNU/Linux machine then vimtutor was also installed. vimtutor is an interactive tutorial which can be run from the terminal.

vimtutor

It's easy to learn that way.

~/.vimrc

To make a configuration file for Vim, create a file in your home directory called .vimrc, and fill it with stuff that you'd normally type into Vim's "ex" mode. For example, if you regularly do ":syntax on" from inside Vim, put "syntax on" (no colon or quotes) on a line by itself in your ~/.vimrc file.

Here's the contents of a useful .vimrc file:

syntax on
set number
set expandtab
set tabstop=4
set shiftwidth=4
set autoindent

Where:

  • "syntax on" turns on syntax highlighting
  • "set number" turns on line numbers
  • "set expandtab" makes Vim insert spaces (instead of tabs) whenever you hit the tab key
  • "set tabstop=4" sets tabs to equal 4 spaces
  • "set shiftwidth=4" makes it so when you use the text shifting command, it shifts over using 4-space indents.
  • "set autoindent" has Vim use "smart indenting", ie. when you are tabbed out to, say, the 8th column, and you type something then hit enter, the cursor is helpfully placed at the 8th column again for you.

Additionally you can turn any single argument command off by adding an exclamation point (!). So "set number" can be turned off by "set number!"

(You can shut off line numbering from command mode by typing :set nonumber.)

Syntax highlighting

You can turn syntax highlighting on/off with:

:syntax off
:syntax on

If you have syntax highlighting on, but are using a dark background and the colors don't show up well, you can use this command:

:set background=dark

External links