Vim is a terminal text editor. It is an extended version of vi with additional features, including syntax highlighting, a comprehensive help system, native scripting (vimscript), a visual mode for text selection, and comparison of files (vimdiff).
- 1 Installation
- 2 Usage
- 3 Configuration
- 4 Merging files
- 5 Tips and tricks
- 6 Plugins
- 7 See also
Install one of the following packages:
- — with Python 2/3, Lua, Ruby and Perl interpreters support but without GTK/X support.
vimpackage with GTK/X support. — which also provides the same as the above
- The Xorg support; specifically the
+clipboardfeature is missing, so Vim will not be able to operate with the primary and clipboard selection buffers. The package provides also the CLI version of Vim with the
package is built without
- The unofficial repository herecura also provides a number of Vim/gVim variants:
For a basic overview on how to use Vim, follow the vim tutorial by running either vimtutor (for the terminal version) or gvimtutor (for the graphical version).
Vim includes a broad help system that can be accessed with the
:h subject command. Subjects include commands, configuration options, key bindings, plugins etc. Use the
:h command (without any subject) for information about the help system and jumping between subjects.
Vim's user-specific configuration file is located in the home directory:
~/.vimrc, and Vim files of current user are located inside
~/.vim/. The global configuration file is located at
/etc/vimrc. Global Vim files are located inside
defaults.vim, which is loaded when no
~/.vimrcis present. Add
/etc/vimrcto disable loading of
Vim commands such as
:paste operate with the unnamed register, which by default corresponds to the
"* register. If the
+clipboard feature is available, the
"* register is reflected to the
PRIMARY buffer in X.
To change the default register, you can
:set clipboard=unnamedplus to use the
"+ register instead. The
"+ register corresponds to the
CLIPBOARD buffer in X.
For more information, see
To enable syntax highlighting for many programming languages:
:filetype plugin on :syntax on
The indent file for specific file types can be loaded with:
:filetype indent on
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 affects how text is displayed, the text itself is not modified.
The wrapping normally occurs after the last character that fits 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
Wrapped lines are normally displayed at the beginning of the next line, regardless of any indentation. The breakindent 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 fine-tuned with the
breakindentopt option, for example to shift the wrapped line another four spaces to the right for Python files (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:
- xterm/urxvt-based terminal emulators
- Linux console with Console mouse support for details) (see
To enable this feature, add this line into
mouse=a option is set in
"*register if there is access to an X server, see the #Clipboard section. The xterm handling of the mouse buttons can still be used by keeping the
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
Vim includes a diff editor (a program that shows differences between two or more files and aids to conveniently merge them). Use vimdiff to run the diff editor — just specify some couple of files to it:
vimdiff file1 file2. Here is the list of vimdiff-specific commands.
Tips and tricks
To show the line number column, use
:set number. By default absolute line numbers are shown, relative numbers can be enabled with
Jumping to a specific line is possible with
:line number or
line numbergg. Jumps are remembered in a jump list, see
:h jump-motions for details.
Vim has the ability to do spell checking, enable by entering:
By default, only English language dictionaries are installed. More dictionaries can be found in the official repositories by searching for
vim-spell. Additional dictionaries can be found in the Vim's FTP archive. 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).
|spelling good, add|
|spelling good, session|
|spelling wrong, add|
|spelling wrong, session|
|spelling repeat all in file|
- To enable spelling in two languages (for instance English and German), add
set spelllang=en,deinto your
/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 with .txt, create the file
/usr/share/vim/vimfiles/ftdetect/plaintext.vim, and insert the line
autocmd BufRead,BufNewFile *.txt setfiletype plaintextinto that file. Next, insert the line
autocmd FileType plaintext setlocal spell spelllang=en_usinto your
/etc/vimrc, and then restart Vim. Alternatively, one can simply insert the line
autocmd BufRead,BufNewFile *.txt setlocal spellinto their
/etc/vimrc, and then restart Vim. Be sure to edit this line (specifically
*.txt) to include the filetype(s) intended for spell checking.
- To enable spell checking for LaTeX (or TeX) documents only, add
autocmd FileType tex setlocal spell spelllang=en_usinto your
/etc/vimrc, and then restart Vim.
Save the previous runtime state
vim and resuming work again at a later time makes you lose a lot of information. Making up for that, before starting again where you left off, can be both time-consuming and tedious. If you ever want to start a vim session based on a previous runtime state, mainly two possibilities exist:
- Session files can be used to save the states of any number of particular sessions over time. One distinct session file may be used for each session or project of your interest. For that modality to be available, the version of
vimyou installed must have been compiled with the
- Within a session,
:mksession[!] [my_session_name.vim]will write a vim-script to my_session_name.vim, and by default, if you choose not to provide a file name, to file Session.vim in the current directory. The optional
!will clobber a pre-existing session file with the same name and path.
- To restore a
vimsession, just issue either:
vim -S [my_session_name.vim]
- respectively from command line interface (some terminal) or in ex-mode from an already opened session buffer.
- Exactly what is saved and additional details on session files options are extensively covered in the vim documentation. Commented examples are found here.
viminfofile may also be used to store command line history, search string history, input-line history, registers' content, marks for files, location marks within files, last search/substitute pattern (to be used in search mode with `n` and `&` within the session), buffer list, and any global variables you may have defined.
- Note that the
viminfofile is independent from any session file you may have already created, or plan to create. For the
viminfomodality to be available, the version of
vimyou installed must have been compiled with the
Configure what is kept in your
viminfo file, by adding (for example) the following to your ~/.vimrc file:
- where each parameter is preceded by an identifier:
'q : q, number of edited file remembered \"m : m, number of lines saved for each register :p : p, number of history cmd lines remembered % : saves and restore the buffer list n...: fully qualified path to the
.viminfofile -- this is a literal "n".
- Please refer to the official viminfo documentation for particulars on how a pre-existing
viminfofile is modified as it is updated with current session information, say from several buffers in the current session you are in the process of exiting.
Save cursor position
If you want the cursor to appear in its previous position after you re-open a file, add the following to your
function! ResCur() " Restore session with cursor in last position inside line if line("'\"") > 1 && line("'\"") <= line('$') normal! g`" return 1 endif endfunction if has('folding') function! UnfoldCur() if !&foldenable return endif let cl = line('.') if cl <= 1 return endif let cf = foldlevel(cl) let uf = foldlevel(cl - 1) let min = (cf > uf ? uf : cf) if min execute 'normal!' min . 'zo' return 1 endif endfunction endif
The above snippet will take care of whether:
- - folds exist(ed) when you last exited your previous file edition session
- - the re-opened file only contains one line
For those not (yet) familiar with vim scripting, the third line of the above snippet specifies that the cursor position will be exactly restored to its previous letter position, upon re-opening a file. Make
g'" to restore cursor position to the last visited line instead. In that case the cursor will be positioned at line begin.
augroup resCur autocmd! if has('folding') autocmd BufWinEnter * if ResCur() | call UnfoldCur() | endif else autocmd BufWinEnter * call ResCur() endif augroup END
Both topics (autocommands and autogroups) are out of scope in this Wiki section, but can be reviewed in great depth in the official vim documentation. There is also extensive community help available online, should the above example-snippet not perform to your liking or need modification.
Replace vi command with Vim
Create an alias for
Alternatively, if you want to be able to type
sudo vi and get
vim, install AUR which will remove
vi and replace it with a symlink to
DOS/Windows carriage returns
If there is a
^M at the end of each line then this means you are editing a text file 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
To remove all carriage returns from a file do:
Note that there
^ is a control letter. To enter the control sequence
Alternatively install the package
dos2unix file to fix the file.
set ff=unixto convert files with DOS/Windows line ending to Unix line ending. To do the reverse, just issue
set ff=dosto convert files with Unix line ending to DOS/Windows line ending.
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. Put the following line in
Vim as a pager
Using scripts Vim can be used as a terminal pager, so that you get various vim features such as color schemes.
Vim comes with the
/usr/share/vim/vim81/macros/less.sh script, for which you can create an alias.
Alternatively there is also the export the
PAGER environment variable.
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.
Using the built-in package manager
Vim 8 added the possibility to load natively third-party plugins. It is possible to use this functionality by storing third-party packages in
Using a plugin manager
A plugin manager installs and manages Vim plugins in a similar way independent of which platform on you are running Vim. It is a plugin that acts as a package manager for other Vim plugins.
- Vundle is currently the most popular plugin manager for Vim.
- Vim-plug is a minimalist Vim plugin manager with many features like on-demand plugin loading and parallel updating.
- pathogen.vim is a simple plugin for managing Vim's runtimepath.
- Dein.vim is a plugin manager replacing NeoBundle, available as AUR.
From Arch repositories
pacman -Sg vim-plugins command to list available packages which you can then install with pacman.
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.
Install the package.
Copy the cscope default file where it will be automatically read by Vim:
mkdir -p ~/.vim/plugin wget -P ~/.vim/plugin http://cscope.sourceforge.net/cscope_maps.vim
~/.vim/plugin/cscope_maps.vimin order to enable cscope shortcuts in Vim 7.x:
set timeoutlen=4000 set ttimeout
Create a file which contains the list of files you wish cscope to index (cscope can handle many languages but this example finds .c, .cpp and .h files, specific for C/C++ project):
cd /path/to/project/dir find . -type f -print | grep -E '\.(c(pp)?|h)$' > cscope.files
Create database files that cscope will read:
$CSCOPE_DBvariable, pointing it to the
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 provides an overview of the structure of source code files and allows you to efficiently browse through source code files in different programming languages.
Install the package.
Useful options to be put in
let Tlist_Compact_Format = 1 let Tlist_GainFocus_On_ToggleOpen = 1 let Tlist_Close_On_Select = 1 nnoremap <C-l> :TlistToggle<CR>
- vim Tutorial and Primer
- vi Tutorial and Reference Guide
- Graphical vi-Vim Cheat Sheet and Tutorial
- Vim Introduction and Tutorial
- Open Vim — collection of Vim learning tools
- Learn Vim Progressively
- Learning Vim in 2014
- Seven habits of effective text editing
- Basic Vim Tips
- HOWTO Vim
- Vimcasts — screencasts in .ogg format.
- Vim Tutorial Videos — covering the basics up to advanced topics.
- A detailed configuration from Amir Salihefendic
- Bart Trojanowski
- Steve Francia's Vim Distribution
- Vim Awesome - Vim Plugins
- W4RH4WK's Vim configuration
- Fast vimrc/colorscheme from askapache
- Basic vimrc