Vi

From Andreida
Revision as of 11:43, 16 February 2016 by Andreas (talk | contribs) (Created page with "== Vim + Windows == === change menu and messages to us english === c:\Installed\Text\Vim\_vimrc set langmenu=en_US.UTF-8 " sets the language of the menu (gvim) language ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Vim + Windows

change menu and messages to us english

c:\Installed\Text\Vim\_vimrc

set langmenu=en_US.UTF-8    " sets the language of the menu (gvim)
language en                 " sets the language of the messages / ui (vim)
setlocal encoding=utf-8     " so you can get for example german umlauts

VIM + Linux

Install vim or you will only have a bad vi clone

 apt-get install vim gpm ctags vim-doc

Vi Shortcuts

pasting

If you paste formatted code, it will look ugly. Either use

set paste
... do stuff
set nopaste

Or use something like

set pastetoggle=<F2>

starting vim

vim -o *.php     open vim with a view for each found php file
vim -p *.php     open vim with one tab for each found php file
vim -u NONE      start vim without any plugins

recording

q <letter>        record the following
q                 stop recording
@<letter>         play the recording
<number>@<letter> play the recording <number> times

I use this for example to check files which have to be in a certain format. The '.' does only repeat the last command that did change something, so you need to do it different for move commands.

misc

:reg                 show registers, like copy buffers
:set paste/nopaste   allow to paste formatted source code
:set hls/nohls       highlight search matches
:set list            show special chars
:set et!             leave tabs alone
:set et              replace tabs with spaces depending on the tabstop value
:set tabstop=N

move/view

 ^               goto start of line
 $               goto end of line
 ctrl-l          redraw screen
 ctrl-z          vi -> bg
 fg              vi -> fg
 h,l/j,k         move one col/row
 w,b             move one word forward/backwards
 ctrl-e/y        move screen one up/down
 zt/zb/z.        scroll current line to the top/bottom/center of the view
 gf              open file under cursor
 :split          new window
 :split <file>   new window with given file
 ctrl-w n        new empty window
 ctrl-w j/k/l/h  next/previous window
 ctrl-w p        jump to last window
 ctrl-w f        open file under cursor in new window
 ctrl-w _        resize to max (like :res without parameter, that is an underscore)
 ctrl-w }        preview tag under cursor
 :tabnew         create a new tab
 ctrl-w gf       open new tab with file under cursor
 crtl-w T        move current split to new tab
 gt/gT           move to next/previous tab
 <n> gt          move to given tab, count starts with 1
 :tabc[lose]     close the current tab
 :tabo[nly]      close all other tabs
:res <lines>            resize window height
:vertical res <columns> resize window width

delete

 dw              delete until end of word
 d$              delete until end of line
 d/BLA           delete until BLA, example: d/>  delete until >, but do not delete the >


change

 guw             make the word lowercase
 gUw             make the word uppercase
 gu$             make lowercase until the end of line
 guu             line lowercase
 gUU             line upercase
 g~~             line change case
 g~w             word change case
 ....

~/.exrc


"no message
set nomesg

"show matching braces
set sm

"tabstop
set ts=4

" no search result highlighting
set nohlsearch

"shiftwidth
set shiftwidth=4

"expandtab
set expandtab 

"line numbers
set nu

"syntax
syntax on

"c-style indent
set cindent

"tags
"set tags=~/.tags/ProjektX.tags 
set tags=./tags

"auto indent
set autoindent

"preview - just use pre in your current window, then move the new window
map PRE :set previewwindow <CR>
map pre :ped % <CR>            

map lint :!clear;php -l % <CR>
map php  :!clear;php    % <CR>
map phpl :!clear;php    % \| less  <CR>

map go <Esc> :!clear;./myProgram & <CR>
map  make <Esc> :!clear;make -s <CR>

"go next/previous window
map gp <C-W>k
map gn <C-W>j


"folding
"set foldcolumn=4
"set foldmethod=indent
set foldmethod=syntax


"sessions
set sessionoptions+=resize
set sessionoptions+=winpos
map <F5> <ESC> :mksession! quicksave.vim <Return>
map <F9> <ESC> :source quicksave.vim <Return>

"create c++ tags for all files in and below the current directory into the file tags in the current directory
map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>

"makefiles need tabs
:autocmd FileType make set noexpandtab



Sessions

If you have some windows open and would like to return some day to this layout, just use

 :mksession! <filename>

for example

 :mksession! proj-1.vim

and when you later want to continue where you were, you either start vim like this

 vi -S proj-1.vim

or you use the following command being in vi

 :source proj-1.vim

You will have to watch yourself, so you are always in the correct directory. Here are two mappings which may help you:

 map <F5> <ESC> :mksession! quicksave.vim <Return>
 map <F9> <ESC> :source quicksave.vim <Return>

This gets interesting, when you later rename the quicksave.vim *and* change the mappings in the new .vim file to ones that load and save *that* .vim file. Example:

 mv quicksave.vim misc.vim
 map <F5> <ESC> :mksession! misc.vim <Return>
 map <F9> <ESC> :source misc.vim <Return>

That way you can save and load your current session with F5 and F9.

rename-quicksave.sh

#!/bin/bash

if [ "$1" = "--help" ] || [ $# -ne 1 ]; then
   echo rename-quicksave v 0.1
   echo This script will rename a quicksave.vim and change the string quicksave inside the .vim file.
   echo Syntax: $0 \<new name without .vim\>
   exit 1
fi

if [ ! -f quicksave.vim ]; then
   echo Error: File quicksave.vim has not been found in the current directory !
   pwd
   exit 2
fi

echo -n renaming...
mv quicksave.vim $1.vim
echo ok

echo -n replacing string...
sed -i s/quicksave/$1/g $1.vim
echo ok 

CTags einrichten (exuberant-ctags)

http://weierophinney.net/matthew/archives/134-exuberant-ctags-with-PHP-in-Vim.html

First you have to make sure "exuberant ctags" is installed. Just call

ctags --version

to find out if it is installed. If not:

 apt-get install exuberant-ctags

create the tags:

#!/bin/bash
PROJECT_DIR=/home/user/work
TAG_DIR=/home/user/.tags
TAG_FILE=project.tags
TAG_FILE_TEMP=${TAG_FILE}.creating
          
mkdir -p $TAG_DIR
cd ${PROJECT_DIR}
ctags-exuberant -f $TAG_DIR/$TAG_FILE_TEMP \
    -h ".php" -R \
    --exclude="\.svn" \
    --exclude="\CVS" \
    --totals=yes \
    --languages=PHP \
    --tag-relative=yes \
    --PHP-kinds=+cf \
    --regex-PHP='/abstract class ([^ ]*)/\1/c/' \
    --regex-PHP='/interface ([^ ]*)/\1/c/' \
    --regex-PHP='/(public |static |abstract |protected |private )+function ([^ (]*)/\2/f/' \
    Project-*/*

mv ${TAG_DIR}/${TAG_FILE_TEMP} ${TAG_DIR}/${TAG_FILE}

ls -lh ${TAG_DIR}/${TAG_FILE}

You can use the created tag file on a normal system, where "exuberant-ctags" are not installed. It seems, you need the addon only to create the tags, but use the normal vi mechanism to use the tags. So the last line in the generator script could be like:

     scp ${TAG_DIR}/${TAG_FILE} <server>:$TAG_TARGET_DIR


Now you want to use the tags in every vi you start: ~/.exrc

  set tags=~/.tags/MyProjekt.tags

The documentation says to use

 ctrl-]        Jump to tag under cursor
 crtl-t        jump back

but in my systems I have to use

 ctrl-alt-]    jump to tag under cursor
 ctrl-alt-t    jump back
 ctrl-w ]      split tag under cursor into new window
 ctrl-(alt)-o  last position in jump stack or so (unsure)

FuzzyFinder

Either just use http://wiki.andreas-duffner.de/images/a/a3/Fuzzyfinder.vim or get the current version from http://www.vim.org/scripts/script.php?script_id=1984.

The current version does not work with vim 7.0.

in your .exrc

wildmode=list:longest
wildmenu
hidden
history=1000
cab ffb FuzzyFinderBuffer
cab ff FuzzyFinderFile
cab ffm FuzzyFinderMruFile
cab fft FuzzyFinderTag

in vim use

:Fuzzy<Tab>

to show all new commands. Above are shortcuts for some of them.


Syntax

Makefile

"makefiles need tabs
:autocmd FileType make set noexpandtab

PHP

.exrc

"php
"If you like SQL syntax highlighting inside Strings: >
let php_sql_query = 1
"For highlighting the Baselib methods: >
let php_baselib = 1
"Enable HTML syntax highlighting inside strings: >
let php_htmlInStrings = 1
"Using the old colorstyle: >
let php_oldStyle = 1
"Enable highlighting ASP-style short tags: >
let php_asp_tags = 1
"Disable short tags: >
let php_noShortTags = 1
"For highlighting parent error ] or ): >
let php_parent_error_close = 1
"For skipping an php end tag, if there exists an open ( or [ without a closing one: >
let php_parent_error_open = 1
"Enable folding for classes and functions: >
"let php_folding = 1
"Selecting syncing method: >
"let php_sync_method = 0

XML

  • get the script from xmledit
    • get the .vba file
  • edit the .vba file with vim
  • :source %
  • installation done
  • edit your .exrc
"folding for xml
let g:xml_syntax_folding=1
au FileType xml setlocal foldmethod=syntax

"reformatting xml to look good
au FileType xml exe ":silent 1,$!xmllint --format --recover - 2>/dev/null"
au FileType xsd exe ":silent 1,$!xmllint --format --recover - 2>/dev/null"
au FileType wsdl exe ":silent 1,$!xmllint --format --recover - 2>/dev/null"


  • now you should have folds: zo zc etc...
  • open tags should be closed when you finish a tag with >
  • try >> for setting the cursor better

Cobol

.exrc

au BufRead,BufNewFile *.eco set filetype=cobol
au! Syntax cobol source ~/.vim/cobol.vim 

~/.vim/cobol.vim

" Vim syntax file
" Language: COBOL
" Maintainers: Davyd Ondrejko
" (formerly Sitaram Chamarty
" James Mitchell
" Last change: 2001 Sep 02

" For version 5.x: Clear all syntax items
" For version 6.x: Quit when a syntax file was already loaded

" Stephen Gennard
" - added keywords - AS, REPOSITORY
" - added extra cobolCall bits

if version < 600
  syntax clear
elseif exists("b:current_syntax")
  finish
endif

" MOST important - else most of the keywords wont work!
if version < 600
  set isk=@,48-57,-
else
  setlocal isk=@,48-57,-
endif

syn case ignore

if exists("cobol_legacy_code")
  syn match cobolKeys "^\a\{1,6\}" contains=cobolReserved
else
  syn match cobolKeys "" contains=cobolReserved
endif

syn keyword cobolReserved contained ACCEPT ACCESS ADD ADDRESS ADVANCING AFTER ALPHABET ALPHABETIC
syn keyword cobolReserved contained ALPHABETIC-LOWER ALPHABETIC-UPPER ALPHANUMERIC ALPHANUMERIC-EDITED ALS
syn keyword cobolReserved contained ALTERNATE AND ANY ARE AREA AREAS ASCENDING ASSIGN AT AUTHOR BEFORE BINARY
syn keyword cobolReserved contained BLANK BLOCK BOTTOM BY CANCEL CBLL CD CF CH CHARACTER CHARACTERS CLASS
syn keyword cobolReserved contained CLOCK-UNITS CLOSE COBOL CODE CODE-SET COLLATING COLUMN COMMA COMMON
syn keyword cobolReserved contained COMMUNICATIONS COMPUTATIONAL COMPUTE CONFIGURATION CONTENT CONTINUE
syn keyword cobolReserved contained CONTROL CONVERTING CORR CORRESPONDING COUNT CURRENCY DATA DATE DATE-COMPILED
syn keyword cobolReserved contained DATE-WRITTEN DAY DAY-OF-WEEK DE DEBUG-CONTENTS DEBUG-ITEM DEBUG-LINE
syn keyword cobolReserved contained DEBUG-NAME DEBUG-SUB-1 DEBUG-SUB-2 DEBUG-SUB-3 DEBUGGING DECIMAL-POINT
syn keyword cobolReserved contained DELARATIVES DELETE DELIMITED DELIMITER DEPENDING DESCENDING DESTINATION
syn keyword cobolReserved contained DETAIL DISABLE DISPLAY DIVIDE DIVISION DOWN DUPLICATES DYNAMIC EGI ELSE EMI
syn keyword cobolReserved contained ENABLE END-ADD END-COMPUTE END-DELETE END-DIVIDE END-EVALUATE END-IF
syn keyword cobolReserved contained END-MULTIPLY END-OF-PAGE END-PERFORM END-READ END-RECEIVE END-RETURN
syn keyword cobolReserved contained END-REWRITE END-SEARCH END-START END-STRING END-SUBTRACT END-UNSTRING
syn keyword cobolReserved contained END-WRITE ENVIRONMENT EQUAL ERROR ESI EVALUATE EVERY EXCEPTION
syn keyword cobolReserved contained EXTEND EXTERNAL FALSE FD FILE FILE-CONTROL FILLER FINAL FIRST FOOTING FOR FROM
syn keyword cobolReserved contained GENERATE GIVING GLOBAL GREATER GROUP HEADING HIGH-VALUE HIGH-VALUES I-O
syn keyword cobolReserved contained I-O-CONTROL IDENTIFICATION IN INDEX INDEXED INDICATE INITIAL INITIALIZE
syn keyword cobolReserved contained INITIATE INPUT INPUT-OUTPUT INSPECT INSTALLATION INTO IS JUST
syn keyword cobolReserved contained JUSTIFIED KEY LABEL LAST LEADING LEFT LENGTH LOCK MEMORY
syn keyword cobolReserved contained MERGE MESSAGE MODE MODULES MOVE MULTIPLE MULTIPLY NATIVE NEGATIVE NEXT NO NOT
syn keyword cobolReserved contained NUMBER NUMERIC NUMERIC-EDITED OBJECT-COMPUTER OCCURS OF OFF OMITTED ON OPEN
syn keyword cobolReserved contained OPTIONAL OR ORDER ORGANIZATION OTHER OUTPUT OVERFLOW PACKED-DECIMAL PADDING
syn keyword cobolReserved contained PAGE PAGE-COUNTER PERFORM PF PH PIC PICTURE PLUS POSITION POSITIVE
syn keyword cobolReserved contained PRINTING PROCEDURE PROCEDURES PROCEDD PROGRAM PROGRAM-ID PURGE QUEUE QUOTES
syn keyword cobolReserved contained RANDOM RD READ RECEIVE RECORD RECORDS REDEFINES REEL REFERENCE REFERENCES
syn keyword cobolReserved contained RELATIVE RELEASE REMAINDER REMOVAL REPLACE REPLACING REPORT REPORTING
syn keyword cobolReserved contained REPORTS RERUN RESERVE RESET RETURN RETURNING REVERSED REWIND REWRITE RF RH
syn keyword cobolReserved contained RIGHT ROUNDED SAME SD SEARCH SECTION SECURITY SEGMENT SEGMENT-LIMITED
syn keyword cobolReserved contained SELECT SEND SENTENCE SEPARATE SEQUENCE SEQUENTIAL SET SIGN SIZE SORT
syn keyword cobolReserved contained SORT-MERGE SOURCE SOURCE-COMPUTER SPECIAL-NAMES STANDARD
syn keyword cobolReserved contained STANDARD-1 STANDARD-2 START STATUS STRING SUB-QUEUE-1 SUB-QUEUE-2
syn keyword cobolReserved contained SUB-QUEUE-3 SUBTRACT SUM SUPPRESS SYMBOLIC SYNC SYNCHRONIZED TABLE TALLYING
syn keyword cobolReserved contained TAPE TERMINAL TERMINATE TEST TEXT THAN THEN THROUGH THRU TIME TIMES TO TOP
syn keyword cobolReserved contained TRAILING TRUE TYPE UNIT UNSTRING UNTIL UP UPON USAGE USE USING VALUE VALUES
syn keyword cobolReserved contained VARYING WHEN WITH WORDS WORKING-STORAGE WRITE

" new
syn keyword cobolReserved contained AS LOCAL-STORAGE LINKAGE SCREEN ENTRY

" new
syn keyword cobolReserved contained environment-name environment-value argument-number
syn keyword cobolReserved contained call-convention identified pointer

syn keyword cobolReserved contained external-form division wait national

" new -- oo stuff
syn keyword cobolReserved contained repository object class method-id method object static
syn keyword cobolReserved contained class-id class-control private inherits object-storage
syn keyword cobolReserved contained class-object protected delegate
syn keyword cobolReserved contained try catch raise end-try super property
syn keyword cobolReserved contained override instance equals

" new - new types
syn match cobolTypes "condition-value"hs=s,he=e
syn match cobolTypes "binary-long"hs=s,he=e
syn match cobolTypes "binary-short"hs=s,he=e
syn match cobolTypes "binary-double"hs=s,he=e
syn match cobolTypes "procedure-pointer"hs=s,he=e
syn match cobolTypes "object reference"hs=s,he=e

syn match cobolReserved contained "\<CONTAINS\>"
syn match cobolReserved contained "\<\(IF\|ELSE|INVALID\|END\|EOP\)\>"
syn match cobolReserved contained "\<ALL\>"

syn keyword cobolConstant SPACE SPACES NULL ZERO ZEROES ZEROS LOW-VALUE LOW-VALUES

if exists("cobol_legacy_code")
  syn match cobolMarker "^.\{6\}"
  syn match cobolBadLine "^.\{6\}[^ D\-*$/].*"hs=s+6
  " If comment mark somehow gets into column past Column 7.
  syn match cobolBadLine "^.\{6\}\s\+\*.*"
endif

syn match cobolNumber "\<-\=\d*\.\=\d\+\>" contains=cobolMarker,cobolComment
syn match cobolPic "\<S*9\+\>" contains=cobolMarker,cobolComment
syn match cobolPic "\<$*\.\=9\+\>" contains=cobolMarker,cobolComment
syn match cobolPic "\<Z*\.\=9\+\>" contains=cobolMarker,cobolComment
syn match cobolPic "\<V9\+\>" contains=cobolMarker,cobolComment
syn match cobolPic "\<9\+V\>" contains=cobolMarker,cobolComment
syn match cobolPic "\<-\+[Z9]\+\>" contains=cobolMarker,cobolComment
syn match cobolTodo "todo" contained

if exists("cobol_mf_syntax")
  syn region cobolComment start="*>" end="$" contains=cobolTodo,cobolMarker
endif

syn keyword cobolGoTo GO GOTO
syn keyword cobolCopy COPY

" cobolBAD: things that are BAD NEWS!
syn keyword cobolBAD ALTER ENTER RENAMES

" cobolWatch: things that are important when trying to understand a program
syn keyword cobolWatch OCCURS DEPENDING VARYING BINARY COMP REDEFINES
syn keyword cobolWatch REPLACING THROW
syn match cobolWatch "COMP-[123456XN]"

syn region cobolEXECs contains=cobolLine start="EXEC " end="END-EXEC"

syn match cobolComment "^.\{6\}\*.*"hs=s+6 contains=cobolTodo,cobolMarker
syn match cobolComment "^.\{6\}/.*"hs=s+6 contains=cobolTodo,cobolMarker
syn match cobolComment "^.\{6\}C.*"hs=s+6 contains=cobolTodo,cobolMarker

if exists("cobol_legacy_code")
  syn match cobolCompiler "^.\{6\}$.*"hs=s+6
  syn match cobolDecl "^.\{6} \{1,8}\(0\=1\|77\|78\) "hs=s+7,he=e-1 contains=cobolMarker
  syn match cobolDecl "^.\{6} \+[1-8]\d "hs=s+7,he=e-1 contains=cobolMarker
  syn match cobolDecl "^.\{6} \+0\=[2-9] "hs=s+7,he=e-1 contains=cobolMarker
  syn match cobolDecl "^.\{6} \+66 "hs=s+7,he=e-1 contains=cobolMarker
  syn match cobolWatch "^.\{6} \+88 "hs=s+7,he=e-1 contains=cobolMarker
else
  syn match cobolWhiteSpace "^*[ \t]"
  syn match cobolCompiler "$.*"hs=s,he=e contains=cobolWhiteSpace,cobolTypes
  syn match cobolDecl "0\=[1-9] *$"hs=s,he=e-1 contains=cobolWhiteSpace,cobolTypes
  syn match cobolDecl "66 *$"hs=s,he=e-1 contains=cobolWhiteSpace,cobolTypes
  syn match cobolWatch "88 *$"hs=s,he=e-1 contains=cobolWhiteSpace,cobolTypes
endif

syn match cobolBadID "\k\+-\($\|[^-A-Z0-9]\)"

syn keyword cobolCALLs CALL CANCEL GOBACK INVOKE PERFORM END-PERFORM END-CALL RUN
syn match cobolCALLs "STOP \+RUN"
syn match cobolCALLs "EXIT \+PROGRAM"
syn match cobolCALLs "EXIT \+PROGRAM \+RETURNING"
syn match cobolCALLs "EXIT \+PERFORM"
syn match cobolCALLs "EXIT \+METHOD"
syn match cobolCALLs "EXIT \+SECTION"
syn match cobolCALLs "STOP " contains=cobolString

syn match cobolExtras /\<VALUE \+\d\+\./hs=s+6,he=e-1

" zero terminated strings eg: pic x(10) value z"My C String"
if exists("cobol_mf_syntax")
  syn match cobolString /z"[^"]*\("\|$\)/
endif

syn match cobolString /"[^"]*\("\|$\)/
syn match cobolString /'[^']*\('\|$\)/

if exists("cobol_legacy_code")
  syn region cobolCondFlow contains=ALLBUT,cobolLine start="\<\(IF\|INVALID\|END\|EOP\)\>" skip=/\('\|"\)[^"]\{-}\("\|'\|$\)/ end="\." keepend
  syn region cobolLine start="^.\{6} " end="$" contains=ALL
endif

if exists("cobol_legacy_code")
  " catch junk in columns 1-6 for modern code
  syn match cobolBAD "^ \{0,5\}[^ ].*"
endif

" many legacy sources have junk in columns 1-6: must be before others
" Stuff after column 72 is in error - must be after all other "match" entries
if exists("cobol_legacy_code")
  syn match cobolBadLine "^.\{6}[^*/].\{66,\}"
endif

" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
" For version 5.8 and later: only when an item doesn't have highlighting yet
if version >= 508 || !exists("did_cobol_syntax_inits")
  if version < 508
    let did_cobol_syntax_inits = 1
    command -nargs=+ HiLink hi link <args>
  else
    command -nargs=+ HiLink hi def link <args>
  endif
  HiLink cobolBAD Error
  HiLink cobolBadID Error
  HiLink cobolBadLine Error
  HiLink cobolMarker Comment
  HiLink cobolCALLs Function
  HiLink cobolComment Comment
  HiLink cobolKeys Comment
  HiLink cobolCompiler PreProc
  HiLink cobolEXECs PreProc
  HiLink cobolCondFlow Special
  HiLink cobolCopy PreProc
  HiLink cobolDecl Type
  HiLink cobolTypes Type
  HiLink cobolExtras Special
  HiLink cobolGoTo Special
  HiLink cobolConstant Constant
  HiLink cobolNumber Constant
  HiLink cobolPic Constant
  HiLink cobolReserved Statement
  HiLink cobolString Constant
  HiLink cobolTodo Todo
  HiLink cobolWatch Special
  delcommand HiLink
endif

let b:current_syntax = "cobol"

" vim: ts=6 nowrap

cvs

  • get the file from http://www.vim.org/scripts/script.php?script_id=90
  • unzip it to ~/.vim (or whereever your vim stuff is)
  • in vi call
    • :helptags ~/.vim/doc
    • :help local-additions
    • read the help for vcscommand.txt
    • :VCSVimDiff (diff to head)
    • :VCSVimDiff 1.34 (diff to special revision or tag)

c++

  • c++ tags
ctags -R --sort=1 --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ .
  • make
:map  make <Esc> :!clear;make -s <CR>
  • run your main program
:map go <Esc> :!clear;./yagodu2 & <CR>


I am not using this below anymore. C++ tags are really all you need. The rest is nice to have or for show off. At least it seems so to me, because it did not help me to work faster.

from [1] I got how to get a better auto complete list for c++:

  • build your tags with the following command
ctags -R --sort=1 --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ .
  • get omnicppcomplete from here omnicppcomplete
  • set the following in your .exrc or .vimrc file
" =============================================
" OmniCppComplete
" from http://www.vim.org/scripts/script.php?script_id=1520
" via http://vim.wikia.com/wiki/C%2B%2B_code_completion
set nocp
filetype plugin on

" OmniCppComplete
let OmniCpp_NamespaceSearch = 1
let OmniCpp_GlobalScopeSearch = 1
let OmniCpp_ShowAccess = 1

" show function parameters
let OmniCpp_ShowPrototypeInAbbr = 1 

" autocomplete after .
let OmniCpp_MayCompleteDot = 1

" autocomplete after ->
let OmniCpp_MayCompleteArrow = 1 

" autocomplete after ::
let OmniCpp_MayCompleteScope = 1 
 
let OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD"]

" automatically open and close the popup menu / preview window
au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif
set completeopt=menuone,menu,longest,preview

Of course you might perhaps want to remove/change the default namespaces etc...