Vim PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Vim - ArchWiki https://wiki.archlinux.org/index.

php/vim

Vim
From ArchWiki

Vim is a terminal text editor, an extended version of vi with additional Related articles
features. To primarily help with editing source code some of Vim's added
features include syntax highlighting, a comprehensive help system, native List of
scripting (vimscript), a visual mode for text selection, and comparison of les applications/Documents#Vi_text_
(vimdi).

Vim's focus is keyboard-centric and is not a simple text editor like nano or picoit requires time to learn,
and a lifetime to master.

Contents
1 Installation
2 Usage
2.1 Basic editing
2.2 Moving around
2.3 Repeating commands
2.4 Deleting
2.5 Undo and redo
2.6 Visual mode
2.7 Search and replace
2.8 Saving, quitting, and opening
2.9 Additional commands
3 Conguration
3.1 Syntax highlighting
3.2 Visual wrapping
3.3 Using the mouse
3.4 Traverse line breaks with arrow keys
3.5 Example ~/.vimrc
4 Merging les
5 Tips and recommendations
5.1 Help system
5.2 Line numbers
5.3 Spell checking
5.4 Save cursor position
5.5 Replace vi command with Vim
5.6 DOS/Windows carriage returns
5.7 Empty space at the bottom of gVim windows
6 Plugins
6.1 Installation
6.1.1 Using a plugin manager
6.1.2 From Arch repositories
6.2 cscope
6.3 Taglist
7 See also
7.1 Oicial
7.2 Tutorials
7.2.1 Videos
7.2.2 Games
7.3 Example congurations
7.4 Other

Installation
Install one of the following CLI packages:

vim-minimal (https://www.archlinux.org/packages/?name=vim-minimal) a lightweight version.


vim (https://www.archlinux.org/packages/?name=vim) Python 2, Lua and Ruby interpreters support but

1 di 9 13/01/2016 00:13
Vim - ArchWiki https://wiki.archlinux.org/index.php/vim

without GTK/X support.


vim-python3 (https://www.archlinux.org/packages/?name=vim-python3) the same as the vim package above but
with Python 3 interpreter support.

... or one of the following GUI packages:

gvim (https://www.archlinux.org/packages/?name=gvim) which also provides the same as the above vim
package.
gvim-python3 (https://www.archlinux.org/packages/?name=gvim-python3) for the same as the gvim package
above but with Python 3 interpreter support.

Note:

The CLI Vim packages are built with no X.org display server support; these builds are missing the
+clipboard feature so Vim will not be able to operate with the X.org primary and clipboard buers.
For X.org clipboard support install a GUI package will also install the CLI version with this
extended feature.
The unoicial repository herecura-stable also provides a number of Vim/gVim variants: vim-cli ,
vim-gvim-common , vim-gvim-gtk , vim-gvim-qt , vim-rt and vim-tiny .

Usage
This is a basic overview on how to use Vim. Alternately, running vimtutor/gvimtutor will launch the
embedded tutorial.

Vim has four dierent modes:

Command mode: keystrokes are interpreted as commands.


Insert mode: keystrokes are entered into the text.
Visual mode: keystrokes select, cut, or copy text.
Ex mode: input mode for additional commands (e.g. saving a le, replacing text and so on).

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 diicult learning curve, but once you get
started, you will nd that it is extremely powerful. Also, all commands are case sensitive. Sometimes
the uppercase versions are "blunter" versions (i.e. s will replace a character, while S will replace a
whole line). Other times they are completely dierent commands ( j will move down, while J will join
two lines).

You insert text (stick it before the cursor) with the i command. Uppercase I inserts text at the
beginning of the line. You append text with a . Typing A will place the cursor at the end of the line. And
you return to command mode at any time by pressing Esc .

Moving around

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.

To move down press j; to move the cursor back up press k. Left is h, 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

2 di 9 13/01/2016 00:13
Vim - ArchWiki https://wiki.archlinux.org/index.php/vim

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 le, G will go to the
end of the le.

Repeating commands

If a command is prexed 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 and hit 3x . Now move to the b symbol of 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. Use s to replace a single letter and go into insert mode.
S will erase the whole line and place you in insert mode. To just replace a single letter use r , R will put
you in replace mode.

Undo and redo

Vim has a built-in clipboard (also known as a buer). 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 buer (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 buer and is available for
pasting.

Search and replace

To search for a word or character in the le, 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 (http://vim.wikia.com/wiki/Search_and_replace) use the substitute :s/ command.


The syntax is: [range]s///[arguments] . For example:

3 di 9 13/01/2016 00:13
Vim - ArchWiki https://wiki.archlinux.org/index.php/vim

Command Outcome
:s/xxx/yyy/ Replace xxx with yyy at the first occurrence
:s/xxx/yyy/g Replace xxx with yyy at every occurrence in the current line
:s/xxx/yyy/gc Replace xxx with yyy at every occurrence in the current line, with confirm
:%s/xxx/yyy/g Replace xxx with yyy global in the whole file
:#,#s/xxx/yyy/g Replace xxx with yyy line number range

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, quitting, and opening

To save and/or quit, you will need to use Ex mode. Ex mode commands are preceded by a : . To write a
le use :w or if you wish to create a new le :w filename . Quitting is done with :q . If you choose not to
save your changes, use :q! . To save and quit type :x . To open a new le use :e filename , type just :e to
reload the le.

Additional commands

1. ddwill delete the current line ( ddp for quick line switching).
2. ccwill delete the current line and place you in insert mode.
3. 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.
4. yy will yank an entire line to the buer
5. * will highlight the current word and n will search it

Conguration
Vim's user-specic conguration le is located in the home directory: ~/.vimrc , and Vim les of current
user are located inside ~/.vim/ . The global conguration le is located at /etc/vimrc . Global Vim les are
located inside /usr/share/vim/ .

To get some commonly expected behaviors (such as syntax highlighting), add the Vim example
conguration to /etc/vimrc :

/etc/vimrc/

...

runtime! vimrc_example.vim

Syntax highlighting

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

:filetype plugin on
:syntax on

Visual wrapping

The wrap option is on by default, which instructs Vim to wrap lines longer than the width of the window,
so that the rest of the line is displayed on the next line. The wrap option only aects how text is displayed,
the text itself is not modied.

The wrapping normally occurs after the last character that ts the window, even when it is in the middle
of a word. More intelligent wrapping can be controlled with the linebreak option. When it is enabled with
set linebreak , the wrapping occurs after characters listed in the breakat string option, which by default
contains a space and some punctuation marks (see :help breakat ).

Wrapped lines are normally displayed at the beginning of the next line, regardless of any indentation.

4 di 9 13/01/2016 00:13
Vim - ArchWiki https://wiki.archlinux.org/index.php/vim

The breakindent (https://retracile.net/wiki/VimBreakIndent) option instructs Vim to take indentation into


account when wrapping long lines, so that the wrapped lines keep the same indentation of the previously
displayed line. The behaviour of breakindent can be ne-tuned with the breakindentopt option, for example
to shift the wrapped line another four spaces to the right for Python les (see :help breakindentopt for
details):

autocmd FileType python set breakindentopt=shift:4

Using the mouse

Vim has the ability to make use of the mouse, but it only works for certain terminals (on Linux it is xterm
and Linux console with gpm (https://www.archlinux.org/packages/?name=gpm), see Console mouse support for
details).

To enable this feature, add this line into ~/.vimrc :

set mouse=a

Note:

This even works in PuTTY over SSH.


In PuTTY, the normal highlight/copy behavior is changed because Vim enters visual mode when the
mouse is used. To select text with the mouse normally, hold down the Shift key while selecting
text.

Traverse line breaks with arrow keys

By default, pressing at the beginning of a line, or pressing at the end of a line, will not let the cursor
traverse to the previous, or following, line.

The default behavior can be changed by adding set whichwrap=b,s,<,>,[,] to your ~/.vimrc le.

Example ~/.vimrc

See Vim/.vimrc.

Merging les
Vim includes a di editor (a program that shows dierences between two or more les and aids to
conveniently merge them). Use vimdi to run the di editor just specify some couple of les to it:
vimdiff file1 file2 . Here is the list of vimdi-specic commands. For basic vim editing, read the tutorial in
the #Usage section.

Action Shortcut
next change ]c

previous change [c

di obtain do

di put dp

fold open zo

fold close zc

rescan les :diffupdate

switch windows Ctrl+w+w

Tips and recommendations


Some tips and recommendations that may help a user with Vim's workow.

Help system

5 di 9 13/01/2016 00:13
Vim - ArchWiki https://wiki.archlinux.org/index.php/vim

Vim includes a broad help system that can be accessed by typing :h or :h subject ; subjects include basic
usage to conguration options. Other subjects that are highlighted and can be jumped to by placing the
cursor on them and typing Ctrl-] , use Ctrl-T to go back. Type :q to close the help window.

Line numbers

Show line number column with :set number ; show relative line number column with :set relativenumber ;
and jump to a line number with :line number or line numbergg .

Spell checking

Vim has the ability to do spell checking, enable by entering:

set spell

By default, only English language dictionaries are installed. More dictionaries can be found in the oicial
repositories by searching for vim-spell . Additional dictionaries can be found in the Vim's FTP archive
(http://ftp.vim.org/vim/runtime/spell/). Additional dictionaries can be put in the folder ~/.vim/spell/ and
enabled with the command: :setlocal spell spelllang=en_us (replacing the en_us with the name of the
needed dictionary).

Action Shortcut
next spelling ]s

previous spelling [s

spelling suggestions z=

spelling good, add zg

spelling good, session zG

spelling wrong, add zw

spelling wrong, session zW

spelling repeat all in le :spellr

Tip:

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 le types (e.g. .txt) by using the FileType plugin and a
custom rule for le type detection. To enable spell checking for any le ending with .txt, create the
le /usr/share/vim/vimfiles/ftdetect/plaintext.vim , and insert the line
autocmd BufRead,BufNewFile *.txt setfiletype plaintext into that le. Next, insert the line
autocmd FileType plaintext setlocal spell spelllang=en_us into your ~/.vimrc or /etc/vimrc , and then
restart Vim.
To enable spell checking for LaTeX (or TeX) documents only, add
autocmd FileType tex setlocal spell spelllang=en_us into your ~/.vimrc or /etc/vimrc , and then restart
Vim.

Save cursor position

If you want the cursor to appear in its previous position (http://vim.wikia.com


/wiki/Restore_cursor_to_le_position_in_previous_editing_session) after you open a le, add the following
to your ~/.vimrc :

augroup resCur
autocmd!
autocmd BufReadPost * call setpos(".", getpos("'\""))
augroup END

Replace vi command with Vim

Create an alias for vi to vim .

6 di 9 13/01/2016 00:13
Vim - ArchWiki https://wiki.archlinux.org/index.php/vim

DOS/Windows carriage returns

If there is a ^M at the end of each line then this means you are editing a text le which was created in
MS-DOS or Windows. This is because in Linux only a single line feed character (LF) used for line break,
but in Windows/MS DOS systems they are using a sequence of a carriage return (CR) and a line feed (LF)
for the same. And this carriage returns are displayed as ^M .

To remove all carriage returns from a le do:

:%s/^M//g

Note that there ^ is a control letter. To enter the control sequence ^M press Ctrl+v,Ctrl+m .

Alternatively install the package dos2unix (https://www.archlinux.org/packages/?name=dos2unix) and run


dos2unix file to x the le.

Empty space at the bottom of gVim windows

When using a window manager congured to ignore window size hints, gVim will ll 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. Put the following
line in ~/.vimrc :

set guiheadroom=0

Note: If you set it to zero, you will not be able to see the bottom horizontal scrollbar.

Plugins
Adding plugins to Vim can increase your productivity. Plugins can alter Vim's UI, add new commands,
code completion support, integrate other programs and utilities with Vim, add support for additional
languages and more.

Installation

Using a plugin manager

A plugin manager allows to install and manage Vim plugins in a similar way independently on which
platform you are running Vim. It is a plugin that acts as a package manager for other Vim plugins.

Vundle (https://github.com/gmarik/Vundle.vim) is currently the most popular plugin manager for


Vim.
Vim-plug (https://github.com/junegunn/vim-plug) is a minimalist Vim plugin manager with many
features like on-demand plugin loading and parallel updating.
pathogen.vim (https://github.com/tpope/vim-pathogen) is a simple plugin for managing Vim's
runtimepath.

From Arch repositories

The group provides many various plugins. Use


vim-plugins (https://www.archlinux.org/groups/x86_64/vim-plugins/)
pacman -Sg vim-plugins command to list available packages which you can then install with pacman.

cscope

Cscope (http://cscope.sourceforge.net/) is a tool for browsing a project. By navigating to a word/symbol


/function and calling cscope (usually with shortcut keys) it can nd: functions calling the function, the
function denition, and more.

Install the cscope (https://www.archlinux.org/packages/?name=cscope) package.

Copy the cscope default le where it will be automatically read by Vim:

7 di 9 13/01/2016 00:13
Vim - ArchWiki https://wiki.archlinux.org/index.php/vim

mkdir -p ~/.vim/plugin
wget -P ~/.vim/plugin http://cscope.sourceforge.net/cscope_maps.vim

Note: You will probably need to uncomment these lines in ~/.vim/plugin/cscope_maps.vim in order to enable
cscope shortcuts in Vim 7.x:

set timeoutlen=4000
set ttimeout

Create a le which contains the list of les you wish cscope to index (cscope can handle many languages
but this example nds .c, .cpp and .h les, specic for C/C++ project):

cd /path/to/project/dir
find . -type f -print | grep -E '\.(c(pp)?|h)$' > cscope.files

Create database les that cscope will read:

cscope -bq

Note: You must browse your project les from this location or set and export the $CSCOPE_DB variable,
pointing it to the cscope.out le.

Default keyboard shortcuts:

Ctrl-\ and
c: Find functions calling this function
d: Find functions called by this function
e: Find this egrep pattern
f: Find this file
g: Find this definition
i: Find files #including this file
s: Find this C symbol
t: Find assignments to

Feel free to change the shortcuts.

#Maps ctrl-c to find functions calling the function


nnoremap <C-c> :cs find c <C-R>=expand("<cword>")<CR><CR>

Taglist

Taglist (http://vim-taglist.sourceforge.net/) provides an overview of the structure of source code les and
allows you to eiciently browse through source code les in dierent programming languages.

Install the vim-taglist (https://www.archlinux.org/packages/?name=vim-taglist) package.

Useful options to be put in ~/.vimrc :

let Tlist_Compact_Format = 1
let Tlist_GainFocus_On_ToggleOpen = 1
let Tlist_Close_On_Select = 1
nnoremap <C-l> :TlistToggle<CR>

See also
Oicial

Homepage (http://www.vim.org/)
Documentation (http://vimdoc.sourceforge.net/)
Vim Wiki (http://vim.wikia.com)

Tutorials

vim Tutorial and Primer (http://www.danielmiessler.com/study/vim/)

8 di 9 13/01/2016 00:13
Vim - ArchWiki https://wiki.archlinux.org/index.php/vim

vi Tutorial and Reference Guide (http://usalug.org/vi.html)


Graphical vi-Vim Cheat Sheet and Tutorial (http://www.viemu.com
/a_vi_vim_graphical_cheat_sheet_tutorial.html)
Vim Introduction and Tutorial (http://blog.interlinked.org/tutorials/vim_tutorial.html)
Open Vim (http://www.openvim.com/) collection of Vim learning tools
Learn Vim Progressively (http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/)
know vim (http://www.knowvim.com/) [dead link 2014-10-29]
Learning Vim in 2014 (http://benmccormick.org/learning-vim-in-2014/)

Videos

Vimcasts (http://vimcasts.org/) screencasts in .ogg format.


Vim Tutorial Videos (http://derekwyatt.org/vim/tutorials/) covering the basics up to advanced
topics.

Games

Vim Adventures (http://vim-adventures.com/)


VimGolf (http://vimgolf.com/)

Example congurations

nion's (http://nion.modprobe.de/setup/vimrc)
A detailed conguration from Amir Salihefendic (http://amix.dk/vim/vimrc.html)
Bart Trojanowski (http://www.jukie.net/~bart/conf/vimrc)
Steve Francia's Vim Distribution (https://github.com/spf13/spf13-vim)
W4RH4WK's Vim conguration (https://github.com/W4RH4WK/dotVim)
Fast vimrc/colorscheme from askapache (http://www.askapache.com/linux/fast-vimrc.html)
Basic vimrc (https://gist.github.com/anonymous/c966c0757f62b451ba)

Other

HOWTO Vim (http://www.gentoo-wiki.info/HOWTO_VIM) Gentoo wiki article which this article


was based on (author unknown).
Vivify (http://byteuent.com/vivify/) a colorscheme editor for Vim
Usevim (http://www.usevim.com/) frequently updated blog highlighting plugins, tips, etc
Vim Awesome (http://vimawesome.com/) a website that lists and ranks Vim plugins by popularity
among GitHub users.
Basic Vim Tips (http://bencrowder.net/les/vim-fu/) a reference chart for Vim, primarily aimed at
beginners.
Vim colorscheme tailoring (https://linuxtidbits.wordpress.com/2014/10/14/vim-customize-installed-
colorschemes/) override installed colorscheme to try-out or permanently alter.

Retrieved from "https://wiki.archlinux.org/index.php?title=Vim&oldid=411348"

Categories: Development Text editors

This page was last modied on 9 December 2015, at 19:49.


Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.

9 di 9 13/01/2016 00:13

You might also like