Any word completion

Ctrl+n - While typing text, display a list of possible words, based on file contents and #includes found

Copy, cut, and paste

Basic clipboard operation

  1. Move the cursor to the initial position
  2. Press v (or V to select whole line)
  3. Move the cursor, selecting the desired content
  4. Press d (delete) to cut, or y (yank) to copy.
  5. Go to destiny location
  6. press P to paste

Cut or copy a single line

dd - cut current line

yy - copy current line

Multiple lines

Tipe a number just before dd or yy to cut or copy a number of lines, including the one where cursor is located.

example: 10dd - cut 10 lines

Managed clipboard

It is possible to give different names to your cutted or copied contents:

Before steps 4 and 6, press " (quotes) followed by a single letter. For example:

"zd - cut and stores and stores in a position named z

"zp - paste contents from z position

:reg - list slots

Paste from another file

:r /home/paulodev/filetoinject - Injects /home/paulodev/filetoinject contents, starting at current cursor position

Auto identation mess

If you have autoindent turned on in your vim, you will experiance some trouble when pasting already indented source code.

:set paste - Turn on paste mode, for safe pasting code.

:set nopaste - Turn off paste mode.

 

Diff against SVN

Create /etc/vim/plugin/diffsvn.vim file, with the following contents:

function! s:DiffWithSVNCheckedOut()
  let filetype=&ft
  diffthis
  vnew | exe "%!svn cat " . expand("#:p")
  diffthis
  exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
endfunction
com! DiffSVN call s:DiffWithSVNCheckedOut()

from: http://vim.wikia.com/wiki/Diff_current_buffer_and_the_original_file

Diff buffer against saved version

Create /etc/vim/plugin/difflocal.vim file, with the following contents:

function! s:DiffWithSaved()
  let filetype=&ft
  diffthis
  vnew | r # | normal! 1Gdd
  diffthis
  exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
endfunction
com! DiffSaved call s:DiffWithSaved()

from: http://vim.wikia.com/wiki/Diff_current_buffer_and_the_original_file

Execute external commands

:!command - Execute the command in shell, and wait for pressing [return] 

Append command output to text

:read !command - Execute the command and injects the output in the current file

 Using the current filename (%) in external commands

:!echo % - Does an echo showing the current filename. % is the symbol for that

 Mapping keys to run external build commands

:map <F7> :w !command %<CR> - Maps the F7 key to save the file, then run the custom command on it.

Filtering things

Filter lines using grep

Press V and highlight desired lines, or 1GVG to select all

Press ! and vim will automatuically put :'<,'>! in command buffer (because there are selected lines). After it, type your grep command.

ex: :'<,'>!grep "Keep only this line"

Filter lines using cut

Same proces, but using cut command. This example keeps only the second field (-f2) , in a set of comma separated values (-d,)

ex: :'<,'>!cut -f2 -d,

Find text

Normally, searches, in vim starts with / (slash), and [return] must be pressed to confirm:

/searched text - Go no the first occurence. Press n to search forward, and N to search backwards

/\<exactword\> - Locate exact word. You can also press * (asterisk) just after the method above to turn it in a exact word search

/this\|that - search for this OR that. Note that | (pipe) is used for OR, but must be escaped to work properly

Other kinds of search (not starting with slash):

:g/searched text/# - List occurences and line numbers

Search current word or selection

Press * and # to search the word at current position, or the text selected with v

Jump to line

:123 - jump to line 123

G - jump to the last line

Line numbers

:set number - display line numbers

:set nonumber - hide line numbers

:hi LineNr ctermfg=DarkGrey - Show line numbers in DarkGrey color (:help cterm-colors to list available colors)

Setting as default

You can put this command without the : (colon) inside /etc/vim/vimrc, /etc/vim/vimrc.local or ~/.vimrc (depending on your environment configuration) to always display line numbers and set the display color for them.

Macro

qa - Start recording at slot a. q to stop recording. You can user lowercase letters from a to z as slots.

@a - run macro stored at slot a.

Protect file contents with encryption

Protecting a file

vim -x <filename>: Vim asks for a encryption key (or password) before showing contents. If the file is not encrypted, it will became when saved. If you open the file again, regardless the -x parameter, vim will ask for the key, and shows binary unreadable contents if wrong.

Change password / remove protection

vim +X <filename>: Change the encryption key. Provide blank twice to remove the encryption and restore the file as normal text after saving.