Difference between revisions of "Vim"
m (Remove extra space. See Help:Style.) |
(→See also: links from the talk page) |
||
Line 285: | Line 285: | ||
* [http://blog.interlinked.org/tutorials/vim_tutorial.html Vim Introduction and Tutorial] | * [http://blog.interlinked.org/tutorials/vim_tutorial.html Vim Introduction and Tutorial] | ||
* [http://vim.runpaint.org/ Vim Recipes] - A free cookbook. | * [http://vim.runpaint.org/ Vim Recipes] - A free cookbook. | ||
+ | * [http://www.openvim.com/ Open Vim] - Collection of Vim learning tools | ||
+ | * [http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/ Learn Vim Progressively] | ||
+ | * [http://www.knowvim.com/ know vim] | ||
====Videos==== | ====Videos==== | ||
* [http://vimcasts.org/ Vimcasts] - Screencasts in .ogg format. | * [http://vimcasts.org/ Vimcasts] - Screencasts in .ogg format. | ||
* [http://www.derekwyatt.org/vim/vim-tutorial-videos/vim-novice-tutorial-videos/ Tutorial Videos] - Covering the basics up to advanced topics. | * [http://www.derekwyatt.org/vim/vim-tutorial-videos/vim-novice-tutorial-videos/ Tutorial Videos] - Covering the basics up to advanced topics. | ||
+ | |||
+ | ====Games==== | ||
+ | * [http://vim-adventures.com/ Vim Adventures] | ||
===Example configurations=== | ===Example configurations=== |
Revision as of 11:35, 26 September 2012
zh-CN:Vim zh-TW:Vim "Vim is an advanced text editor that seeks to provide the power of the de-facto UNIX editor ‘vi’, with a more complete feature set."
Vim focuses on keyboard usage, and offers useful features such as syntax highlighting and scripting capabilities. Vim is not a simple text editor, like nano or pico. It does require some time to learn, and a great amount of time to master.
Contents
Installation
Install the command line version with the vim package, or you can install the GUI version (which also provides vim
) by installing the gvim package.
herecura-stable
unofficial repository provides a couple different vim / gvim variants:
$ pacman -Slq herecura-stable | grep vim
vim-cli vim-gvim-gtk vim-gvim-motif vim-gvim-qt vim-gvim-x11 vim-rt vim-tiny
Usage
This is a basic overview on how to use vim. Alternately, you could use vimtutor
to further familiarize yourself. Vim has four different modes:
- Command mode: keystrokes are interpreted as commands.
- Insert mode: keystrokes are entered into the file.
- Visual mode: keystrokes select, cut, or copy text
- Ex mode: input mode for additional commands (e.g. saving a file, replacing text...)
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.
You insert text (stick it before the cursor) with the Template:Keypress command. Template:Keypress (uppercase i) inserts text at the beginning of the line. You append text (place text after the cursor, what most people expect) with Template:Keypress. Typing Template:Keypress will place the cursor at the end of the line.
Return to command mode at any time by pressing Template:Keypress.
Moving Around
In vim, you can move the cursor with the arrow keys, but this isn't the vim way. You’d have to move your right hand all the way from the standard typing position all the way to the arrow keys, and then back. Not fun.
In vim you can move down by pressing Template:Keypress. You can remember this because the “j” hangs down. You move the cursor back up by pressing Template:Keypress. Left is Template:Keypress (it's left of the “j”), and right is Template:Keypress (lowercase L).
Template:Keypress will put the cursor at the beginning of the line, and Template:Keypress will place it at the end.
To advance a word, press the Template:Keypress key. Template:Keypress 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, Template:Keypress is used. Once again, Template:Keypress will include more characters in what vim considers a word. To advance to the end of a word, use Template:Keypress, Template:Keypress includes more characters.
To advance to the beginning of a sentence, Template:Keypress will get the job done. Template:Keypress will do the opposite, moving to the end of a sentence. For an even bigger jump, Template:Keypress will move the the beginning a whole paragraph. Template:Keypress will advance to the end of a whole paragraph.
To advance to the header (top) of the screen, Template:Keypress will get the job done. Template:Keypress will advance to the middle of the screen, and Template:Keypress will advance to the last (bottom). Template:Keypress will go to the beginning of the file, Template:Keypress will go to the end of the file. Template:Keypress will let you scroll page by page.
Repeating commands
If a command is prefixed by a number, then that command will be executed that number of times over (there are exceptions, but they still make sense, like the Template:Keypress command). For example, pressing Template:Keypress then “Help! ” then Template:Keypress will print “Help! Help! Help!“. Pressing Template:Keypress will advance you two paragraphs. This comes in handy with the next few commands…
Deleting
The Template:Keypress command will delete the character under the cursor. Template:Keypress will delete the character before the cursor. This is where those number functions get fun. Template:Keypress will delete 6 characters. Pressing Template: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 Template:Keypress, move to the next "foobar" and hit Template:Keypress (dot).
The Template:Keypress will tell vim that you want to delete something. After pressing Template:Keypress, you need to tell vim what to delete. Here you can use the movement commands. Template:Keypress will delete up to the next word. Template:Keypress will delete up unto the beginning of the line. Prefacing the delete command with a number works well too: Template:Keypress will delete the next three words. Template:Keypress (uppercase) is a shortcut to delete until the end of the line (basically Template:Keypress). Pressing Template:Keypress will delete the whole line.
To delete then replace the current word, place the cursor on the word and execute the command Template:Keypress. This will delete the word and change to insert mode. To replace only a single letter use Template:Keypress.
Undo and Redo
vim has a built-in clipboard (also known as a buffer). Actions can be undone with Template:Keypress and redone with Template:Keypress.
Visual Mode
Pressing Template:Keypress will put you in visual mode . Here you can move around to select text, when you’re done, you press Template:Keypress to yank the text into the buffer (copy), or you may use Template:Keypress to cut. Template:Keypress pastes after the cursor, Template:Keypress pastes before. Template:Keypress, Visual Line mode, is the same for entire lines. Template:Keypress is for blocks of text.
Search and Replace
To search for a word or character in the file, simply use Template:Keypress and then the characters your are searching for and press enter. To view the next match in the search press Template:Keypress, press Template:Keypress for the previous match.
To search and replace use the substitute Template:Keypress command. The syntax is: [range]s///[arguments]
. For example:
Command Outcome :s/xxx/yyy/ Replace xxx with yyy at the first occurence :s/xxx/yyy/g Replace xxx with yyy first occurrence, global (whole sentence) :s/xxx/yyy/gc Replace xxx with yyy global with confirm :%s/xxx/yyy/g Replace xxx with yyy global in the whole file
You can use the global Template:Keypress command to search for patterns and then execute a command for each match. The syntax is: [range]:g//[cmd]
.
Command Outcome :g/^#/d Delete all lines that begins with # :g/^$/d Delete all lines that are empty
Saving and Quitting
To save and/or quit, you will need to use Ex mode. Ex mode commands are preceded by a Template:Keypress. To write a file use Template:Keypress or if the file doesn’t have a name :w filename
. Quitting is done with Template:Keypress. If you choose not to save your changes, use Template:Keypress. To save and quit Template:Keypress.
Additional Commands
- Pressing Template:Keypress will erase the current letter under the cursor, and place you in insert mode. Template:Keypress will erase the whole line, and place you in insert mode.
- Template:Keypress will create a newline below the line and put you insert mode, Template:Keypress will create a newline above the line and put you in insert mode.
- Template:Keypress will yank an entire line
- Template:Keypress will delete the current line and place you in insert mode.
- Template:Keypress will highlight the current word and Template:Keypress will search it
Configuration
Vim's personal configuration file is located in the home directory: ~/.vimrc
. Advanced users tend to keep a well-tailored ~/.vimrc
. The global configuration file is located at /etc/vimrc
. The fall-back $VIM
variable is defined as /usr/share/vim/
. For example, to create a global colorscheme the *.vim
colorscheme file should be stored in /usr/share/vim/vimfiles/
.
Currently, 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 (like syntax highlighting, return to the line of the last edit...), consider using vim's example configuration file:
cp /etc/vimrc /etc/vimrc.bak cp /usr/share/vim/vim73/vimrc_example.vim /etc/vimrc
Backup Files
Vim by default creates a backup of an edited file in the same directory as the file called filename~
. To prevent clutter, many users tell vim to use a backup directory:
set backupdir=~/.vim/backup,/tmp
Or, it's possible to even disable this behavior:
set nobackup set nowritebackup set noswapfile ! (additionally disable swap files)
Wrap Searches
With this option the search next behaviour allows to jump to the beginning of the file, when the end of file is reached. Similarly, search previous jumps to the end of the file when the start is reached.
set wrapscan
Spell Checking
set spell
With this setting, Vim will highlight incorrectly spelled words. Place the cursor on a misspelled word and enter Template:Keypress to view spelling suggestions.
Only English language dictionaries are installed by default, more can be found in the official repositories. To get the list of available languages type:
# pacman -Ss vim-spell
Language dictionaries can also be found at the Vim FTP archive. Put the downloaded dictionar(y/ies) into the ~/.vim/spell
folder and set the dictionary by typing: :setlocal spell spelllang=LL
- To enable 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. For spell checking of languages other than English, simply replaceen_us
with the value appropriate for your language. - To enable spelling in two languages (for instance English and German), add
set spelllang=en,de
into your~/.vimrc
or/etc/vimrc
, and then restart vim. - You can enable spell checking for arbitrary file types (e.g. *.txt) by using the FileType plugin and a custom rule for file type detection. To enable spell checking for any file ending in
*.txt
, create the file/usr/share/vim/vimfiles/ftdetect/plaintext.vim
, and insert the lineautocmd BufRead,BufNewFile *.txt setfiletype plaintext
into that file. Next, insert the lineautocmd FileType plaintext setlocal spell spelllang=en_us
into your~/.vimrc
or/etc/vimrc
, and then restart Vim.
Syntax Highlighting
To enable syntax highlighting (vim supports a huge list of programming languages):
filetype plugin on syntax on
Using the Mouse
vim has the ability to make use of the mouse, but requires xterm's mouse reporting feature.
- See the example .vimrc below to enable the mouse.
- Use xterm. In your console:
export TERM=xterm-color
orexport TERM=xterm
Notes:
- This even works in PuTTY over SSH.
- 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 Template:Keypress key while selecting text.
Traverse line breaks with arrow keys
By default, pressing Template:Keypress at the beginning of a line, or pressing Template:Keypress 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.
Example ~/.vimrc
An example Vim configuration.
Merging Files (Vimdiff)
Vim includes a diff editor (a program that can merge differences between two files). Begin with vimdiff file1 file2
; to use:
]c : - next difference [c : - previous difference Ctrl+w +w - switch windows do - diff obtain dp - diff put zo - open folded text zc - close folded text :diffupdate - re-scan the files for differences
Vim Tips
Specific user tricks to accomplish tasks.
Line Numbers
- Show line numbers by
:set number
. - Jump to line number
:<line number>
.
Substitute on Lines
To only substitute between certain lines:
:n,ns/one/two/g
For example, to replace instances of 'one' with 'two' between lines 3 and 4, one would execute:
:3,4s/one/two/g
Make Vim restore cursor position in files
If you want the cursor to appear in its previous position after you open a file, add the following to your ~/.vimrc
:
if has("autocmd") au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif endif
See also this tip in Vim Wiki.
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.
A solution is to make a more pleasing background color: just put the following lines in ~/.gtkrc-2.0
:
style "vimfix" { bg[NORMAL] = "#242424" # this matches my gvim theme 'Normal' bg color. } widget "vim-main-window.*GtkForm" style "vimfix"
Replace vi command with vim
Run the following commands:
# ln -s $(which vim) /usr/local/bin/vi # ln -s $(which vim) /usr/local/bin/view
Also see http://superuser.com/questions/27091/vim-to-replace-vi
Vim Troubleshooting
"^M"
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.
Solution: Replace all "^M" using the command:
:%s/^M//g
Pay attention, "^" is the control letter, press <CTRL+Q> to get the right "^".
Alternatively, install the package dos2unix from the official repositories, and run dos2unix <file name here>
.
See also
Official
Tutorials
- vi Tutorial and Reference Guide
- Graphical vi-vim Cheat Sheet and Tutorial
- Vim Introduction and Tutorial
- Vim Recipes - A free cookbook.
- Open Vim - Collection of Vim learning tools
- Learn Vim Progressively
- know vim
Videos
- Vimcasts - Screencasts in .ogg format.
- Tutorial Videos - Covering the basics up to advanced topics.
Games
Example configurations
- nion's
- A detailed configuration from Amir Salihefendic
- Bart Trojanowski
- Steve Francia's Vim Distribution