Difference between revisions of "Vim"

From ArchWiki
Jump to: navigation, search
(512 intermediate revisions by more than 100 users not shown)
Line 1: Line 1:
[[Category:Development (English)]]
+
[[Category:Text editors]]
[[Category:Utilities (English)]]
+
[[de:Vim]]
[[Category:General (English)]]
+
[[es:Vim]]
{{i18n_links_start}}
+
[[it:Vim]]
{{i18n_entry|English|Vim}}
+
[[ja:Vim]]
{{i18n_entry|Italiano|Vim_(Italiano)}}
+
[[lt:Vim]]
{{i18n_entry|Русский|Vim_(Русский)}}
+
[[ru:Vim]]
{{i18n_links_end}}
+
[[zh-hans:Vim]]
 +
[[zh-hant:Vim]]
 +
{{Related articles start}}
 +
{{Related|List of applications/Documents#Vi text editors}}
 +
{{Related articles end}}
  
Vim ('''Vi''' I'''M'''Proved), is a text editor derived from Vi. It's notoriously known for its steep learning curve, and user unfriendly interface. However, because of it's efficiently, variety of plugins, and customization options vim is one of the most popular text editors for programmers and *nix users (along with [[Emacs]]*). A graphical version gVim which provides a user with menus is also available.
+
[[Wikipedia:Vim (text editor)|Vim]] is a terminal text editor. It is an extended version of [[Wikipedia:vi|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).
 
<nowiki>*</nowiki>''Note that The Cult of Vi has determined that using emacs may cause dry mouth, blurred vision, drowsiness, dizziness, profuse sweating, tremors, sexual problems, headache, nausea and abdominal pain.''
 
  
==Installation==
+
== Installation ==
* <tt>vim</tt> depends on <tt>vi</tt>, so that must be installed first. However, since vi is part of the base installation most users should already have it.
 
  
<pre>
+
[[Install]] one of the following packages:
pacman -S vim
 
</pre>
 
  
==Configuration==
+
* {{Pkg|vim}} — with Python 2/3, Lua, Ruby and Perl interpreters support but without GTK/X support.
vim's configuation file is in a user's home directy (~/) and is named .vimrc. A sample .vimrc can be found in /etc/vimrc.
+
* {{Pkg|gvim}} — which also provides the same as the above {{ic|vim}} package with GTK/X support.
  
<pre>
+
{{Note|
"Sample .vimrc
+
* The {{Pkg|vim}} package is built without [[Xorg]] support; specifically the {{ic|+clipboard}} feature is missing, so Vim will not be able to operate with the ''primary'' and ''clipboard'' [[Clipboard|selection buffers]]. The {{Pkg|gvim}} package provides also the CLI version of Vim with the {{ic|+clipboard}} feature.
set nocompatible
+
* The unofficial repository [[Unofficial user repositories#herecura|herecura]] also provides a number of Vim/gVim variants: {{ic|vim-cli}}, {{ic|vim-gvim-common}}, {{ic|vim-gvim-gtk}}, {{ic|vim-gvim-qt}}, {{ic|vim-rt}} and {{ic|vim-tiny}}.
set showmatch
+
}}
set incsearch
 
set ignorecase 
 
set smartcase
 
set history=100
 
set backspace=eol,start,indent
 
set ruler
 
set tabstop=4
 
set shiftwidth=4
 
set expandtab
 
set virtualedit=all
 
set background=dark
 
set vb t_vg=
 
set mouse=v
 
set textwidth=79
 
set formatoptions=tcrq
 
</pre>
 
  
* To install gvim (Just like vim but with gtk2 interface and listens to /etc/gvimrc and ~/.gvimrc)
+
== Usage ==
<pre>
 
pacman -S gvim
 
</pre>
 
  
* There is also a group of selected popular vim scripts available via pacman
+
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).
<pre>
 
pacman -S vim-plugins
 
</pre>
 
  
==VIM quick start guide==
+
Vim includes a broad help system that can be accessed with the {{ic|:h ''subject''}} command. Subjects include commands, configuration options, key bindings, plugins etc. Use the {{ic|:h}} command (without any subject) for information about the help system and jumping between subjects.
  
 +
== Configuration ==
  
===How to start vim===
+
Vim's user-specific configuration file is located in the home directory: {{ic|~/.vimrc}}, and Vim files of current user are located inside {{ic|~/.vim/}}. The global configuration file is located at {{ic|/etc/vimrc}}. Global Vim files are located inside {{ic|/usr/share/vim/}}.
  
* to start vim and edit a file (new or existing)
+
{{Note|Commonly expected behavior such as syntax highlighting is enabled in {{ic|defaults.vim}}, which is loaded when no {{ic|~/.vimrc}} is present. Add {{ic|1=let skip_defaults_vim=1}} to {{ic|/etc/vimrc}} to disable loading of {{ic|defaults.vim}} completely. [https://github.com/vim/vim/issues/1033]}}
  
vim file_name
+
=== Clipboard ===
* to start vim and open a new file
 
vim
 
  
(You may name your file later when you save it.)
+
Vim commands such as {{ic|:yank}} or {{ic|:paste}} operate with the unnamed register, which by default corresponds to the {{ic|"*}} register. If the {{ic|+clipboard}} feature is available, the {{ic|"*}} register is reflected to the {{ic|PRIMARY}} buffer in X.
  
===How to enter text===
+
To change the default register, you can {{ic|1=:set clipboard=unnamedplus}} to use the {{ic|"+}} register instead. The {{ic|"+}} register corresponds to the {{ic|CLIPBOARD}} buffer in X.
  
If this is one of the first few times you use vim, perhaps you're wondering why you cannot type in anything. This is because you're in command mode. In command mode, your key strokes are interpreted as editing commands. To begin typing new text into the file, you need to switch to insert mode by press i. Then you can see each character you type appears on the screen.
+
For more information, see {{ic|:help 'clipboard'}}.
  
The two basic modes of vim are.
+
{{Tip|Custom shortcuts for copy and paste operations can be created. See e.g. [http://superuser.com/a/189198] for binding {{ic|ctrl+c}}, {{ic|ctrl+v}} and {{ic|ctrl+x}}.}}
  
* Insert mode, in which anything you type (except some special keys) will appear on the screen and become part of your file buffer
+
=== Syntax highlighting ===
  
* Command mode, in which your key strokes are interpreted as commands.
+
To enable syntax highlighting (Vim supports a huge list of programming languages):
  
After you start vim, you're in command mode.
+
:filetype plugin on
 +
:syntax on
  
*Switch between modes
+
=== Visual wrapping ===
  
1. From command mode to insert mode, press
+
The {{ic|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 {{ic|wrap}} option only affects how text is displayed, the text itself is not modified.
  
i
+
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 {{ic|linebreak}} option. When it is enabled with {{ic|set linebreak}}, the wrapping occurs after characters listed in the {{ic|breakat}} string option, which by default contains a space and some punctuation marks (see {{ic|:help breakat}}).
  
2. from insert mode to command mode, press ESC
+
Wrapped lines are normally displayed at the beginning of the next line, regardless of any indentation. The [https://retracile.net/wiki/VimBreakIndent 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 {{ic|breakindent}} can be fine-tuned with the {{ic|breakindentopt}} option, for example to shift the wrapped line another four spaces to the right for Python files (see {{ic|:help breakindentopt}} for details):
  
===How to move the cursor===
+
autocmd FileType python set breakindentopt=shift:4
  
In both the command mode and the insert mode, you may always use the arrow keys to move the cursor, and with gvim you can mouse click to get to a new position. However, this is not the vim way. If you do that, you're not using vim, you're using notepad. If you want to call yourself a vi guy, forget about the arrow keys, backspace, delete, insert, ... and of course, the mouse. The most effective way of moving the cursor is first go to the command mode by pressing ESC and then use vim's cursor-moving commands to move around. The 4 basic commands are
+
=== Using the mouse ===
  
* j move down one line
+
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 {{Pkg|gpm}} (see [[Console mouse support]] for details)
 +
* [[PuTTY]]
  
* k move up one line
+
To enable this feature, add this line into {{ic|~/.vimrc}}:
  
* h move left one character
+
set mouse=a
  
* l move right one character
+
The {{ic|1=mouse=a}} option is set in {{ic|defaults.vim}}.
  
Remember: these commands work only in command mode. At first you may feel a bit uncomfortable. After you get familiar using these commands you will stick to them and forget the arrow keys.
+
{{Note|Copy/paste will use the {{ic|"*}} 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 {{ic|shift key}} pressed.}}
  
===How to delete text===
+
=== Traverse line breaks with arrow keys ===
  
First I will tell you that the DELETE key always works, and the BACKSPACE key works with the newly typed text in the insert mode. However, I suggest you do not use them. Instead, force yourself to use vim's deletion commands.
+
By default, pressing {{ic|←}} at the beginning of a line, or pressing {{ic|→}} at the end of a line, will not let the cursor traverse to the previous, or following, line.
  
1. Make sure you are in command mode by press ESC
+
The default behavior can be changed by adding {{ic|1=set whichwrap=b,s,<,>,[,]}} to your {{ic|~/.vimrc}} file.
  
2. move the cursor to the character you want to delete
+
== Merging files ==
  
3. press x , this character disappears
+
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: {{ic|vimdiff ''file1'' ''file2''}}. Here is the list of ''vimdiff''-specific commands.
  
x is just one of the many powerful deletion commands. Usually you need to move the cursor to the character you want to delete and then press x to remove it. Remember using the cursor motion commands j k h l to locate your target, and don't leave the command mode.
+
{| class="wikitable"
 +
! Action          !! Shortcut
 +
|-
 +
| next change    || {{ic|]c}}
 +
|-
 +
| previous change || {{ic|[c}}
 +
|-
 +
| diff obtain    || {{ic|do}}
 +
|-
 +
| diff put        || {{ic|dp}}
 +
|-
 +
| fold open      || {{ic|zo}}
 +
|-
 +
| fold close      || {{ic|zc}}
 +
|-
 +
| rescan files    || {{ic|:diffupdate}}
 +
|}
  
===How to insert text===
+
== Tips and tricks ==
  
1. In command mode, move the cursor to the desired location
+
=== Line numbers ===
  
2. press i, then you are in insert mode and can type in whatever you like.
+
To show the line number column, use {{ic|:set number}}. By default absolute line numbers are shown, relative numbers can be enabled with {{ic|:set relativenumber}}.
  
===How to COPY, CUT and PASTE===
+
Jumping to a specific line is possible with {{ic|:''line number''}} or {{ic|''line number''gg}}. Jumps are remembered in a jump list, see {{ic|:h jump-motions}} for details.
  
If you run the GUI version of vim, gvim, you can use mouse and the pull-down menus to do that---the same fashion with other editors. However, that is not the preferred style. You'll feel better off if you can live without a mouse.
+
=== Spell checking ===
  
1. Enter the command mode
+
Vim has the ability to do spell checking, enable by entering:
by pressing ESC
 
  
2. Move the cursor to the line which you want to make a copy, by pressing j or k
+
set spell
  
3. press
+
By default, only English language dictionaries are installed. More dictionaries can be found in the [[official repositories]] by searching for {{ic|vim-spell}}. Additional dictionaries can be found in the [http://ftp.vim.org/vim/runtime/spell/ Vim's FTP archive]. Additional dictionaries can be put in the folder {{ic|~/.vim/spell/}} and enabled with the command: {{ic|1=:setlocal spell spelllang=''en_us''}} (replacing the {{ic|''en_us''}} with the name of the needed dictionary).
  
yy
+
{| class="wikitable"
 +
! Action                      !! Shortcut
 +
|-
 +
| next spelling              || {{ic|]s}}
 +
|-
 +
| previous spelling          || {{ic|[s}}
 +
|-
 +
| spelling suggestions        || {{ic|1=z=}}
 +
|-
 +
| spelling good, add          || {{ic|zg}}
 +
|-
 +
| spelling good, session      || {{ic|zG}}
 +
|-
 +
| spelling wrong, add        || {{ic|zw}}
 +
|-
 +
| spelling wrong, session    || {{ic|zW}}
 +
|-
 +
| spelling repeat all in file || {{ic|:spellr}}
 +
|}
  
to make a copy of the line, or
+
{{Tip|
 +
* To enable spelling in two languages (for instance English and German), add {{ic|1=set spelllang=''en,de''}} into your {{ic|~/.vimrc}} or {{ic|/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 {{ic|/usr/share/vim/vimfiles/ftdetect/plaintext.vim}}, and insert the line {{ic|autocmd BufRead,BufNewFile *.txt setfiletype plaintext}} into that file. Next, insert the line {{ic|1=autocmd FileType plaintext setlocal spell spelllang=''en_us''}} into your {{ic|~/.vimrc}} or {{ic|/etc/vimrc}}, and then restart Vim.
 +
* To enable spell checking for LaTeX (or TeX) documents only, add {{ic|1=autocmd FileType '''tex''' setlocal spell spelllang=''en_us''}} into your {{ic|~/.vimrc}} or {{ic|/etc/vimrc}}, and then restart Vim.}}
  
dd
+
=== Save cursor position ===
  
to cut it and make a copy
+
If you want the cursor to [http://vim.wikia.com/wiki/Restore_cursor_to_file_position_in_previous_editing_session appear in its previous position] after you open a file, add the following to your {{ic|~/.vimrc}}:
  
4. now move cursor (by pressing k or j) to the the location where you want to put this copy
+
augroup resCur
 +
  autocmd!
 +
  autocmd BufReadPost * call setpos(".", getpos("'\""))
 +
augroup END
  
5. press
+
=== Replace vi command with Vim ===
  
p
+
Create an [[alias]] for {{ic|vi}} to {{ic|vim}}.
  
to put the buffer after current line, or
+
Alternatively, if you want to be able to type {{ic|sudo vi}} and get {{ic|vim}}, install {{AUR|vi-vim-symlink}} which will remove {{ic|vi}} and replace it with a symlink to {{ic|vim}}.
  
P
+
=== DOS/Windows carriage returns ===
  
to put the buffer before current line
+
If there is a {{ic|^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 {{ic|^M}}.
  
If you want to copy or cut several lines, put a number before the yy or dd command, like
+
To remove all carriage returns from a file do:
  
  8yy
+
  :%s/^M//g
  
to copy 8 lines.
+
Note that there {{ic|^}} is a control letter. To enter the control sequence {{ic|^M}} press {{ic|Ctrl+v,Ctrl+m}}.
  
===How to search for a word===
+
Alternatively install the package {{pkg|dos2unix}} and run {{ic|dos2unix ''file''}} to fix the file.
Suppose you want to find all the words apple in your file
 
  
1. Make sure you are in command mode
+
{{Note| Another simple way is by changing {{ic|fileformat}} setting. {{ic|<nowiki>set ff=unix</nowiki>}} to convert files with DOS/Windows line ending to Unix line ending. To do the reverse, just issue {{ic|<nowiki>set ff=dos</nowiki>}} to convert files with Unix line ending to DOS/Windows line ending. }}
by hitting ESC
 
  
2. type
+
=== Empty space at the bottom of gVim windows ===
  
/apple
+
When using a [[window manager]] configured to ignore window size hints, gVim will fill the non-functional area with the GTK theme background color.
  
followed by ENTER to find an occurrence of apple. When you type the slash, it and the following characters will be shown on the bottom of the screen. After you press ENTER, the cursor will go to the first occurrence of apple if found, and the target will be highlighted.
+
The solution is to adjust how much space gVim reserves at the bottom of the window. Put the following line in {{ic|~/.vimrc}}:
  
3. after you got the first apple, you can keep typing
+
set guiheadroom=0
  
n
+
{{Note|If you set it to zero, you will not be able to see the bottom horizontal scrollbar.}}
  
to find other apples
+
== Plugins ==
  
===How to substitute text===
+
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.
  
First make sure you're in command mode by pressing ESC.
+
{{Tip|For a list of popular plugins, see [http://vimawesome.com/ Vim Awesome]}}
  
* replace first occurrence of old in current line with new
+
=== Installation ===
  
:s/old/new/
+
==== Using the built-in package manager ====
  
* replace all occurrence of old in current line with new
+
Vim 8 added the possibility to load natively third-party plugins. It is possible to use this functionality by storing third-party packages in {{ic|~/.vim/pack/foo}}.
  
:s/old/new/g
+
==== Using a plugin manager ====
  
* replace the first occurrence of old in each line between line n1 and n2 with new
+
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.
  
:n1,n2s/old/new/
+
* [https://github.com/gmarik/Vundle.vim Vundle] is currently the most popular plugin manager for Vim.
 +
* [https://github.com/junegunn/vim-plug Vim-plug] is a minimalist Vim plugin manager with many features like on-demand plugin loading and parallel updating.
 +
* [https://github.com/tpope/vim-pathogen pathogen.vim] is a simple plugin for managing Vim's runtimepath.
 +
* [https://github.com/Shougo/dein.vim Dein.vim] is a plugin manager replacing [https://github.com/Shougo/neobundle.vim NeoBundle], available as {{AUR|vim-dein-git}}.
  
* replace all occurrence of oldbetween line n1 and n2 with new
+
==== From Arch repositories ====
  
:n1,n2s/old/new/g
+
The {{Grp|vim-plugins}} group provides many various plugins. Use {{ic|pacman -Sg vim-plugins}} command to list available packages which you can then [[install]] with pacman.
  
* replace all occurrence of old in the whole buffer with new, prompt for confirmation.
+
=== cscope ===
  
:1,$s/old/new/gc
+
[http://cscope.sourceforge.net/ 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.
  
* replace all occurrence of old in the whole buffer with new, prompt for confirmation.
+
[[Install]] the {{Pkg|cscope}} package.
  
:%s/old/new/gc
+
Copy the cscope default file where it will be automatically read by Vim:
===How to exit vim===
 
  
* To save and exit: press ESC to enter command mode, then:
+
mkdir -p ~/.vim/plugin
 +
wget -P ~/.vim/plugin http://cscope.sourceforge.net/cscope_maps.vim
  
:wq
+
{{Note|You will probably need to uncomment these lines in {{ic|~/.vim/plugin/cscope_maps.vim}} in order to enable cscope shortcuts in Vim 7.x:
or
+
{{bc|1=
:x
+
set timeoutlen=4000
or
+
set ttimeout
ZZ
+
}}}}
  
* save your file as '''newname''' before you exit: press ESC, then type
+
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):
  
  :wq newname
+
  cd ''/path/to/project/dir''
 +
find . -type f -print | grep -E '\.(c(pp)?|h)$' > cscope.files
  
* Exit without saving, press ESC, then type
+
Create database files that cscope will read:
  
  :q
+
  cscope -bq
  
* Forced quit
+
{{Note|You must browse your project files from this location or set and export the {{ic|$CSCOPE_DB}} variable, pointing it to the {{ic|cscope.out}} file.}}
  
If :q doesn't work, it's probably because you didn't save the change. If you want to save, use :wq. If you don't want to save the changes, type
+
Default keyboard shortcuts:
  
:q!
+
  Ctrl-\ and
==Vim Tutor==
+
      c: Find functions calling this function
To make your very first steps learning vim, just enter
+
      d: Find functions called by this function
<pre>
+
      e: Find this egrep pattern
vimtutor
+
      f: Find this file
</pre>
+
      g: Find this definition
vim will open the tutor file.
+
      i: Find files #including this file
 +
      s: Find this C symbol
 +
      t: Find assignments to
  
 +
Feel free to change the shortcuts.
  
==External links==
+
#Maps ctrl-c to find functions calling the function
* [http://www.vim.org/ The Official website]
+
nnoremap <C-c> :cs find c <C-R>=expand("<cword>")<CR><CR>
* [http://vimdoc.sourceforge.net/ Vim Documentation]
+
 
 +
=== Taglist ===
 +
 
 +
[http://vim-taglist.sourceforge.net/ 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 {{Pkg|vim-taglist}} package.
 +
 
 +
Useful options to be put in {{ic|~/.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 ==
 +
 
 +
=== Official ===
 +
 
 +
* [http://www.vim.org/ Homepage]
 +
* [http://vimdoc.sourceforge.net/ Documentation]
 +
* [http://vim.wikia.com Vim Wiki]
 +
* [http://www.vim.org/scripts/ Vim Scripts]
 +
 
 +
=== Tutorials ===
 +
 
 +
* [https://danielmiessler.com/study/vim/ vim Tutorial and Primer]
 +
* [http://usalug.org/vi.html vi Tutorial and Reference Guide]
 +
* [http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html Graphical vi-Vim Cheat Sheet and Tutorial]
 +
* [http://blog.interlinked.org/tutorials/vim_tutorial.html Vim Introduction and Tutorial]
 +
* [http://www.openvim.com/ Open Vim] — collection of Vim learning tools
 +
* [http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/ Learn Vim Progressively]
 +
* [http://benmccormick.org/learning-vim-in-2014/ Learning Vim in 2014]
 +
* [http://www.moolenaar.net/habits.html Seven habits of effective text editing]
 +
* [http://bencrowder.net/files/vim-fu/ Basic Vim Tips]
 +
* [http://www.gentoo-wiki.info/HOWTO_VIM HOWTO Vim]
 +
 
 +
==== Videos ====
 +
 
 +
* [http://vimcasts.org/ Vimcasts] — screencasts in ''.ogg'' format.
 +
* [http://derekwyatt.org/vim/tutorials/ Vim Tutorial Videos] — covering the basics up to advanced topics.
 +
 
 +
==== Games ====
 +
 
 +
* [http://vim-adventures.com/ Vim Adventures]
 +
* [http://vimgolf.com/ VimGolf]
 +
 
 +
=== Configuration ===
 +
 
 +
* [https://web.archive.org/web/20131020125020/http://nion.modprobe.de/setup/vimrc nion's]
 +
* [http://amix.dk/vim/vimrc.html A detailed configuration from Amir Salihefendic]
 +
* [https://web.archive.org/web/20131004071740/http://www.jukie.net/~bart/conf/vimrc Bart Trojanowski]
 +
* [https://github.com/spf13/spf13-vim Steve Francia's Vim Distribution]
 +
* [http://vimawesome.com/ Vim Awesome] - Vim Plugins
 +
* [https://github.com/W4RH4WK/dotVim W4RH4WK's Vim configuration]
 +
* [https://www.askapache.com/linux/fast-vimrc/ Fast vimrc/colorscheme from askapache]
 +
* [https://gist.github.com/anonymous/c966c0757f62b451bffa Basic vimrc]
 +
* [http://www.usevim.com/ Usevim]
 +
 
 +
==== Colors ====
 +
 
 +
* [http://bytefluent.com/vivify/ Vivify]
 +
* [https://linuxtidbits.wordpress.com/2014/10/14/vim-customize-installed-colorschemes/ Vim colorscheme customization]

Revision as of 07:29, 2 February 2018

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

Installation

Install one of the following packages:

  • vim — with Python 2/3, Lua, Ruby and Perl interpreters support but without GTK/X support.
  • gvim — which also provides the same as the above vim package with GTK/X support.
Note:
  • The vim package is built without Xorg support; specifically the +clipboard feature is missing, so Vim will not be able to operate with the primary and clipboard selection buffers. The gvim package provides also the CLI version of Vim with the +clipboard feature.
  • The unofficial repository herecura 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

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.

Configuration

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 /usr/share/vim/.

Note: Commonly expected behavior such as syntax highlighting is enabled in defaults.vim, which is loaded when no ~/.vimrc is present. Add let skip_defaults_vim=1 to /etc/vimrc to disable loading of defaults.vim completely. [1]

Clipboard

Vim commands such as :yank or :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 :help 'clipboard'.

Tip: Custom shortcuts for copy and paste operations can be created. See e.g. [2] for binding ctrl+c, ctrl+v and ctrl+x.

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 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 :help breakat).

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:

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

set mouse=a

The mouse=a option is set in defaults.vim.

Note: Copy/paste will use the "* 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 shift key pressed.

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

Merging files

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.

Action Shortcut
next change ]c
previous change [c
diff obtain do
diff put dp
fold open zo
fold close zc
rescan files :diffupdate

Tips and tricks

Line numbers

To show the line number column, use :set number. By default absolute line numbers are shown, relative numbers can be enabled with :set relativenumber.

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.

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

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 file :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 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 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.
  • 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 after you open a file, 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.

Alternatively, if you want to be able to type sudo vi and get vim, install vi-vim-symlinkAUR which will remove vi and replace it with a symlink to vim.

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 ^M.

To remove all carriage returns from a file 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 and run dos2unix file to fix the file.

Note: Another simple way is by changing fileformat setting. set ff=unix to convert files with DOS/Windows line ending to Unix line ending. To do the reverse, just issue set ff=dos to convert files with Unix line ending to DOS/Windows line ending.

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

Tip: For a list of popular plugins, see Vim Awesome

Installation

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 ~/.vim/pack/foo.

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 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 vim-dein-gitAUR.

From Arch repositories

The vim-plugins group provides many various plugins. Use pacman -Sg vim-plugins command to list available packages which you can then install with pacman.

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.

Install the cscope 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
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 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 -bq
Note: You must browse your project files from this location or set and export the $CSCOPE_DB variable, pointing it to the cscope.out file.

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

Official

Tutorials

Videos

Games

Configuration

Colors