Vi editor

From AVRFreaks Wiki

(Redirected from Vim)
Jump to: navigation, search

Contents

[edit] Vi and Vim

Vi is one of the most popular text editors for programmers and users of Unix-like operating systems. As compared to Emacs, the other principal contender in the Editor War, Vi is small, fast and less resource-hungry. For this reason, Vi is almost always available by default, even in small embedded systems like the NGW100, whereas Emacs may have to be installed separately.

Vim (Vi IMproved) is a text editor derived from Vi and intended to be available on a wider range of platforms, to be easier to use and having a richer feature set.


[edit] Vi is Modal

The vi editor is generally considered to be a modal editor, meaning that the same keystrokes do different things depending on the current mode. The two modes are the command mode and the edit mode.

The initial mode (when the editor is started) is the command mode. In this mode you can perform such actions as moving the cursor, saving the file, switching to the edit mode or ending the program. Some examples of commands are:

+ go to the beginning of the next line
- go to the beginning of the previus line
i insert text (that is change to edit mode)
a append text (go to edit mode but place the cursor behind the char)

In the edit mode, keystrokes cause text to be added to (or for the delete and backspace keys, deleted from) the file. To change back to the command mode, the user presses the "Esc" key.


[edit] vimrc

First add this to the end off your ~/.vimrc or your _vimrc (C:\Program Files\Vim\_vimrc)

set nocompatible
syntax enable

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
set smartindent
set autoindent

set lines=75
set columns=80

set dir=$TEMP
set backupdir=$TEMP

" set noignorecase
set ignorecase

" :%s for search and replace is hard to type
" lets map them to gs...
" map gs :%s/FROM/TO/gci
map gs :%s


[edit] Indent code

Then open a sourcefile and do this

  1. gg
  2. V
  3. G
  4. =

or

  1. gg
  2. =
  3. G

The above indents everything in your sourcecode, enjoy.

[edit] Sort text in your editor

There is another handy trick that is useful and often is not found in the tutorials, and that is how to sort text in your source code.

The basic is that vim can pipe data to and from shell commands using the ! sign, and in this case we will send data to the sort command and then bring the result back into our sourcefile again.

Let's say that you have a header that looks like this.

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

You can the use the visual mode and mark the text and then press !sort, you then get something like this

  • :'<,'>!sort

And then after you pressed enter the marked text has been sorted into this and is more readable.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

Or if you don't want to use the visual mode, you can use this instead (but the marker must be at the first line when you use this).

  • :.,+4!sort

And if you look at the syntax you see that it is quite easy to use.

  • :"start line","end line"!"command to send lines to"

So if your lines was at lines 11 to 14 you could use this

  • :11,14!sort

But to be honest the visual mode is probably the most straight forward, just mark and sort.

[edit] Tutorials


[edit] Good plugins for development


[edit] Other vim related development tools


[edit] links

Personal tools