Vim

Uit De Vliegende Brigade
Ga naar: navigatie, zoeken

Bestand openen met cursor op een bepaalde positie

Bv. om weer 's een read error in een LibreOffice Calc-document te fiksen:

 vim "+call cursor(2,84862)" content.xml

Vim opent zo'n bestand trouwens beduidend sneller dan Nano.

Color schemes

Bv.

:colorscheme elflord

is al een stuk beter te lezen. En zoals vaker bij VI, zijn de mogelijkheden onbeperkt. Wat dacht je van een color scheme dat verandert met het uur van de dag? http://vim.wikia.com/wiki/Switch_color_schemes

Om dit blijven in te stellen: Voeg toe aan bestand ~/.vimrc

colorscheme elflord

Commentaarregels

Regels activeren

  • Ga met de cursor links op de eerste regel staan
  • CTRL-V - block visual of visual column mode of zoiets
  • Selecteer alles wat weg moet
  • X of DEL en de tekens zijn foetie.

Regels uitsluiten

  • Ga met de cursor links op de eerste regel staan
  • CTRL-V - block visuall of visual column mode of zoiets
  • Beweeg met de cursor naar de laatste regel die je wilt uitcommentariseren
  • SHIFT-I
  • Voeg de commentaar-symbolen voor de specifieke taal in
  • ESC
  • Wacht heel even...

Cursortoetsen werken niet in insert-mode & backspage werkt ook niet

Optie (1): Installeer vim

sudo apt-get install vim

Additioneel voordeel: Color highlighting.

Optie (2): Pas .vimrc aan

Zie

voor details.

Find

Find:

/

Find again:

n

Find & Replace

Complete handleiding van [1]:

:%s/foo/bar/g
   Find each occurrence of 'foo' (in all lines), and replace it with 'bar'. 
:s/foo/bar/g
   Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'. 
:%s/foo/bar/gc
   Change each 'foo' to 'bar', but ask for confirmation first. 
:%s/\<foo\>/bar/gc
   Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation. 
:%s/foo/bar/gci
   Change each 'foo' (case insensitive) to 'bar'; ask for confirmation. 
   This may be wanted after using :set noignorecase to make searches case sensitive (the default). 
:%s/foo/bar/gcI
   Change each 'foo' (case sensitive) to 'bar'; ask for confirmation. 
   This may be wanted after using :set ignorecase to make searches case insensitive. 

The g flag means global – each occurrence in the line is changed, rather than just the first. This tip assumes the default setting for the 'gdefault' and 'edcompatible' option (off), which requires that the g flag be included in %s///g to perform a global substitute. Using :set gdefault creates confusion because then %s/// is global, whereas %s///g is not (that is, g reverses its meaning).

When using the c flag, you need to confirm for each match what to do. Vim will output something like: replace with foobar (y/n/a/q/l/^E/^Y)? (where foobar is the replacement part of the :s/.../.../ command. You can type y which means to substitute this match, n to skip this match, a to substitute this and all remaining matches ("all" remaining matches), q to quit the command, l to substitute this match and quit (think of "last"), ^E to scroll the screen up by holding the Ctrl key and pressing E and ^Y to scroll the screen down by holding the Ctrl key and pressing Y. However, the last two choices are only available, if your Vim is a normal, big or huge built or the insert_expand feature was enabled at compile time (look for +insert_expand in the output of :version).

Also when using the c flag, Vim will jump to the first match it finds starting from the top of the buffer and prompt you for confirmation to perform replacement on that match. Vim applies the IncSearch highlight group to the matched text to give you a visual cue as to which match it is operating on (set to reverse by default for all three term types as of Vim 7.3). Additionally, if more than one match is found and you have search highlighting enabled with :set hlsearch, Vim highlights the remaining matches with the Search highlight group. If you do use search highlighting, you should make sure that these two highlight groups are visually distinct or you won't be able to easily tell which match Vim is prompting you to substitute.

Voorbeeld escape codes

Als je zoekt voor een line break, moet je \n gebruiken, maar als je een line break wilt invoeren, moet je \r gebruiken [2]

Voorbeeld van de brondata:

"8719468000003
"
"8719468000010
"
"8719468000027
"
"8719468000034
"
"8719468000041
"
"8719468000058
"
"8719468000065
"
"8719468000072
"
"8719468000089
"
"8719468000096
"

Commando VIm:

:%s/\n"\n/"\r/g

Oftewel:

  • Zoek voor \n"\n" - Line break, dubbele aanhalingstekens, line break
  • Substitute met "\r - Dubbele aanhalingstekens, line break.

Regel verwijderen

dd

Regelnummers

Ga naar regelnummer...

:1234

weergeven

:set number

of

:set no

Startup-opties

Startup-opties sla je op in bestand

~/.vimrc

Inhoud van .vimrc op de computer waar ik dit op tik:

set nocompatible
colorscheme elflord

Tekst selecteren, verwijderen & knippen

Samenvatting

v - begin selectie
y - copy (yank) of d - delete
p - paste

Uitgebreid

1. Position the cursor at the beginning of the text you want to cut/copy.
2. Press v to begin character-based visual selection, or V to select whole lines, or Ctrl-v or Ctrl-q to select a block.
3. Move the cursor to the end of the text to be cut/copied. While selecting text, you can perform searches and other advanced movement.
4. Press d (delete) to cut, or y (yank) to copy.
5. Move the cursor to the desired paste location.
6. Press p to paste after the cursor, or P to paste before. 

Visual selection (steps 1-3) can be performed using a mouse.

If you want to change the selected text, press c instead of d or y in step 4. In a visual selection, pressing c performs a change by 
deleting the selected text and entering insert mode so you can type the new text. 

Bron: http://vim.wikia.com/wiki/Cut/copy_and_paste_using_visual_selection

Undo & redo

  • Undo: u
  • Redo: CTRL-R

Word wrap

:set wrap
:set nowrap

Zie ook

Bronnen