Gentoo Wiki ArchivesGentoo Wiki

Vim


Vim
VI iMproved
Developer: Vim
Package: app-editors/Vim
Category: app-editors
License: Vim license (GPLv2)
Website: http://www.vim.org/

Contents

Introduction

Vim is an advanced text editor that seeks to provide the power of the de-facto Unix editor 'Vi', with a more complete feature set. Vim is not a simple text editor like nano or pico. It does require some time to learn, and a great amount of time to master.

About Vim

Vim is probably the most popular incarnation of its predecessor vi, but all vi packages are similar.

Vim is designed to make your fingers work as little as possible, and you should never have to use the mouse. This may seem odd, but once you master Vim, you'll wonder why other apps don't behave like it.

Features

Starting Vim

If you start vim with vim somefile.txt you'll see a blank document (providing that somefile.txt does not exist. If it does, you'll see what's in there). You will not be able to edit right away. You are in Command Mode. In this mode you are able to issue commands to vim with the keyboard, and not have to twist and contort your fingers to line up to ctrl+alt+space+whatever.

Note: Vim is an example of classic UNIX-style ware. This means that its not flashy, and it won't hold your hand. It doesn't come with built-in paperclips and games. It will allow you to get the job done, however, and quickly too. Also, all commands are case sensitive. Sometimes the uppercase versions are "blunter" versions (s will replace a character, S will replace a line), other times they are completely different commands (j will move down, J will join two lines).

Starting

Let's work on something. It can be any text file, really. If you absolutely cannot think of any text files to work on, copy an ebuild to your home directory and use that. Then open that file with vim:

vim foo.txt

Basic Editing

Begin in command mode. If you're not sure what mode you're in, just press ESC.

You insert text (stick it before the cursor) with the i command. I will place the cursor at the beginning of the line. You append text (place text after the cursor, what most people expect) with a. Typing A will place the cursor at the end of the line.

Return to command mode at any time by pressing ESC.

Moving Around

Single Characters

In Vim, you can move the cursor with the arrow keys, but that's no very efficient is it? You'd have to move your right hand all the way from the home keys all the way to the arrow keys, and then back. Not fun.

In vi you can move down by pressing j. You can remember this because the "j" hangs down. You move the cursor back up by pressing k. Left is h (its left of the "j"), and right is l (its right of the "k").

^ and 0 will put the cursor at the beginning of the line, and $ will place it at the end.

(^ and $ are commmonly used in regular expressions to match begin and end of the line. Regular expressions are very powerfull and are commonly used in *nix environment, so maybe it is a little bit tricky now, but later you will notice "the idea" behind most of the key mappings. Other commands also use ^ and $ to move/do something from cursor to begin or end of the line).

Multiple Characters

To advance a word, press the w key. W will include more characters in what it thinks is a word. To go back a word, b is used. Once again, B will include more characters in what vim considers a word. To advance to the end of a word, use e. If you haven't guessed it, E includes more characters to be a word.

To advance to the beginning of a sentence, ( will get the job done. ) will do the opposite, moving to the end of a sentence. For an even bigger jump, { will move the the begining a whole paragraph. } will advance to the end of a whole paragraph.

To advance to the header (top) of the screen, H will get the job done. M will advance to the middle of the screen, and L will advance to the last (bottom).

The repetition department of the repetition department of the...

Here's an awesome thing: if you press a number before a command, then that command will be executed that number of times over (there are exceptions, but they still make sense, like the s command). For example, pressing 3i then Help ! will print "Help! Help! Help!". Pressing 2} will advance you two paragraphs. This comes in handy with the next few commands...

Deleting

The x command will delete the character under the cursor. X will delete the character before the cursor. This is where those number functions get fun. 6x will delete 6 characters. Pressing . (dot) will repeat the previous command. So, lets say you have the word foobar in a few places, but after thinking about it, you'd like there just to be "foo". Move the cursor under the b, hit 3x, move to to the next foo bar and hit . (dot). BAM!

The d will tell Vim that you want to delete something. After pressing "d", you need to tell Vim what to delete. Here you can use the movement commands. dW will delete up to the next word. d^ will delete up unto the beginning of the line. Prefacing the delete command with a number works well too: 3dW will delete the next three words. D (uppercase) is a shortcut to delete until the end of the line (basically d$). Pressing dd will delete the whole line.

Undo and Redo

vim has a built in cutboard. Actions and be undone and again redone. Use u to undo and ctrl + r to redo.

Advanced Editing

Pressing s will erase the current letter under the cursor, and place you in edit mode. S will erase the whole line, and place you in edit mode. Pressing 5s will erase 5 letters and place you in edit mode.

Pressing v will put you in visual mode . Here you can move around to select text, when you're done, you press y to yank the text into the buffer (copy), or you may use c to cut. p pastes after the cursor, P pastes before. V, Visual Line mode, is the same for entire lines. c^v is for blocks of text.

Note: Whenever you delete something, that something is placed inside your buffer and is available for pasting.

Search and replace

To search for a word or character in the file, simply use / and then the characters your are searching for and press enter. Eg. /myword. To view the next match in the search press n.

To search and replace use the substitute :s/ command. The syntax is: [range]s/<find pattern>/<substitute>]/[arguments]

Some examples:

CommandOutcome
:s/xxx/yyy/Replace xxx with yyy at the first occurence
:s/xxx/yyy/gReplace xxx with yyy global (whole row)
:s/xxx/yyy/gcReplace xxx with yyy global with confirm
:%s/xxx/yyy/gReplace xxx with yyy global (whole line) in the whole file

You can use the global :g/ command to search for patterns and execute a command for each hit. The syntax is: :[range]:g/<pattern>/[cmd]

Some examples:

CommandOutcome
:g/^#/dDelete all lines that begins with #
:g/^$/dDelete all lines that are empty

To replace the current word. Place the cursor on the word and execute the command cw. This will delete the word and change the mode to "input".

Other things

Vim will auto indent. This can be annoying when you have to paste something that contains a space or tab at the beginning of the line. In command mode typing :set paste will disable this. Typing :set nopaste will reenable it.

Depending on your setup, Vim may also insist on breaking lines you add to existing files at seventy-eight characters. This is also annoying, and it can be corrected by adding the line autocmd BufNewFile,BufRead * set tw=0 to the file .vimrc in your home folder, creating this file if it doesn't exist.

Saving and Quitting

Write a file with :w <filename>. The <filename> part is optional. Quitting done with :q. If you choose not to save your changes, use :q! or ZQ. To save and quit, use :wq or :x or ZZ.

gVim

Using Tabs

Some user may want to use multiple tabs through terminal. Here is my tip (replace gvim1 with anything you want):

alias gvim='gvim -p --servername gvim1 --remote-tab'

Integration with other Software

set editor="gvim '+set ft=mail' -f"
set printexpr=system('kprinter'\ .\ '\ '\ .\ v:fname_in)\ .\ delete(v:fname_in)\ +\ v:shell_error

Tips and Tricks

Activate a Language Dictionary (Vim 7)

:mkspell! ca ca_ES
move ca.utf-8.spl /usr/share/vim/vimfiles/spell/ca.utf-8.spl
:setlocal spell spelllang=ca

Adapted from Felipe Gil Castiņeira

Automatically leave insert mode after X milliseconds of inaction

This is very handy if you have wrist problems, or you change often from insert to normal mode (taken from this page). Insert these lines in your ~.vimrc

au CursorHoldI * stopinsert
au InsertEnter * let updaterestore=&updatetime | set updatetime=2000
au InsertLeave * let &updatetime=updaterestore

Conclusion

You now know how to use Vim to do slightly more than you could do in a simple editor. As your knowledge of Vim grows, you will be able to use it highly efficiently and do amazing things with text files. And most importantly, you'll feel right at home playing nethack.

See also

Cheat sheet

Graphical cheat sheet based tutorial

Retrieved from "http://www.gentoo-wiki.info/Vim"

Last modified: Fri, 12 Sep 2008 23:26:00 +1000 Hits: 36,512

Created by NickStallman.net, Luxury Homes Australia
Real estate agents should list their apartments, townhouses and units in Australia.