Vim

From ArchWiki
Revision as of 08:42, 25 May 2008 by ST.x (Talk | contribs) (spelling)

Jump to: navigation, search

Template:I18n links start Template:I18n entry Template:I18n entry Template:I18n entry Template:I18n entry Template:I18n links end

Vim (Vi IMProved), is a text editor derived from Vi. It's notoriously known for its steep learning curve, and user unfriendly interface. However, because of it's efficiency, variety of plugins, and customization options vim is one of the most popular text editors for programmers and *nix users (along with Emacs*). A graphical version gVim which provides a user with menus is also available.

*Note that The Cult of Vi has determined that using emacs may cause dry mouth, blurred vision, drowsiness, dizziness, profuse sweating, tremors, sexual problems, headache, nausea and abdominal pain.

Installation

  • vim depends on vi, so that must be installed first. However, since vi is part of the base installation most users should already have it.
pacman -S vim

Configuration

vim's configuation file is in a user's home directy (~/) and is named .vimrc. A sample .vimrc can be found in /etc/vimrc.

"Sample .vimrc
set nocompatible
set showmatch
set incsearch
set ignorecase  
set smartcase
set history=100
set backspace=eol,start,indent
set ruler
set tabstop=4
set shiftwidth=4
set expandtab
set virtualedit=all
set background=dark
set vb t_vg=
set mouse=v
set textwidth=79
set formatoptions=tcrq
  • To install gvim (Just like vim but with gtk2 interface and listens to /etc/gvimrc and ~/.gvimrc)
pacman -S gvim
  • There is also a group of selected popular vim scripts available via pacman
pacman -S vim-plugins

VIM quick start guide

How to start vim

  • to start vim and edit a file (new or existing)
vim filename
  • to start vim and open a new file
vim

(You may name your file later when you save it.)

How to enter text

Vim is a modal editor. There are many modes, but the basic modes of vim are:

  • Insert mode, in which anything you type (except some special keys) will appear on the screen and become part of your file buffer
  • Command mode (also called normal mode), in which your key strokes are interpreted as commands.
  • So-called 'last-line' mode or 'ex' mode, where you may save a file or open another, etc.
  • Visual mode, which allows you to expediently cut, copy and pasted large areas of text with the keyboard or mouse.

Open a new text file with vim:

vim mytext

After you start vim, you're in command mode.

  • Switch between modes

1. From command mode to insert mode, press i

Enter some text.

2. from insert mode to command mode, press ESC

You are now in command mode. Vim is waiting for your commands. Notice if you try typing, you get weird and unexpected results, because, well, you need to learn some commands, and Vim is not in insert mode.

3. Press ESC again to make sure you are truly in command mode and press : (colon)

Now you are in ex, which will allow us to save your first file. Type:

wq

for write and quit.

Your file is written and vim will exit.

We'll focus on visual mode later.

How to move the cursor

In both the command mode and the insert mode, the arrow keys function to move the cursor, and with gvim you can mouse click to get to a new position. However, this is not the vim way. The most effective way of moving the cursor is to first enter the command mode by pressing ESC and then use vim's cursor-moving commands to move around. The 4 basic commands are

  • j move down one line
  • k move up one line
  • h move left one character
  • l move right one character

Remember: these commands work only in command mode. At first you may feel a bit uncomfortable. After you get familiar using these commands you will stick to them and forget the arrow keys.

Advanced movement:

  • 0 (zero) move to the first character of a line
  • $ move to the last character of a line
  • w move to the first character of the next word
  • e move to the last character of the next word
  • ( move to the beginning of the previous sentence
  • ) move to the beginning of the next sentence
  • { move to the beginning of the current paragraph
  • } move to the beginning of the next paragraph
  • PGUP or <CTR>F move up one page
  • PGDOWN or <CTR>B move down one page

How to delete text

First I will tell you that the DELETE key always works, and the BACKSPACE key works with the newly typed text in the insert mode. However, I suggest you do not use them. Instead, force yourself to use vim's deletion commands.

1. Make sure you are in command mode by press ESC

2. move the cursor to the character you want to delete

3. press x , this character disappears

x is just one of the many powerful deletion commands. Remember, try to use the cursor motion commands j k h l to locate your target, and don't leave the command mode.

How to insert text

1. In command mode, move the cursor to the desired location

2. press i, to enter insert mode. This will insert before the current character.

-or-

3. press a to enter insert mode. This will append text after the current character.

How to CUT, COPY and PASTE

If you run the GUI version of vim, gvim, you can use mouse and the pull-down menus to do that---the same fashion with other editors. However, that is not the preferred style. You'll feel better off if you can live without a mouse.

1. Enter the command mode by pressing ESC

2. Move the cursor to the line which you want to copy, by pressing j or k

3. press

yy

to make a copy of the line, or

dd

to cut it and make a copy

4. now move cursor (by pressing k or j) to the the location where you want to put this copy

5. press

p

to put the buffer after the current line, or

P

to put the buffer before the current line

If you want to copy or cut several lines, put a number before the yy or dd command, like

8yy

to copy 8 lines.

CUT, COPY and PASTE with Visual mode

  • Visual mode is like Command mode, but the movement commands extend a highlighted area. When a non-movement command is used, it is executed for the highlighted area. Cutting, copying and pasting large sections of text is more efficient in visual mode.

Hit v to enter visual mode from command mode, then use the cursor to go up or down. As you do, the lines of text will become highlighted. (You may also use the mouse to highlight areas of text)

To copy the highlighted section or 'yank':

y

To cut the highlighted section or 'delete':

d

To paste or 'put'

p

to place it before, or

P

for after.

Undo

Now that you have learned how to cut and delete, you may need to undo some mistakes

u

will undo the last function. Pressing it repeatedly will undo successive functions. Likewise, use

Ctrl-R

to redo.

How to search for a word

Suppose you want to find all the words apple in your file

1. Make sure you are in command mode by hitting ESC

2. type

/apple

followed by ENTER to find an occurrence of apple. When you type the slash, it and the following characters will be shown on the bottom of the screen. After you press ENTER, the cursor will go to the first occurrence of apple if found, and the target will be highlighted.

3. after you got the first apple, you can keep typing

n

to find other apples

How to substitute text

First make sure you're in command mode by pressing ESC.

  • to replace a single character, move the cursor over the character and hit r followed by its replacement.
  • replace first occurrence of old in current line with new
:s/old/new/
  • replace all occurrence of old in current line with new
:s/old/new/g
  • replace the first occurrence of old in each line between line n1 and n2 with new
:n1,n2s/old/new/
  • replace all occurrence of oldbetween line n1 and n2 with new
:n1,n2s/old/new/g
  • replace all occurrence of old in the whole buffer with new, prompt for confirmation.
:1,$s/old/new/gc
  • replace all occurrence of old in the whole buffer with new, prompt for confirmation.
:%s/old/new/gc

How to exit vim

  • To save and exit: press ESC to enter command mode, then:
:wq

or

:x

or

ZZ
  • save your file as newname before you exit: press ESC, then type
:wq newname
  • Exit without saving, press ESC, then type
:q
  • Forced quit

If :q doesn't work, it's probably because you didn't save the change. If you want to save, use :wq. If you don't want to save the changes, type

:q!

Vim Tutor

For more, run:

vimtutor

from a shell. Vim will open the tutor file.

External links