MySQL DBA - Tips and Techniques Magazine

6 Oct 2014

LINUX Text Editor : Vim (Vi iMproved)

VIM docs : http://www.vim.org/docs.php


sudo yum install vim   


cp $VIMRUNTIME/vimrc_example.vim ~/.vimrc        (sets up vim config file)


vimtutor         (20 minute hands on tutorial)

 

USEFUL commands:


               format:                operator number motion  

eg           d2w  = delete two words

 

               DELETE (CUT) / YANK (COPY)  - both these commands


               dw -delete to next word

               de - delete to end of this word

               d$ - delete to end of line

               dd  - delete whole line

               2dd  - delete two lines

               d2w - delete 2 words

               yy - YANK (cut the line)

               y2 -  cut two lines

               y5w - yank five words


PASTE


p -paste after cursor ,     (can past deleted words/lines)

P (capital P)- paste before cursor

              

UNDO/REDO


               u - undo last change

               U - undo all changes

               CTRL-R - redo

 

CHANGES


r - replace character

R - overwriting

ce - change till end of current word i.e. deletes word and lets you type in new one

               cw - change till beginning of next word

               c$ - change till end of line

               c2w -change 2 words

               I - insert before the cursor

               a - append after the cursor

               A -append at end of line

 

SEARCH


/  -forward search  , n - finds next , CTRL-o  goes back to previous found word

? -backwards search, n finds next

:set ic   (ignore case)

:set noic (no ignore case i.e turn ignore case off)

:set hls  (highlight search - all occurances highlighted)

:set nohls (turn highlight off)

:set ic (incremental search)

:set noic (turn off)

 

REPLACE


:s/old/new  - replace first one on current line

:s/old/new/g  - replace all on current line

:%s/old/new/g   - global replace

:%s/old/new/gc  - global replace and confirm first

 

MATCHING BRACKETS


put cursor on opening bracket, press % , goes to closing bracket

 

NAVIGATING


               2w - jump two words

               0 -beginning of line

CTRL-G displays file name,  current line number / total lines, %age f way thru doc

G move to end of file

5G - move to line 5

gg - move to first line

 

EXTERNAL COMMANDS:


!

!ls -l

 

SAVE/REMOVE FILE


!wq

!w test

v  (then highlight selection)   :w selection.txt

!rm test

 

INSERT AN EXTERNAL FILE


:r  text.txt

:r !ls


USE SHORTCUT TO ENTER COMMANDS


:e   CTRL-D  (shows all commands beginning with e) , press TAB - fills command

.   - repeats last command

3J - joins next three lines into one long line

 

No comments:

Post a Comment