Using VIM

From Cosmin's Wiki

Jump to: navigation, search

Home > Linux how to's > Using VIM



Vim is a great editor with many features, and it's extensible. So by learning a subset of the features, customizing it by setting preferences, and by extending it with plugins and scripts, each user builds his own editor. Existing extensions are available from www.vim.org (alternative URL: vim.sourceforge.net). Vim also is very fast and is available for many platforms.

Contents


Handy Features in vim

Here's a list of some of the commands that I have found to be quite handy:

What you type What it does
:/pattern Search for pattern in a file. This can be a regular expression
:/\cpattern Just like /pattern, only the search is case insensitive (nice trick!!!)
n or N Will take you to the next/previous match of the search pattern
:%s/find/replace/g Find and Replace (will replace only the first occurence of each line)
:%s/find//gn Searches for the word 'find' and returns the number of occurences found. The n flag prevents actual substitution.
:%s/find/replace/g Find and Replace all occurences
:1,10 s/find/replace/g replaces all occurences of find with replace between line 1 and 10
$ Go to End of Line
^ Beginning of Line
gg Go to beginning of file
G Go to end of file
L Last displayed line
M Middle of the file
H Top of the file (Home)
:10 Go to line 10
dd Delete current line
Y or 5Y Copy or yank a line. Also try yG, and y$.
p Paste
u Undo
Ctrl+R Redo
:tabnew {<filename>} Opens a new tab. <filename> is optional. If not passed in, vim will open a new blank tab
:tabn {count} Tab Next. Jumps forward {count} tabs. If {count} is omitted, vim will jump to the next tab
:tabp {count} Tab Previous. Jumps backward {count} tabs. If {count} is omitted, vim will jump to the previous tab
:tabc Closes the current tab
:set number Shows line numbers
:set nonumber Hides line numbers

Examples of usage

What you type What it does
:452s/foo/bar/ Replace the first occurrence of the word foo with bar on line number 452.
:s/foo/bar/g Replace every occurrence of the word foo with bar on current line.
:%s/foo/bar/g Replace every occurrence of the word foo with bar in the whole file.
:%s/foo/bar/gi The same as above, but ignore the case of the pattern you want to substitute. This replaces foo, FOO, Foo, and so on.
:%s/foo/bar/gc Confirm every substitution.
:%s/foo/bar/c For each line on the file, replace the first occurrence of foo with bar and confirm every substitution.

Colors and syntax highlighting

The standard installation of vim comes with no colors whatsoever. However, if you type

:syn on

vim will automatically do syntax highlighting for you, using the default color scheme. Should you want to change the default color scheme, just type

:colorscheme elflord

and your whole document will be highlithed according to the new colorscheme.


Vim configuration

When doing the changes mentioned above, they will last just until you restart vim. If you want to have these (or some other features) always enabled when you start vim, you should start looking into the configuration file (edit it for example with your favorite text editor):

# vim /etc/vim/vimrc

Here you'll find a lot of features that you can enable by default (like syntax highlighting). Should you want to use by default a certain colorscheme (like elflord),just add in the vimrc file the following line:

colorscheme elflord

then save the file and restart vim.