Andries filmer

Feel free with Open Source Software

Andries Filmer - Internet professional sinds 1996.
Ik ben groot voorstander van Free- en Opensource Software (FOSS) en laat deze site jouw overtuigen waarom dit goed is.
Home Over deze website Kennisbank Ezelsoren Freelancer Online gereedschap

Vim Cheatsheat - Een geweldige editor met een lange leer curve

Inhoud
  1. Basics
  2. Search / replace
    1. Search and replace path's
    2. Replace spaces
  3. Visual mode
    1. Visual block mode
  4. Windows
  5. Tabs
  6. Marks
  7. Set options
  8. .vimrc
  9. sudo
  10. Plugin's
    1. Favorieten
    2. Surround
    3. Matchit
    4. phpFolding
    5. Versleutelde bestanden bewerken
  11. Advanced
    1. Order text
    2. Numbering lines in a file
    3. Macros
    4. Small macro
    5. Tabs and Spaces
  12. Links
  13. Commentaren

Het is een editor met een leer curve. Je gaat Vim waarderen als je veel met terminals werkt. Vim is een editor met meerdere modes

  • command mode
  • insert mode
  • visual mode

Met 'Esc' ga je altijd naar 'command mode'. En met het commando :q! sluit je een bestand zonder wat te doen.

Basics

 vim filename       " Open filename
 :E                 " Open Explorer, o	Open in new window, O Open in previous window
 i 	            " enter insert mode so you can start typing 
 Esc                " leave insert mode , back to command mode
 G                  " goto end of file 	
 :54                " goto line 54
 dd                 " cut current line 	
 yy                 " copy current line
 u                  " undo 	
 Ctrl+r             " redo 	
 .                  " repeat
 :w                 " save file
 :wq                " save file and quit
 :<command>         " runs named command: Example :q!
 :help word 	    " shows help on word: Typing Ctrl+d after word shows all entries containing word

Search / replace

 /regexp            " searches forwards for regexp, ? reverses direction
 n                  " repeat search, N reverses direction
 *                  " searches forward for word under cursor, # reverses direction
 :%s/foo/bar/gc     " search for 1 and replace with 2, options are: g = global (entire file), c = confirm change

Search and replace path's

Using a another separator ":" is useful when doing file path search and replace. This way you don't have to escape every "/" with "\/".

 :%s:/dir1/dir2/dir3/file:/dir1/dir4/dir5/file1:gc

Replace spaces

 :%s/\s\+$//                   " Delete all trailing whitespace (at the end of each line) with
 :%s/^\s\+//                   " More rarely, you might want to delete whitespace at the beginning of each line

Visual mode

 v                             " select visually, use cursor keys
 Shift+v                       " select lines
 Ctrl+v                        " select column
 ~                             " Switch case, lower to upper or upper to lower case
 D                             " cut selection 
 y                             " copy selection 
 p                             " paste (after cursor), P is paste before cursor
 >                             " indent section
 <                             " unindent section, remember . to repeat and u to undo
 gq                            " format lines to 'textwidth' length 

Visual block mode

Copy text in column

  1. In VIM, press Ctrl+V to go in Visual Block mode
  2. Select the required columns with your arrow keys and press y (or press x to cut) them in the buffer.
  3. Move cursor to where you want to insert the text and press p (or press P to past before cursor) in command mode.

Insert text in column

  1. In VIM, press Ctrl+V to go in Visual Block mode
  2. Select the required columns with your arrow keys where you want to insert text and press I.
  3. Type tekst (you only see one row which you type)
  4. Press then ESC, whola the other rows are also inserted with the text from the 1st row you typed.

Windows

 :sp                           " split window horizontal
 :sv                           " split window vertical
 CTRL-W <DOWN>                 " Move to window down
 CTRL-W <UP>                   " Move to window up
 :q                            " Quit window, :close Close window
 :new                          " Open new window, :vne for new vertical window
 :help windows                 " More help for windows

Tabs

 vim -p file1 file2 file3      " open files in tabs

 :tabnew <filename>            " Open file in new tab
 :tabf file*                   " Findtab
 :tabn                         " Nexttab
 :tabp                         " previoustab
 :tabr                         " Po to the first tab
 :tabl                         " Go to the last tab
 :tabm n                       " tab move to the position n 
 :tabdo %s/foo/bar/g           " Command in all tabs
 :help tab-page-intro          " More help for tabs

Marks

 m{a-z}                        " Mark position as {a-z} E.g. ma
 '{a-z}                        " Move to position {a-z} E.g. 'a
 ''                            " Move to previous position 
 :marks                        " Print active marks

Set options

 :set wrap                           " Wrapping on
 :set nowrap                         " Wrapping off

Or make them permanent in ~/.vimrc

.vimrc

My default .vimrc
 set paste                           " Set paste mode, prevent accumulation of tabs and #'s
 set incsearch                       " Set increase search,	Search while you type
 set ignorecase                      " Set ignore case, Search case insensitive
 set list                            " See end of line's
 set list listchars=tab:»·,trail:·   " Make tabs visual: ».......
 set encoding=utf-8
 "set number                         " Set line numbers, default commented out
 "set wrap                           " Wrapping on, default commented out

sudo

Als je tijdens het opslaan merkt dat je niet genoeg privileges hebt is er een snelle oplossing door in Vim de volgende one-liner als commando in te geven:

 :w !sudo tee % 

Daarna merkt Vim dat het bestand 'extern' aangepast is, en accepteer je de wijzigingen door op L (voor Load File) te drukken.

Plugin's

Favorieten

Download [http://www.vim.org/scripts/download_script.php?src_id=1744 FavEx plugin]

  1. Unzip favex.zip, preserving directory structure, into either $HOME/.vim
  2. Start Vim
  3. Do :helptags ~/.vim/doc
  4. Do :help favex to learn about usage and customization.

 :FF                    " Add Favorite File
 :FD                    " Add Favorite Directory
 :FE                    " Favorits Explorer
 :FS                    " Favorits Split

Surround

Handig om tags te maken, veranderen of te verwijderen.

Verwijder de tag op de regel

 dst

Een tag toevoegen

 Vs<p>
of
 viws<a href=''>

of een tag aanpassen

 viws<div>

http://www.vim.org/scripts/script.php?script_id=1697

Matchit

Deze plugin is handig om tussen tags, brackets of haakjes te springen. Ga in een tag, bracket of tussen haakjes staan en druk op "%".

http://www.vim.org/scripts/script.php?script_id=39

phpFolding

Klapt classes, funkties dicht en open.

http://www.vim.org/scripts/script.php?script_id=1623

Versleutelde bestanden bewerken

Als je versleutelde bestanden wilt bekijken of aanpassen, dan ben je waarschijnlijk geneigd om ze eerst te ontsleutelen en ze daarna te openen in je favoriete editor. Gebruik je als editor Vim en voor het versleutelen GnuPG, dan handelt de GnuPG-plugin dit allemaal transparant af.

De installatie van de plugin is eenvoudig: kopieer het bestand gnupg.vim naar de directory $HOME/.vim/plugin. Daarna voeg je de volgende regel toe aan .bashrc.

 export GPG_TTY=`tty`

Advanced

Order text

Shift current line and next 8 lines to right shiftwidth spaces
 9>>   

Shift current line and next 8 lines to left shiftwidth spaces

 9<<   

Numbering lines in a file

A neat filtering trick using the 'nl' linux/unix program.

 :%! nl -ba  

Macros

 qa             " starts recording the macro 'a'
 q              " stops recording the macro
 <num>@a        " repeat the macro <num> number of times

One way to use this is to create a number list

 insert 1       " goto insert mode and write 1
 qa
 yy
 p
 CTRL-A         " on the new line to convert the 1 to a 2
 q
 20@a

Voila! You have number 1 to 22 pasted in your file

So typically the keysequence would be 'i, 1., [esc], qa, yy, p, [CTRL-A], q, 20@a'

Small macro

 :set abbr netexpo Netexpo Internet Services B.V.

Tabs and Spaces

An endless argument in editors is whether to insert tabs or spaces while writing code. In order to satisy people on both sides, vim can be set to do exactly what people want. The 'expandtab' option when unset inserts tabs when you press the key and when set inserts spaces.

 :set expandtab        - for spaces
 :set noexpandtab      - for tabs

For people trying to meet the people on the other side, we can do the following steps to..

1 - Convert tabs to spaces

 :set expandtab
 :%retab!

2 - Convert spaces to tabs

 :set noexpandtab
 :%retab!

In order to visualize the above actions, it is useful to do ':set list' which turns on invisible characters (spaces, tabs, end of line etc).

Links

Deze pagina is gemaakt op 2009-04-12 en aangepast op 2011-04-15

Ik zou heel leuk vinden als je laat weten wat je van deze pagina vindt. Plaats gerust commentaar.
Je e-mailadres wordt niet gepubliceerd het is alleen om eventueel contact met je op te nemen.

 
Jouw naam
Jouw e-mailadres
Om te voorkomen dat robots dit formulier gebruiken vraag ik vriendelijk of je de volgende karakters wilt over typen.
 
Jouw Gravatar Commentaar van elwoode geplaatst op 2011-06-27
Nice site, If you like opensource should you not list a few of you favourite open source programs?
I also have a VIM question! I have line numbering set but I would like to have the number column
a different colour from the rest of the document, is that possilbe?
ps If you answer me it is fine to use Dutch.
best wishes.
Jouw Gravatar Commentaar van Andries geplaatst op 2011-06-27
Hi Elwoode,

There are to many open source programs ik like and they also change a lot ;)
In my footer are the most populair 'server-side' open source program's I use.

Read this page for syntax color highlighting
http://www.faqs.org/docs/Linux-HOWTO/Vim-HOWTO.html

 


Mijn Curriculum vitae | De content op deze website heeft de Creativecommons 3.0 licentie | © 2011
Andries Filmer | http://andries.filmer.nl | andries@filmer.nl | © 2011
Deze website wordt gerealiseerd met Free- en Open Source Software: | | | | | |