zh-CN:Vimzh-TW:Vim"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 focuses on keyboard usage, and offers useful features such as syntax highlighting and scripting capabilities. 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.

Installation

Install the command line version with the vim package, or you can install the GUI version (which also provides vim) by installing the gvim package.

Note:

The vim package is meant to be as lightweight as possible; hence, it does not support Python, Lua, and Ruby interpreters, nor does it support X server options (this means that it will not support copy and paste from the X clipboard). If you require these options, install the gvim package instead (it includes the vim binary as well). The herecura-stable unofficial repository also provides a couple different Vim / gVim variants:

Basic editing

If you start Vim with:

$ vim somefile.txt

you will see a blank document (providing that somefile.txt does not exist. If it does, you will see what is 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. It has a difficult learning curve, but once you get started, you will find that it is extremely powerful. 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 commonly used in regular expressions to match the beginning and ending of the line. Regular expressions are very powerful 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 (e.g. underscores and dashes as a part of 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, 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 beginning 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 beginning of the file, G will go to the end of the file. Ctrl+D will let you scroll page by page.

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…

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 to see just “foo”. Move the cursor under the "b", hit 3x, move to the next "foobar" 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, press N for the previous match.

To search and replace use the substitute :s/ command. The syntax is: [range]s///[arguments]. For example:

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

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

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.

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.

yy will yank an entire line

cc will delete the current line and place you in insert mode.

* will highlight the current word and n will search it

Configuration

Vim's user-specific configuration file is located in the home directory: ~/.vimrc, and files are located inside ~/.vim/ The global configuration file is located at /etc/vimrc. Global files are located inside /usr/share/vim/.

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 (such as syntax highlighting, returning to the last known cursor position), consider using Vim's example configuration file:

Wrap searches

With this option the search next behaviour allows to jump to the beginning of the file, when the end of file is reached. Similarly, search previous jumps to the end of the file when the start is reached.

set wrapscan

Spell checking

set spell

With this setting, Vim will highlight incorrectly spelled words. Place the cursor on a misspelled word and enter z= to view spelling suggestions.

Only English language dictionaries are installed by default, more can be found in the official repositories. To get the list of available languages type:

# pacman -Ss vim-spell

Language dictionaries can also be found at the Vim FTP archive. Put the downloaded dictionar(y/ies) into the ~/.vim/spell folder and set the dictionary by typing: :setlocal spell spelllang=LL

To enable spelling in two languages (for instance English and German), add set spelllang=en,de into your ~/.vimrc or /etc/vimrc, and then restart Vim.

You can enable spell checking for arbitrary file types (e.g. *.txt) by using the FileType plugin and a custom rule for file type detection. To enable spell checking for any file ending in *.txt, create the file /usr/share/vim/vimfiles/ftdetect/plaintext.vim, and insert the line autocmd BufRead,BufNewFile *.txt setfiletype plaintext into that file. Next, insert the line autocmd FileType plaintext setlocal spell spelllang=en_us into your ~/.vimrc or /etc/vimrc, and then restart Vim.

Example ~/.vimrc

Plugins

Adding plugins to vim can increase your productivity. The group vim-plugins has many plugins to choose from(there are more in the repos though ie: vim-supertab).

pacman -Ss vim-plugins

cscope

Cscope is a tool for browsing a project. By navigating to a word/symbol/function and calling cscope(usually with shortcut keys) it can find: functions calling the function, the function definition, and more. Multiple steps are required to search a code base.

Merging files (vimdiff)

Vim includes a diff editor, a program that aids the merging of differences between two (or more, with limited usefulness) files. vimdiff opens a horizontally multi-paned view that colorfully highlights differences, each pane containing one of the files to be examined/edited. Vim has several modes, two important ones being Insert mode, which lets text be edited, and Command mode, which lets the cursor be moved move across windows and lines. Begin by running vimdiff file1 file2. Some example commands follow.

]c

next difference

[c

previous difference

Ctrl+w+w

switch windows

i

enter Insert mode

Esc

exit Insert mode

p

paste

do

diff obtain. When the cursor is on a (highlighted) difference, copies the changes from the other window to the current one.

dp

diff put. Inverse of diff obtain; copies the changes from current windows to the other one.

zo

open folded text

zc

close folded text

:diffupdate

re-scan the files for differences

yy

copy a line

dd

cut a line

:wq

save and exit the current window

:wqa

save and exit both windows

:q!

exit without saving

Once your file has been correctly edited, taking into account changes in file.pacnew:

# mv file file.bck
# mv file.pacnew file

Check whether your new file is correct, then remove your backup:

# rm file.bck

Vim tips

Specific user tricks to accomplish tasks.

Line numbers

Show line numbers by :set number.

Jump to line number :<line number>.

Substitute on lines

To only substitute between certain lines:

:n,ns/one/two/g

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

:3,4s/one/two/g

Make Vim restore cursor position in files

If you want the cursor to appear in its previous position after you open a file, add the following to your ~/.vimrc:

Empty space at the bottom of gVim windows

When using a window manager configured to ignore window size hints, gVim will fill the non-functional area with the GTK theme background color.

The solution is to adjust how much space gVim reserves at the bottom of the window. Take note that if you set it to zero, you won't be able to see the bottom horizontal scrollbar, if you have one. Put the following line in ~/.vimrc: