Table of Contents

VI tips

Quick save

Press SHIFT + ZZ which is equivalent to :x which is equivalent to :wq, except that it only saves if the file has been changed.

Set tab size and convert tabs to spaces

:set tabstop=4
:retab

Turn off autoindent when you paste code

To toggle this in VI on the fly to switch off auto-indent

:set paste

and to switch it back on

:set nopaste

In .vimrc this can be set:

set copyindent

Another alternative via .vimrc is:

set clipboard=unnamed

That makes the default paste buffer map to X's clipboard.
When a bit of text is highlighted in a terminal, it can be pasted into vim by simply press p to paste. Similarly, one can yank things in vim (e.g. YY to yank the current line into the buffer) and middle click in any window to paste it.

Default 4 tab spaces

in ~/.vimrc

filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab

Cut, Copy & Paste

  1. Press v to select lines or test with cursor keys
  2. Then press y to copy the selection or press d to cut the selection
  3. Then press '> to skip to the end of the selection
  4. Then press p to paste under the cursor or SHIFT+p to paste after the cursor
  1. Copy the entire line by pressing yy (more info :help yy ) or cut using dd.
  2. Paste the line by pressing p.

Copy whole file to clipboard

The clipboard is buffer +. To copy to clipboard, do “+y and [movement].
So, gg”+yG will copy the whole file.

Similarly, to paste from clipboard, “+p

Alternatively:
gg”*yG
:%y+

Search & Replace

:%s/bla/blubs/

Complex replace with reference:

:%s/rtrim(\(.*\),"0")/zerotrim(\1)/

to repeat, press “:”, then the cursor up