Tuesday, 8 May 2012

Learning to make vim fast and lovable editor

.. Or settings and tips I have learnt to make vim nicer, but are not on by default. ( I will keep adding to this )

I am using vim as my main editor for a few years but since freelancing I am learning to use it even more.

Vimrc settings

My changes to vim at the moment look like this

syntax on
set bg=dark 
" indent
set cindent
set smartindent
set autoindent 
" tabs
set expandtab
set shiftwidth=2
set tabstop=4
set showmatch " Show matching brackets.
" auto complete + C-n
set showcmd " Show (partial) command in status line.
" case search
set ignorecase " search ingore case if all lower search
set smartcase " Do smart case matching (flips igonore if use any caps)
set incsearch " search as type
" highlight search
set hlsearch " highlight all searches " :nohlsearch
"set visualbell
"set cinkeys=0{,0},:,0#,!,!^F
set cinkeys=0{,0},:,0#
set cinwords=if,else,while,do,for,switch,case   " Which keywords should indent
imap <C-e> <esc>$a
imap <C-l> <esc>g_
:nmap <C-n> :tabnext<cr>
" Press Space to turn off highlighting and clear any message already displayed.
:nnoremap <silent> <Space> :nohlsearch<Bar>:echo<CR>
" Press F4 to toggle highlighting on/off, and show current value.
:noremap <F4> :set hlsearch! hlsearch?<CR>

Outside of the config you can open files in tabs inside vim and then tab between them
vim -p file1 file2 file3
then use 'tg' to go to the next tab

Open a grep search of files as tabs in vim
vim -p `fgrep -l cust_function core/*.php`
This would search for 'cust_function' in the dir core for php files

Vimdiff - visual character level diff from cmd line

vimdiff is much nicer to look at if you have to understand merging files on the cmd line.
if you migrate from

line by line without checking white space (so ignore tabs/spaces which can change with editors and versioning)
diff -w filea.php fileb.php
 files side by side if you need to see context of more than a couple of changes
diff -wy filea.php fileb.php
In this view you can scroll up and down but right edge gets cut, you can see marks in middle of page
| different
> change to right version
< change to left version

But those two show only lines. Vimdiff shows colours to show character changes and move around in a visual way
 vimdiff filea.php fileb.php
And if you add this to ~/.vimrc you dont see the white space changes which is nice.
set diffopt+=iwhite
When in vimdiff you can move between windows and also which is nice, push and pull changes between versions.
So move to change "dp" pushes change from left to right side. The other way "do" pulls it from right to left

To switch windows in vimdiff or to navigate windows in vimdiff or to change windows in vimdiff try the following: The ":vertical" command can be inserted before another command that splits a window. 
CTRL-W h        move to the window on the left         CTRL-W j        move to the window below
CTRL-W k        move to the window above 
CTRL-W l        move to the window on the right         CTRL-W t        move to the TOP window      
CTRL-W b        move to the BOTTOM window 
Moving windows  
CTRL-W K        move window to the upper 
CTRL-W H        move window to the far left
CTRL-W J        move window to the bottom         CTRL-W L        move window to the far right 

Have Vim jump to the last position when reopening a file
if has("autocmd")
  au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")
      \| exe "normal! g'\"" | endif

Auto complete comment block lines.
" In edit mode
" #b<return>
" type text
" #e<return>
:ab #b /************************************************
:ab #e ************************************************/
:ab #l /*----------------------------------------------*/

Show hidden characters, leading and tailing white space with
:set list
Then you can see if you have lots of white space as $ will be on end of lines
Next you can run a zap trailing white space
:1,$s/[ <tab>]*$//
and off with
:set nolist

source: http://www.oualline.com/vim-cook.html

Not quite vim but useful

You can get a sort of auto complete from your history file. So for example
cd PageUp
changes to
cd /file/path/i/used/earlier

or if your jumping around
cd /file/path/PageUpPageUp
becomes the last bit
cd /file/path/i/used/before/that

Change it here:
vim /etc/inputrc
Then uncomment the lines for using PageUp and PageDown to fill the rest of your cmd propt from your history
# alternate mappings for “page up” and “page down” to search the history
#”e[5~”: history-search-backward
#”e[6~”: history-search-forward