Educating yourself does not mean that you were stupid in the first place; it means that you are intelligent enough to know that there is plenty left to 'learn'. -Melanie Joy

Tuesday 5 June 2012

vim/gvim-Tips and Tricks

June 05, 2012 Posted by Dinesh No comments

Tip 1 :

Use markers to set a place where you want to go back quickly or to delete some block of code

ma   - mark current position as 'a' [ can use a-z in same file, or can use A-Z between files]
'a     - go to mark a
d'a   - delete current position to mark a [ can use 'p' after this to paste that block ]

 

Tip 2:

 Do you want to reverse the entire file or a block ?? its easy. Try the following command
     :g/^/m0                          [ Reverse file ]
    :'a, 'bg/^/m'b                   [ Reverse a section a to b ]

 

Tip 3:

Want to delete all empty lines from a file, here you go

    :g/^\s*$/d 

Tip 4: 

Display all lines which matches the pattern
    :g/<pattern>/
    :g/<pattern>/#                     [ display all line with line numbers (in vi) ]


Tip 5: 

Finding some successive lines
    :/^\n\{3}                                     [ find 3 successive empty lines ]
    :/\(^str.*\n\)\{2}                          [ find 2 successive line starting with str ]  
    :%s/^\n\{3}//                              [ delete block of 3 empty line ]    
    :%s/^\(.*\)\n\1$/\1/                    [ delete duplicate lines ] 
    :%s/^\(.*\)\(\n\1\)\+$/\1/            [ delete multiple duplicate line ]
    :%s/\v(.*\n){5}/&\r                     [ insert a blank line every 5 lines (can write a string also after & )]

 

Tip 6:

changing the case
    guu or Vu              [ lower case line ]
    gUU or VU           [ uppercase line ]
    vEu                        [ lower case word ] 
    vEU                       [ upper case word ]
    g~~                        [ flip case ]
    guG                       [ lower case entire file (go to line 1 and execute the command) ]
    gUG                      [ upper case entire file (go to line 1 and execute the command) ]


 Tip 7: 

    gf         [ Go to  file name under curser ]
    gd        [ Go to declaration of local variable under cursor ]
    gD       [ Go to declaration of global variable under cursor ]

 Tip 8:

*In ESC mode

    vib         [ Select a block - selecting function arguments ( ) ]
    viB         [ Select a block - selecting entire function definition { } ]  - useful when indenting a function(viB and = )
    ci"          [ Edit content within " " ] - useful when replacing string
    ci'          [ Edit content within ' ' ]

0 comments:

Post a Comment