VIM

VI(M) is a very powerful editor which requires you to survive a learning period as it is different from almost any interface you have used before. If you survive, you will love it, if not you will hate it.

So lets us vi a file right now!
vi test.txt

The first thing that whacks you in the face is that you cannot type text into your document! Why? VI has two modes:
1) command mode
2) edit mode
And you initially start in command mode. You can enter edit mode by pressing 'i'. Now you can type into the document. You can always get back to command mode by pressing escape. Back in command mode you can type :x to exit and save. That is the major hurdle that most people don't jump. There are plenty of good VIM documents on the web explaining basic usage to build on from there.

Here are some development specific tips once you are comfortable with VIM as an editor which make it far more productive than an IDE:

Use ctags

If you were recently editing but navigated away and want to go back to where you were, press u for undo then (ctr)r for redo.

Use .
This is 'repeat last command'
Suppose you called a variable 'notvalid' and then later decided you wanted it to be 'isvalid':
/notvalid
(searches forward for occurance of notvalid)
cw !isvalid
(change word - replaces notvalid with !isvalid)
(esc)
/
(search again)
.
(repeat last command - this will replace the notvalid we just found with !isvalid)
/
.
(and again)

Of course you could also use a global search and replace:
:%s/notvalid/!isvalid/g
Which is less typing, but I prefer to check each replace as I go, so the search and repeat is good for that. You can do very complex editing very quickly using this technique, just by putting a little thought into how to modify text in a repeatable way.

:make
will build your project from within VIM. The advantage of this is that it will take you instantly to the line and file of any error that might occur in the build.
:cn and :cp can be used to move forward and backward through the error messages.

Customise your ~/.vimrc
This is what will transform VIM into a customised IDE.

syntax on
Will do syntax highlighting

set viminfo='10,\"100,:20,%,n~/.viminfo
au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif
Will remember the last line you were in when you lasted edited a file.

set expandtab
Is usually the easiest way to ensure tab size is irrelevant.

set smartcase
Will allow you to search for strings case-insensitive unless you use capitals. There are many more handy ones, look up the documentation and go nuts!

set autoindent
set smartindent
set softtabstop=4
set shiftwidth=4
set showmatch
set ruler
set incsearch
set ignorecase
set number
set cindent
set backspace=2
set laststatus=2

%
If you press this while over a brace, it will take you to the complimentary brace. This is a life saver if the code isn't formatted how you like it and you can't be sure what nest you are in.

(shift)G
Go to end of file

?
search backward

/(up) or %(up) or ?(up)
each time you press (up) it goes back to the previous string so you can modify it. (down) goes forward through the list.

I
insert at start of line

A
append at end of line,
just press (esc) after if you dont want to put any text in

8yy
(navigate)
p
Will copy 8 lines and paste it where you want it

8dd
Will delete 8 lines of code

8(down)
Will move down 8 lines
I use this to measure how much I want to cut copy or delete.
Alternatively you can use visual select mode
(ctrl)v


1 comment:

  1. This might be useful only for C/C++ programmers.

    When you open either a header or source file, in my opinion, most of times, opening both files on the screen makes coding a lot easier. However, opening two files in vim is kind of annoying. BUT!

    :AV - opens current file's couple file vertically.

    :AS - opens current file's couple file horizontally.

    You can navigate different windows by using Ctrl+W+(Arrow Key).

    For example, if you are working on boo.cpp and type ":AS" then vim's screen will be split into 2 and showing both header and source files nicely!

    ReplyDelete