topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 7:18 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Vim: important usage tips  (Read 13013 times)

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Vim: important usage tips
« on: December 06, 2006, 05:17 AM »
Well.. Recently i haven't been able to spend as much time here at DC as i'd like to. That's because i've had an humongous amount of work to do lately.
The up side on this, is that i've been intensively using VIM. This resulted in learning a few tricks, and making a few improvements on what already is possibly the best editor around.
So, i'd like to share them with you.

Actually, i won't share them all, they are too many, but i'll give you the best pointers :P
I started by making a google search about vimrc. It's vim's configuration and startup file, the place to put all your configs. The google link i mentioned was where i learned the most, because it had other people's vimrc files, and the big majority of them are very well commented, which is a big help in understanding what each of the commands does.
After, i found that there's a small url in vim's page that links to vim tips. It's amazing the quantity of fantastic stuff available there, not only on the topics itself but also on the comments. Be sure to search for the top rated first, as those are the most interesting advices, and some sum lots of topics into a single one. This will require that you know something about vim already, though. That's why i recomend you do the vim tutor first.

Well... That sums it up. I know it doesn't looks like much, but there's material for about 2-3 weeks worth of reading material ;)
I leave you with my vimrc file, with the options i consider most important. (awaiting editing to add the file)

my vimrc
"added by jgpaiva
syntax on

"set
set hlsearch
set ignorecase smartcase
set incsearch
set infercase
set shiftwidth=3
set expandtab
set softtabstop=2
set wrap
set ruler
set foldcolumn=1
set smartindent
set backspace=2
set shiftwidth=2
set virtualedit=all
set wildmenu
set number
set hidden
set showbreak=\ +\
set mouse=ar
set showmatch
set gdefault
set fillchars=vert:\ ,fold:-
set foldenable " Turn on folding
set foldmethod=indent " Make folding indent sensitive
set foldlevel=100 " Don't autofold anything (but I can still fold manually)
set laststatus=2
set splitright
set viminfo=s1,<1024,'1024,/1024,:1024,@1024,f1,%
set showcmd
set nostartofline
set suffixes+=.out,.in
set showcmd

"colorscheme
colorscheme asmanian2
"imap
imap <c-s>:update<BR>i<Right>
imap <F9> <esc>:update<BR>:make clean<BR>:make
imap <C-BS> <c-left>vboldi
inoremap <tab> <c-r>=InsertTabWrapper()<cr>
"vmap
vmap <tab> >gv
vmap <s-tab> <gv
vmap <c-s>:update<BR>gv
"nmap
nmap <F9> :update<BR>:make clean<BR>:make
nmap <c-s> :update
nmap <Space> <PageDown>
nmap <BS> <PageUp>
nmap <c-p> :bnext
nmap <c-n> :bprevious
nmap -cd :cd%:h<CR>
nmap -lcd :lcd%:h<CR>
nmap <c-f4> :bdel<cr>
nmap ð :cprev<CR>
nmap <c-tab> :tabnext<CR>
nmap <c-s-tab> :tabprev<CR>
nmap <C-J> gj
nmap <C-K> gk


"nmap :W :w
"set statusline=%f%m%r%h%w\ [%03l,%03v][%p%%]\ [LEN=%L]


if has('syntax') && (&t_Co > 2)
  syntax on
endif

function InsertTabWrapper()
  let col = col('.') - 1
  if !col || getline('.')[col - 1] !~ '\k'
    return "\<tab>"
  else
    return "\<c-p>"
  endif
endfunction

« Last Edit: December 06, 2006, 06:16 AM by jgpaiva »

Gothi[c]

  • DC Server Admin
  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 873
    • View Profile
    • linkerror
    • Donate to Member
Re: Vim: important usage tips
« Reply #1 on: December 06, 2006, 05:55 AM »
I'm also an intensive vim user, though I haven't spend too much time looking for tweaks. My vimrc is extremely basic, line numbering, syntax highlighting, 2 spaces instead of a tab, no line wrapping, etc,...


So, here's mine:

syntax on
set wildmode=list:longest,full
set showmode
set showcmd
set shortmess+=r
set mouse=a
set nowrap
set shiftwidth=2
set tabstop=2
set expandtab
set autoindent
set number
set title
set ttyfast
set shiftround
filetype on
set ignorecase
set smartcase
set incsearch
set gdefault

housetier

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 1,321
    • View Profile
    • Donate to Member
Re: Vim: important usage tips
« Reply #2 on: February 01, 2009, 04:36 PM »
ooh an old thread! let's revive it with

housetier's vimrc
version 6.0
set nocp
set backspace=indent,eol,start
set hid
set hls
set mouse=a
set ts=4
set et
set sw=4
set tw=80
set sta
set noscs
set ic
set is
set ww+=<>[]
set nu
set swb="useopen,split"
set nojs
set fo+=tcroq
set sc
set smd
set vi+='1000,\"500,:100,/100,h
set stl=%F%m%r%h%w\ type=%Y\ ascii/hex=\%03.3b/\%02.2B\ pos=%04l,%04v\ len=%L
set ls=2
set dy=lastline
set sr
set bg=dark
set enc=utf8
filetype plugin indent on
let g:sql_type_default = 'mysql'
syntax on
colo literal_tango
set nocul
set si

" ~/.vim/syntax/php.vim
let php_sql_query = 1
let php_baselib = 1

set visualbell

" insert a UUID at curser in insert mode
inoremap <F6> <C-R>=system("uuidgen")[0:-2]<CR>
" insert timestamp at cursor in insert mode
inoremap <F5> <C-R>=strftime("%s")<CR>

let mapleader = ","

au BufNewFile,BufRead *conkyrc set ft=conkyrc


ewemoa

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 2,922
    • View Profile
    • Donate to Member
Re: Vim: important usage tips
« Reply #3 on: February 04, 2009, 10:15 PM »
Perhaps for my own future reference, the following was the state of my vimrc when I stopped using it regularly (was still evaluating it, but just didn't keep up with figuring things out I guess) -- warning verbose content ahead.

vimrc
" .vimrc
"   of all places, started using gvim on ms-windows...
"
" important notes for this vimrc
"
"   * using this vimrc with a portable installation of gvim
"
"   * if GVimPortable was started via GVimPortable.bat.lnk, then $HOME
"     should already point to the correct location -- this is how $HOME
"     is set so that it is not dependent on the underlying OS remembering
"     anything about it (consequence of desiring a portable set-up)
"
"   * using NTFS hardlinks to relocate the data directory outside of the
"     portable application installation directory (trying to keep
"     applications and data separate)
"
"   * this vimrc 'depends' on:
"
"       mru.vim
"       taglist.vim (and exhuberant ctags)
"       winmanager.vim
"       ColorSamplePack.vim
"       confirmQuit.vim
"       minibuffexpl.vim
"
" still trying to learn how to / wish list:
"
"   * evaluate current line (or specify by range?) in this file
"       -> :execute getline(".")
"       
"   * how do you get a buffer being displayed in a particular
"     window to be freed and have another buffer take its place?
"   
"   * how to determine whether certain commands already have
"     default keybindings: :cn, :tabnew, etc.
"
"   * how to choose keybindings for things that aren't already,
"     random choice seems like it be a problem later
"
"   * how to customize popup menu
"
"   * how to determine what a given key sequence is bound to,
"     if it is already used
"
"   * how to select/copy/cut/paste column blocks
"
"   * how to open current buffer/window in new tab
"
" still reading:
"
"   * check out Help menu items
"   * howto.txt
"   * cmdline.txt
"   * autocmd.txt
"   * quickfix.txt
"   * fold.txt
"   * insert.txt - ft-javascript-omni
"   * various.txt - :helpgrep
"   * http://vim.wikia.com...as_an_IDE_all_in_one
"   * http://vim.wikia.com...wiki/Vim_on_Freenode
"   * http://www.vi-improved.org/tutorial.php
"   * index.txt - z (Commands starting with 'z')
"
" ideas:
"
"   * periodically analyze various histories (e.g. cmdline)
"     and possibly make a menu of frequently used commands
"
"   * look into ShowMarks:
"       http://www.vim.org/scripts/script.php?script_id=152
"
"   * look into CRefVim:
"       http://www.vim.org/scripts/script.php?script_id=614
"
"   * look into exUtility:
"       http://www.vim.org/scripts/script.php?script_id=1729
"
"   * look into CCTree:
"       http://www.vim.org/scripts/script.php?script_id=2368
"
" nice tips:
"
"   * :tab ba   " for each buffer in buffer list open in tab
"
"   * :tabm     " make current tab the rightmost one
"
"   * :sbn      " split window and go to next buffer in list
"
"   * q:        " cmdline-window -- use C-c to get to cmdline
"
"   * xp        " transpose characters
"
"   * "*yy      " copy a line to clipboard
"
"   * "*p       " copy from clipboard to text
"
"   * C-PgUp    " previous tab
"
"   * C-PgDn    " next tab
"
"   * :vimgrep /<pattern>/ ../**/* " recursively search for <pattern> in
"                                  " files
"
"   * C-t       " indent
"
"   * C-d       " dedent
"
"   * gq]/      " format comment (position cursor at start of comment first)
"
" already use a fair bit:
"
"   * :         " cmdline
"
"   * /         " search - use ? for search backwards
"
"   * :s        " substitute :[range]s/pattern/repl/[flags]
"
"   * :bd       " delete buffer
"
"   * :bn       " next buffer
"
"   * :tabnew   " new tab
"
"   * :tabclose " close tab
"
"   * :edit .   " explorer current directory
"
"   * :MRU      " recent files buffer - via plugin
"
"   * :WMToggle " windowmanager - via plugin + vimrc
"
"   * :Tlist    " taglist - via plugin
"
"   * gf        " edit file whose name is under or after cursor
"
"   * u         " undo
"
"   * C-w c     " close window
"
"   * C-w o     " make current window the sole window for current tab
"
"   * C-w s     " horizontally split current window
"
"   * C-w v     " vertically split current window
"
" XXX: not necessary when $HOME is set?
"let s:myhome = 'P:\home\GVimPortable\Data'
let s:myhome = $HOME
let s:mysettings = s:myhome . '\settings'

" no tabs please
set expandtab

" to get various things to work...
set nocp
filetype plugin on

" more command history
set history=500

" row and column in mode/status line
set ruler

" hilighting based on deduced structure of content
syntax enable

" searching options
set hls
set is

" don't want toolbar -- add 'T' to following string if reverting
" default was: set guioptions=egmrLtT
set guioptions=egmrLt

" recent-files plugin
let MRU_File = s:myhome . '\_vim_mru_files'
let MRU_Max_Entries = 40

" taglist plugin
" XXX: why doesn't the configuration file seem to work?
"let Tlist_Ctags_Cmd = 'P:\unix\bin\ctags.exe --options="' . s:myhome . '\ctags.cfg"'
"let Tlist_Ctags_Cmd = 'P:\unix\bin\ctags.exe -V --options=ctags.cfg'
let Tlist_Ctags_Cmd = 'P:\unix\bin\ctags.exe -V'
let Tlist_Show_Menu = 1
let tlist_javascript_settings =
  \'javascript;f:function;c:class;m:method;v:variable;I:inner'

" winmanager plugin
map <c-w><c-t> :WMToggle<cr>
map <c-w><c-f> :FirstExplorerWindow<cr>
map <c-w><c-b> :BottomExplorerWindow<cr>
" XXX: default is:
"let winManagerWindowLayout = 'FileExplorer,TagsExplorer|BufExplorer'
let g:winManagerWindowLayout = 'FileExplorer,TagList|BufExplorer'
let g:winManagerWidth = 40

" minibufexplorer
"   \mbe to start
"   \mbc to close
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplModSelTarget = 1

" color scheme: black background
"colorscheme torte
"colorscheme koehler
" http://www.cs.cmu.ed.../VimColorSchemeTest/
" dante is from ColorSamplePack (plugin) others may be too
"colorscheme desert256
"colorscheme ChocolateLiquor
"colorscheme lilac
"colorscheme camo
"colorscheme desert
"colorscheme freya
"colorscheme neon
"colorscheme umber-green
"colorscheme *zenburn
"colorscheme *tabula
"colorscheme candycode
"colorscheme lettuce
"colorscheme darkslategray
"colorscheme darkblue
"colorscheme midnight
"colorscheme midnight2
"colorscheme navajo-night
colorscheme dante

" try this font
set guifont=Lucida_Console:h14:cANSI

" default viminfo was: '20,<50,s10,h,rA:,rB:
" just trying to get vim to save the viminfo file at some decent location
" XXX: may be there's a simpler way to do this
execute 'set viminfo+=' . 'n' . escape(s:myhome . '\_viminfo', '\')

" confirmation before various things
set confirm

" confirm before quitting
execute 'source ' . escape(s:mysettings . '\confirmQuit.vim', '\')

" get rid of variables?
unlet s:myhome
unlet s:mysettings


kartal

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 1,529
    • View Profile
    • Donate to Member
Re: Vim: important usage tips
« Reply #4 on: July 10, 2009, 11:28 AM »
Try adding this script to your script arsenal. It is really helpful and works great. Make sure that you modify default behaviours.

autocomplpop.vim