Difference between revisions of "Vim"

From ArchWiki
Jump to: navigation, search
(Substitute on Lines)
(Substitute on Lines)
Line 288: Line 288:
===Substitute on Lines===
===Substitute on Lines===
To only substitute on certain lines:
To only substitute between certain lines:
For example, to replace instances of 'one' with 'two' on lines 3&4, one would execute:
For example, to replace instances of 'one' with 'two' between lines 3&4, one would execute:

Revision as of 18:46, 13 May 2010

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.

Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어

External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Summary help replacing me
A basic tutorial, and tips to setup the console/terminal text editor.

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.

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 begin to understand the reasoning.


  • Vim is very powerful for advanced editing tasks
  • Extensible configuration options
  • Simple, robust keybindings
  • Syntax highlighting


Install the commandline version:

# pacman -S vim

Get the GUI (optional):

# pacman -S gvim


This is a basic overview on how to use vim. Alternately, you could use Template:Codeline to further familiarize yourself. Vim has four different modes:

  • Command mode: keystrokes are interpreted as commands.
  • Insert mode: keystrokes are entered into the file.
  • Visual mode: keystrokes select, cut, or copy text
  • Ex mode: input mode for additional commands (e.g. saving a file, replacing text...)

Basic Editing

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.

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).

You insert text (stick it before the cursor) with the i command. I (uppercase i) inserts text 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

In vim, you can move the cursor with the arrow keys, but this isn't the vim way. You’d have to move your right hand all the way from the standard typing position all the way to the arrow keys, and then back. Not fun.

In vim 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 (it's left of the “j”), and right is l (lowercase L).

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

Note: ^ and $ are commmonly used in regular expressions to match the beginning and ending 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 the use of most of these key mappings.

To advance a word, press the w key. W will include more characters in what it thinks is a word (- dashes,u _ underscores...). 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, E includes more characters.

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). gg will go to the begininng of the file, G will go to the end of the file.

Repeating commands

If a command is prefixed by a number, 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! ” then Esc will print “Help! Help! Help!“. Pressing 2} will advance you two paragraphs. This comes in handy with the next few commands…


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 the next foo bar and hit . (dot).

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.

To delete then replace the current word, place the cursor on the word and execute the command cw. This will delete the word and change to insert mode. To replace only a single letter use r.

Undo and Redo

vim has a built-in clipboard (also known as a buffer). Actions can be undone with u and redone with Ctrl+r.

Visual 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. Ctrl-v is for blocks of text.

Note: Whenever you delete something, that something is placed inside a 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. To view the next match in the search press n.

To search and replace use the substitute :s/ command. The syntax is: Template:Codeline. For example:

Command        Outcome
:s/xxx/yyy/    Replace xxx with yyy at the first occurence
:s/xxx/yyy/g   Replace xxx with yyy first occurrence, global (whole sentence)
:s/xxx/yyy/gc  Replace xxx with yyy global with confirm
:%s/xxx/yyy/g  Replace xxx with yyy global in the whole file

You can use the global :g/ command to search for patterns and then execute a command for each match. The syntax is: Template:Codeline.

Command  Outcome
:g/^#/d  Delete all lines that begins with #
:g/^$/d  Delete all lines that are empty

Saving and Quitting

To save and/or quit, you will need to use Ex mode. Ex mode commands are preceded by a :. To write a file use :w or if the file doesn’t have a name :w filename. Quitting is done with :q. If you choose not to save your changes, use :q!. To save and quit :x.

Additional Commands

  1. Pressing s will erase the current letter under the cursor, and place you in insert mode. S will erase the whole line, and place you in insert mode.
  2. o will create a newline below the line and put you insert mode, O will create a newline above the line and put you in insert mode.
  3. yy will yank an entire line
  4. cc will replace the current line with a newline
  5. * will highlight the current word and n will search it


Vim's personal configuration file is located in the home directory: Template:Filename. Advanced users tend to keep a well-tailored Template:Filename. The global configuration file is located at Template:Filename.

Currently the vim global configuration in Arch Linux is very basic and differs from many other distributions' default vim configuration file. To get some commonly expected behaviors (like syntax highlighting, return to the line of the last edit...), consider using vim's example configuration file:

cp /etc/vimrc /etc/vimrc.bak
cp /usr/share/vim/vim72/vimrc_example.vim /etc/vimrc

Backup Files

Vim by default creates a backup of an edited file in the same directory as the file called Template:Filename. To prevent clutter, many users tell vim to use a backup directory:

set backupdir=~/.vim/backup,/tmp

Or, it's possible to even disable this behaviour:

set nobackup
set nowritebackup
set noswapfile    ! (additionally disable swap files)

Wrap Searches

set wrapscan

Check Spelling

set spell

Vim will now highlight incorrectly spelled words. With the cursor on a mis-spelled word, use z= to view suggested corrections.

Only English language dictionaries are installed by default, more can be found at the vim ftp archive. After downloading the spl files for the given language (e.g. hu.cp1250.spl, hu.iso-8859-2.spl and/or hu.utf-8.spl for Hungarian) into Template:Filename, run:

:setlocal spell spelllang=LL

To activate spell checking for the selected language (i.e. Template:Codeline for the above mentioned example).

Syntax Highlighting

To enable syntax highlighting (vim supports a huge list of programming languages):

filetype plugin on
syntax on

Example .vimrc

An example Template:Filename covering common options.


Some more examples can be found in #Resources.

Merging Files (Vimdiff)

Vim includes a diff editor (a program that can merge differences between two files). Begin with Template:Codeline; to use:

]c :        - next difference
[c :        - previous difference
Ctrl+w +w   - switch windows
do          - diff obtain
dp          - diff put
zo          - open folded text
zc          - close folded text
:diffupdate - re-scan the files for differences

Vim Tips

Specific user tricks to accomplish tasks.

Line Numbers

  1. Show line numbers by Template:Codeline.
  2. Jump to line number Template:Codeline.

Substitute on Lines

To only substitute between certain lines:


For example, to replace instances of 'one' with 'two' between lines 3&4, one would execute:





Example configurations


  • HOWTO Vim -- Gentoo wiki article which this article was based on (author unknown).