https://wiki.archlinux.org/api.php?action=feedcontributions&user=Audrius&feedformat=atomArchWiki - User contributions [en]2024-03-28T20:37:42ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Vim&diff=271244Vim2013-08-15T09:02:58Z<p>Audrius: /* Configuration */ vim version change</p>
<hr />
<div>[[Category:Development]]<br />
[[Category:Text editors]]<br />
[[es:Vim]]<br />
[[de:Vim]]<br />
[[it:Vim]]<br />
[[lt:Vim]]<br />
[[ru:Vim]]<br />
[[zh-CN:Vim]]<br />
[[zh-TW:Vim]]<br />
''"[http://www.vim.org/about.php 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."'' <br />
<br />
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.<br />
<br />
== Installation ==<br />
<br />
[[pacman|Install]] the command line version with the {{Pkg|vim}} package, or you can install the GUI version (which also provides {{ic|vim}}) by installing the {{Pkg|gvim}} package.<br />
<br />
{{Note|The {{Pkg|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 {{Pkg|gvim}} package instead (it includes the {{ic|vim}} binary as well). The {{ic|herecura-stable}} unofficial repository also provides a couple different Vim / gVim variants:<br />
{{hc|$ pacman -Slq herecura-stable &#124; grep vim|<br />
vim-cli<br />
vim-gvim-gtk<br />
vim-gvim-motif<br />
vim-gvim-qt<br />
vim-gvim-x11<br />
vim-rt<br />
vim-tiny<br />
}}<br />
}}<br />
<br />
{{Note|There are some visualization problems in KDE using {{Pkg|gvim}} from official repositories. In that case you can install {{ic|vim-gvim-qt}} from {{ic|herecura-stable}} or {{AUR|vim-qt}}<br />
}}<br />
==Usage==<br />
<br />
This is a basic overview on how to use Vim. Alternately, running {{Ic|vimtutor}} or {{Ic|gvimtutor}} will launch vim's tutorial, which takes about 25-30 minutes.<br />
<br />
Vim has four different modes:<br />
<br />
* Command mode: keystrokes are interpreted as commands.<br />
* Insert mode: keystrokes are entered into the file.<br />
* Visual mode: keystrokes select, cut, or copy text<br />
* Ex mode: input mode for additional commands (e.g. saving a file, replacing text...)<br />
<br />
===Basic editing===<br />
<br />
If you start Vim with:<br />
<br />
$ vim somefile.txt<br />
<br />
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.<br />
<br />
{{Note|Vim is an example of classic Unix-style ware. It has a steep 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 ({{keypress|s}} will replace a character, {{keypress|S}} will replace a line), other times they are completely different commands ({{keypress|j}} will move down, {{keypress|J}} will join two lines).}}<br />
<br />
You insert text (stick it before the cursor) with the {{keypress|i}} command. {{keypress|I}} (uppercase '''i''') inserts text at the beginning of the line. You append text (place text after the cursor, what most people expect) with {{keypress|a}}. Typing {{keypress|A}} will place the cursor at the end of the line.<br />
<br />
Return to command mode at any time by pressing {{keypress|Esc}}.<br />
<br />
===Moving around===<br />
<br />
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.<br />
<br />
In Vim you can move down by pressing {{keypress|j}}. You can remember this because the “j” hangs down. You move the cursor back up by pressing {{keypress|k}}. Left is {{keypress|h}} (it's left of the “j”), and right is {{keypress|l}} (lowercase '''L''').<br />
<br />
{{keypress|^}} will put the cursor at the beginning of the line, and {{keypress|$}} will place it at the end.<br />
<br />
{{Note|{{keypress|^}} and {{keypress|$}} 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.}}<br />
<br />
To advance a word, press the {{keypress|w}} key. {{keypress|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, {{keypress|b}} is used. Once again, {{keypress|B}} will include more characters in what Vim considers a word. To advance to the end of a word, use {{keypress|e}}, {{keypress|E}} includes more characters.<br />
<br />
To advance to the beginning of a sentence, {{keypress|(}} will get the job done. {{keypress|)}} will do the opposite, moving to the end of a sentence. For an even bigger jump, {{keypress|{}} will move the the beginning a whole paragraph. {{keypress|<nowiki>}</nowiki>}} will advance to the end of a whole paragraph.<br />
<br />
To advance to the header (top) of the screen, {{keypress|H}} will get the job done. {{keypress|M}} will advance to the middle of the screen, and {{keypress|L}} will advance to the last (bottom). {{keypress|gg}} will go to the beginning of the file, {{keypress|G}} will go to the end of the file. {{keypress|Ctrl+D}} will let you scroll page by page.<br />
<br />
===Repeating commands===<br />
<br />
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 {{keypress|s}} command). For example, pressing {{keypress|3i}} then “Help! ” then {{keypress|Esc}} will print “Help! Help! Help!“. Pressing {{keypress|<nowiki>2}</nowiki>}} will advance you two paragraphs. This comes in handy with the next few commands…<br />
<br />
===Deleting===<br />
<br />
The {{keypress|x}} command will delete the character under the cursor. {{keypress|X}} will delete the character before the cursor. This is where those number functions get fun. {{keypress|6x}} will delete 6 characters. Pressing {{keypress|.}} (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 {{keypress|3x}}, move to the next "foobar" and hit {{keypress|.}} (dot).<br />
<br />
The {{keypress|d}} will tell Vim that you want to delete something. After pressing {{keypress|d}}, you need to tell Vim what to delete. Here you can use the movement commands. {{keypress|dW}} will delete up to the next word. {{keypress|d^}} will delete up unto the beginning of the line. Prefacing the delete command with a number works well too: {{keypress|3dW}} will delete the next three words. {{keypress|D}} (uppercase) is a shortcut to delete until the end of the line (basically {{keypress|d$}}). Pressing {{keypress|dd}} will delete the whole line.<br />
<br />
To delete then replace the current word, place the cursor on the word and execute the command {{keypress|cw}}. This will delete the word and change to insert mode. To replace only a single letter use {{keypress|r}}.<br />
<br />
===Undo and redo===<br />
<br />
Vim has a built-in clipboard (also known as a buffer). Actions can be undone with {{keypress|u}} and redone with {{keypress|Ctrl+r}}.<br />
<br />
===Visual mode===<br />
<br />
Pressing {{keypress|v}} will put you in visual mode . Here you can move around to select text, when you’re done, you press {{keypress|y}} to yank the text into the buffer (copy), or you may use {{keypress|c}} to cut. {{keypress|p}} pastes after the cursor, {{keypress|P}} pastes before. {{keypress|V}}, Visual Line mode, is the same for entire lines. {{keypress|Ctrl+v}} is for blocks of text.<br />
<br />
{{Note|Whenever you delete something, that something is placed inside a buffer and is available for pasting.}}<br />
<br />
===Search and replace===<br />
<br />
To search for a word or character in the file, simply use {{keypress|/}} and then the characters your are searching for and press enter. To view the next match in the search press {{keypress|n}}, press {{keypress|N}} for the previous match.<br />
<br />
To search and replace use the substitute {{keypress|:s/}} command. The syntax is: {{Ic|[range]s///[arguments]}}. For example:<br />
<br />
{{bc|<br />
Command Outcome<br />
:s/xxx/yyy/ Replace xxx with yyy at the first occurence<br />
:s/xxx/yyy/g Replace xxx with yyy first occurrence, global (whole sentence)<br />
:s/xxx/yyy/gc Replace xxx with yyy global with confirm<br />
:%s/xxx/yyy/g Replace xxx with yyy global in the whole file<br />
}}<br />
<br />
You can use the global {{keypress|:g/}} command to search for patterns and then execute a command for each match. The syntax is: {{Ic|[range]:g//[cmd]}}.<br />
<br />
{{bc|<br />
Command Outcome<br />
:g/^#/d Delete all lines that begins with #<br />
:g/^$/d Delete all lines that are empty<br />
}}<br />
<br />
===Saving and quitting===<br />
<br />
To save and/or quit, you will need to use Ex mode. Ex mode commands are preceded by a {{keypress|:}}. To write a file use {{keypress|:w}} or if the file doesn’t have a name {{ic|''':w''' filename}}. Quitting is done with {{keypress|:q}}. If you choose not to save your changes, use {{keypress|:q!}}. To save and quit {{keypress|:x}}.<br />
<br />
=== Additional commands ===<br />
<br />
# Pressing {{keypress|s}} will erase the current letter under the cursor, and place you in insert mode. {{keypress|S}} will erase the whole line, and place you in insert mode.<br />
# {{keypress|o}} will create a newline below the line and put you insert mode, {{keypress|O}} will create a newline above the line and put you in insert mode.<br />
# {{keypress|yy}} will yank an entire line<br />
# {{keypress|cc}} will delete the current line and place you in insert mode.<br />
# {{keypress|*}} will highlight the current word and {{keypress|n}} will search it<br />
<br />
==Configuration==<br />
<br />
Vim's user-specific configuration file is located in the home directory: {{ic|~/.vimrc}}, and files are located inside {{ic|~/.vim/}} The global configuration file is located at {{ic|/etc/vimrc}}. Global files are located inside {{ic|/usr/share/vim/}}.<br />
<br />
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:<br />
<br />
# mv /etc/vimrc /etc/vimrc.bak<br />
# cp /usr/share/vim/vim74/vimrc_example.vim /etc/vimrc<br />
<br />
===Wrap searches===<br />
<br />
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. <br />
<br />
set wrapscan<br />
<br />
=== Spell checking ===<br />
<br />
set spell<br />
<br />
With this setting, Vim will highlight incorrectly spelled words. Place the cursor on a misspelled word and enter {{keypress|1=z=}} to view spelling suggestions.<br />
<br />
Only English language dictionaries are installed by default, more can be found in the [[Official Repositories|official repositories]]. To get the list of available languages type:<br />
<br />
# pacman -Ss vim-spell<br />
<br />
Language dictionaries can also be found at the [http://ftp.vim.org/vim/runtime/spell/ Vim FTP archive]. Put the downloaded dictionar(y/ies) into the {{ic|~/.vim/spell}} folder and set the dictionary by typing: {{ic|1=:setlocal spell spelllang=LL}}<br />
<br />
{{Tip|<br />
* 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. For spell checking of languages other than English, simply replace {{ic|en_us}} with the value appropriate for your language.<br />
* 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.<br />
* 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 {{ic|*.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.}}<br />
<br />
===Syntax highlighting===<br />
<br />
To enable syntax highlighting (Vim supports a huge list of programming languages):<br />
<br />
:filetype plugin on<br />
:syntax on<br />
<br />
===Using the mouse===<br />
<br />
Vim has the ability to make use of the mouse, but requires xterm's mouse reporting feature.<br />
<br />
# See the example .vimrc below to enable the mouse.<br />
# Use xterm. In your console: {{ic|1=export TERM=xterm-256color}} or {{ic|1=export TERM=xterm}}<br />
<br />
Notes:<br />
* This even works in PuTTY over SSH.<br />
* In PuTTY, the normal highlight/copy behaviour is changed because Vim enters visual mode when the mouse is used. To select text with the mouse normally, hold down the {{keypress|Shift}} key while selecting text.<br />
<br />
===Traverse line breaks with arrow keys===<br />
<br />
By default, pressing {{keypress|←}} at the beginning of a line, or pressing {{keypress|→}} at the end of a line, will not let the cursor traverse to the previous, or following, line.<br />
<br />
The default behavior can be changed by adding {{ic|1=set whichwrap=b,s,<,>,[,]}} to your {{ic|~/.vimrc}} file.<br />
<br />
=== Example ~/.vimrc ===<br />
<br />
An example [[Vim/.vimrc|Vim configuration]].<br />
<br />
==Merging files (vimdiff)==<br />
<br />
Vim includes a diff editor (a program that can merge differences between two files). vimdiff will open colored windows each showing the content of the file with colored highlights of the differences, line by line. You are left with two modes: the insert one, which let you edit the file, and the screen mode, which let you move around windows and lines. Begin by running {{Ic|vimdiff file1 file2}}. Some example commands are found below:<br />
<br />
;{{keypress|]c}} : next difference<br />
;{{keypress|[c}} : previous difference<br />
;{{keypress|Ctrl+w+w}} : switch windows<br />
;{{keypress|i}} : enter Insert mode<br />
;{{keypress|Esc}} : exit Insert mode<br />
;{{keypress|p}} : paste a line<br />
;{{keypress|do}} : diff obtain. when cursor is on a highlighted difference and changes from other window will move into the current one<br />
;{{keypress|dp}} : diff put. same as diff obtain but will put the changes from current windows into the other one<br />
;{{keypress|zo}} : open folded text<br />
;{{keypress|zc}} : close folded text<br />
;{{keypress|<nowiki>:</nowiki>diffupdate}} : re-scan the files for differences<br />
;{{keypress|yy}} : copy a line<br />
;{{keypress|:wq}} : save and exit the current window<br />
;{{keypress|:wqa}} : save and exit both windows<br />
;{{keypress|:q!}} : exit without saving<br />
<br />
Once your file has been correctly edited taking account changes in file.pacnew:<br />
# mv file file.bck<br />
# mv file.pacnew file<br />
Check if your new file is correct, then remove your backup:<br />
# rm file.bck<br />
<br />
==Vim tips==<br />
<br />
Specific user tricks to accomplish tasks.<br />
<br />
===Line numbers===<br />
<br />
# Show line numbers by {{Ic|:set number}}.<br />
# Jump to line number {{Ic|:<line number>}}.<br />
<br />
===Substitute on lines===<br />
<br />
To only substitute between certain lines:<br />
<br />
:''n'',''n''s/one/two/g<br />
<br />
For example, to replace instances of 'one' with 'two' between lines 3 and 4, one would execute:<br />
<br />
:3,4s/one/two/g<br />
<br />
===Make Vim restore cursor position in files===<br />
<br />
If you want the cursor to appear in its previous position after you open a file, add the following to your {{ic|~/.vimrc}}:<br />
<br />
{{bc|<nowiki><br />
if has("autocmd")<br />
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif<br />
endif<br />
</nowiki>}}<br />
<br />
See also [http://vim.wikia.com/wiki/Restore_cursor_to_file_position_in_previous_editing_session this] tip in Vim Wiki.<br />
<br />
===Empty space at the bottom of gVim windows===<br />
When using a [[window manager]] configured to ignore window size hints, gVim will fill the non-functional area with the GTK theme background color. <br />
<br />
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 {{ic|~/.vimrc}}:<br />
<br />
set guiheadroom=0<br />
<br />
===Replace vi command with vim===<br />
<br />
Create an [[Bash#Aliases|alias]] for {{ic|vi}} to {{ic|vim}}.<br />
<br />
==Troubleshooting==<br />
<br />
==="^M"===<br />
There is a "^M" at the end of each line. This usually happens when you are editing a text file which was created in MS-DOS or Windows.<br />
<br />
Solution:<br />
Replace all "^M" using the command:<br />
<br />
{{bc|:%s/^M//g}}<br />
<br />
Pay attention, "^" is the control letter, press {{Keypress|Ctrl+Q}} to get the right "^".<br />
<br />
Alternatively, install the package {{pkg|dos2unix}} from the official repositories, and run {{ic|dos2unix <file name here>}}.<br />
<br />
==See also==<br />
<br />
===Official===<br />
* [http://www.vim.org/ Homepage]<br />
* [http://vimdoc.sourceforge.net/ Documentation]<br />
* [http://vim.wikia.com Tips Wiki]<br />
<br />
===Tutorials===<br />
* [http://usalug.org/vi.html vi Tutorial and Reference Guide]<br />
* [http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html Graphical vi-Vim Cheat Sheet and Tutorial]<br />
* [http://blog.interlinked.org/tutorials/vim_tutorial.html Vim Introduction and Tutorial]<br />
* [http://www.openvim.com/ Open Vim] - Collection of Vim learning tools<br />
* [http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/ Learn Vim Progressively]<br />
* [http://www.knowvim.com/ know vim]<br />
<br />
====Videos====<br />
* [http://vimcasts.org/ Vimcasts] - Screencasts in .ogg format.<br />
* [http://www.derekwyatt.org/vim/vim-tutorial-videos/vim-novice-tutorial-videos/ Tutorial Videos] - Covering the basics up to advanced topics.<br />
<br />
====Games====<br />
* [http://vim-adventures.com/ Vim Adventures]<br />
* [http://vimgolf.com/ VimGolf]<br />
<br />
===Example configurations===<br />
* [http://nion.modprobe.de/setup/vimrc nion's]<br />
* [http://amix.dk/vim/vimrc.html A detailed configuration from Amir Salihefendic]<br />
* [http://www.jukie.net/~bart/conf/vimrc Bart Trojanowski]<br />
* [https://github.com/spf13/spf13-vim Steve Francia's Vim Distribution]<br />
* [https://github.com/W4RH4WK/dotVim W4RH4WK's Vim configuration]<br />
<br />
===Other===<br />
* [http://www.gentoo-wiki.info/HOWTO_VIM HOWTO Vim] - Gentoo wiki article which this article was based on (author unknown).<br />
* [http://bytefluent.com/vivify/ Vivify] - A ColorScheme Editor for Vim</div>Audrius