User Tools

Site Tools


documentation:vim

Vi IMproved

Vim is a moded text editor modeled after VI that offers the user with a much friendlier environment to type in. It adds support for the directional keys in editing so the user doesn't have to use h, j, k, and l to navigate the document along with several other compatibility and usability modifications. This document will serve as sort of a beginners how-to for the use of vim.

Starting Off

First off, when you first start up vim you will be in the command mode. This means that most of your normal keystrokes won't put text to the screen but will rather perform a predetermined command. For instance typing the h key when in command mode will actually move the cursor to the left (assuming of course there is space in that direction for the cursor to move). So before you can get to any actual editing you need to be in the insert mode. Now there are several key combinations that will do this.

i,I - Insert text at the cursor, insert text at the beginning of the current line.
a,A - Append text after the cursor, append text to the end of the current line.
o,O - Insert a new line below the cursor and insert text, insert a new line above the cursor and insert text.
s,S - Delete the character at the cursor and insert text, clear the current line and insert text.

Now, all these options might seem daunting but with time being able to use this variety of options to begin editing your file will prove to be a boon. Once in insert mode Vim behaves just like any other text editor that you've known. It's strengths come from the things that it is able to do beyond simply editing text. When you're ready to save your file and quit simply go to command mode, type : to get the extended commands then type wq to save and quit. Remember that you can append an ! to a command to force its execution. This doesn't work with every extended command but many of them will allow you to force their execution if pesky errors would otherwise keep you from doing so. If that happens to not be your cup of tea for saving and exiting from vim there is another option. Holding the shift key and tapping the Z key twice yields the same result as the :wq command.

Search and Replace

Now let's assume you've written this lovely little bit of source code but woe is you! Upon trying to compile the code you realize that you have misspelled one of your variables and it's causing compiler errors. What do you do? Well, you could simply reopen your code then scroll through it looking for the offending variable in every instance of it and fix it. But there's a better way. Open the code in Vim and from the command mode type / then the name of the offending variable and hit enter. This is Vim's search function and it will take you to every instance of the pattern that you enter after the slash. But what if your code is extremely long and changing all of those variables holds more tedium than you can handle? Fear not! Vim has a handy little search and replace function built in. Assume that a small oversight lead you to try to use input as a variable name several times when in fact you declared value to be the word you were to use for the variable. Now you have all these pesky instances of the word input but not enough to just change the variable declaration. Well, from Vim's command mode type :. This gives you the ability to issue extended commands, just like you use if you want to save and quit. Now from this type the % symbol. This tells vim that the following command is to be run on the entirety of the file. You can change this to be an interval of lines such as 2,14 meaning run this command on lines 2-14 of this file. Another more complex method of selecting is to tell vim that you want to run the command on the current line and the next 3 lines after it, to accomplish this you use .,+3 as the interval. Then you type s/, this tells vim you want to use its search and replace function. Now simply type the pattern that you wish to change, in our case the word input. Then type another / followed by the pattern you wish to replace it with. End it all with another slash followed by the letter g. So our command looks like this :%s/input/value/g. What this says is that for the entirety of the file, find every instance of the pattern input and replace it with value.

Now that's quite a bit of information in one dose. Basically what it means is that if you want to replace a certain pattern within your file all you have to do is use the s option. How you use it is to specify an interval of the file, usually either the whole file, or a group of lines. Then the letter s followed by / and the pattern you're searching for. If anyone reading this is unfamiliar with regex's there will be a tutorial up on them soon as well and will be linked to in this one. Finally another / and the pattern you want to replace it with and another /. Optionally you can add a g to the end of the command after the final / to tell the command to do this replacement on every instance of the pattern, not just the first instance of it on each line.

In the most basic sense its use corresponds to the usage of the sed command in bash.

The Configuration File

Vim uses a configuration file on the home directory of the user running it called .vimrc. This file states the options that should be set by default whenever vim is started. There are many options that can be specified here. As with most things all of the things that can be set here can be done from within vim via the :set command. The following table shows some of the more commonly used commands to be put in the .vimrc file. If no entry is given for the Modes portion then that particular command is on by issuing :set <name> and off by issuing :set no<name>.

Command Modes Description
bg bg=light, bg=dark Sets the colors of syntax highlighting for either dark or light color schemes (For better viewing based on terminal colors)
syn syn=on, syn=off Sets syntax highlighting based on the type of file being edited. (C, C++, HTML, PHP, Perl, etc)
tabstop tabstop=# Sets the number of characters a striking of the TAB key stops at (Ex: A tabstop of 4 means a tab occupies 4 characters of space)
shiftwidth shiftwidth=# This option sets how many columns are shifted using the reindenting (« and ») commands
textwidth textwidth=# Automatically creates a new line after # characters. Useful if the file you change things within long lines as it breaks lines up into manageable portions.
ignorecase Ignores case when searching
incsearch Automatically jump to first instance of searched term as it's typed (search still must be finished with Enter)
hlsearch Highlight search terms
smartindent Automatically indents blocks of code (A block is denoted by { and } in C/C++, also after certain words in bash scripts like for, while, if, etc)
autoindent Preserves indents from previous lines (Useful when editing a block of code.)

Now, Vim can also be set up to have per file type configurations as well. By putting an entry with autocmd for the file type in question and specifying a .vim file in the ~/.vim directory it will use a secondary configuration along with the contents of the .vimrc file if the file being edited matches the file type in question.

For example. If you wanted to have a separate configuration for C source code, then in your .vimrc file you would type

autocmd FileType c source ~/.vim/c.vim

where ~/.vim/c.vim is a file in the same format as .vimrc that contains the settings that you wish to be loaded whenever you edit C source code. This can be done for any file type that you wish, C code, html files, text files, as long as vim can recognize it either by its extension this command and separate configuration can be used.

Vim can also check for filetype not only by extension but by the content of the file as well. For example, say you want to have a set of vim settings for when you are editing bash scripts but said scripts don't always have the .sh extension for whatever reason. Well here's what you do: Create a new file in your ~/.vim directory called scripts.vim, this file is one that vim will automatically check upon startup and apply settings in it accordingly, just like .vimrc. now in this file put this code.

if did_filetype()   " filetype already set..                                    
    finish          " ..don't do these checks
endif
if getline(1) =~ '^#!.*\<bash\>'
    setfiletype sh
endif

Now save the file. Basically what this says is first off if a filetype has already been set either from extension detection or the other rules in the $VIMRUNTIME/scripts.vim then do nothing. But if not, then it checks the first line of the file and if it matches the regex given between the quotes of the beginning of the line followed by a she-bang with the word bash somewhere in the line then it sets the filetype to be an sh script. This could be useful if for some reason you don't want to append a .sh to your shell scripts yet still want to set up an sh.vim with specific rules for editing shell scripts.

Commands

This section will go over the commands that can be used within Vim. Vim's command mode is one of its most powerful assets as a text editor, being able to quickly and easily perform a variety of operations. Almost all of these commands can have a number put before them to have that command execute a certain number of times.

Command Options Function
d w, d, ^, $ Deletes the current word, line, to the beginning of the line, to the end of the line.
c w, ^, $ Delete the current word, or to the beginning or end of the line and go to insert mode.
r any character Replace a single character with the next character typed.
u Undo the last change made.
x Delete the character under the cursor
y w, y Yank (copy) the current word or line to the buffer.
p, P Paste the contents of the buffer after the cursor, paste the contents of the buffer before the cursor.
~ Change the case of the character under the cursor.
» Shift the current line right (indent)
« Shift the current line left (de-indent)
documentation/vim.txt · Last modified: 2011/02/06 00:26 by mtaft4