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

Showing posts with label vim/gvim. Show all posts
Showing posts with label vim/gvim. Show all posts

Sunday, 30 June 2013

A Bit Close Look At Vi - More On 'registers'

June 30, 2013 Posted by Dinesh , No comments

Note - read Marks & Registers before reading this post.

There are some other ways of copy or delete text in vi.

Working with paragraphs:

If you are in paragraph text you can use  }  and  {  to move  cursor to the beginning or ending of the paragraph respectively.  so if you want to move a paragraph you can use  {  and  d} 
and   p  to paste the content.  if you are already in beginning or ending of a paragraph you can use   d}  and  d{  respectively to cut the paragraph.
If you want to delete 3 continuous paragraphs you can use  d3} (From current to next 3 paragraphs will be cut) 
And if you want to move multiple paragraphs from multiple locations to other locations we can make use of registers ( Marks & Registers ).

Ex:
I am at para 1: hit   "ad3}  to cut 3 paragraphs  to the register 'a'
I am at para 9: hit   "by}  to copy next paragraph to the register 'b' 
At EOF: hit  "ap  and  "bp   to paste the paragraphs.

Copy/Cut by Search:

Search using  /  or  ?  is most notable technique in vi. But if you want to copy the content from current line to the next line containing a specific word ??.
for example one can use  y/foo  to copy the text from current line to next line containing the word 'foo'. similarly 'd' can be used to cut ( d/foo).
y?foo to copy from the current line to the most recent (previous) line containing 'bar'.

Similarly as above we can use registers to copy/cut these content.
Thus if you use "ay/foo then you are yanking a copy of the text from here to the next line containing "foo" into the 'a' register. and can use   "ap  to paste the content.


Friday, 28 June 2013

Vi - Marks and Registers

June 28, 2013 Posted by Dinesh ,

Marks:

vi has 26 "marks". A mark is set to any cursor location using the m command. 
Each mark is designated by a letter. 
Thusma sets the 'a' mark to the current location, andms sets the 's' mark. 
 You can move to the line containing a mark using the ' (single quote) command. 
 Thus 'a moves to the beginning of the line containing the 'a' mark. 
 You can move to the precise location of any mark using the ` (backquote) command. 
 Thus `s will move directly to the exact location of the 's' mark.

copy/cut/paste :

One can use d,y,p to delete,yank,paste respectively. 
step 1 : set the mark by using m
step 2 : y'a to copy from current position to line where mark a is set.
             y`a to copy from current position to exact location of mark a.
             [ 'd' can be used similarly ]
step 3 : use p to paste the content.

To access all currently defined markers hit :makrs

some other interesting features are there in vi where we can use marks.

# for example if you want to sort a set of lines in vi what you do ?? There is a simple solution for it.
place two marks on two lines. and then you you hit :'a,'b!sort ( ! will execute shell commands on vi)

# if you want to redirect a part of file to another file
place two marks on two lines and then :'a,'b w temp_file. this will copy the content from mark a to b to the new temp_file. if file already exist use :'a,'b w>>temp_file to append.
  
Note: a-z can be used to mark only in same file. use A-Z to mark across the files.. So now copy a line from one file to other file is much more easier !! 

Registers:

vi has 26 "registers". we can use these registers by prefixing  " (double quote)Each register is designated by a letter.
Thus"a starts the register. now its up to you to use this register to copy the content to it or cut the content to it or paste the content from it.

This is more useful when you want to copy lines from multiple locations and paste it in different places.

copy/cut/paste :
Ex:

I am at line 2: "ayy will copy the current line to register to a
I go to line 30:  "bdd will cut the current line to register b.
I am at the end of file : "ap  and "bp  will paste line 2 and line 30 at end of file.

"ap pastes a copy of the 'a' register's contents into the text after the cursor and "aP pastes a copy from 'a' to before the current line.

capital letters can be used to append the content to a register. 
 "Ayy will append the current line to the register A.

To access all currently defined registers hit :reg

We can do some interesting things with registers.
for example if you want to execute a command which is at current line in vi what you do ?? There is a simple solution for it.
Copy the line you want to execute to a register -  "ayy 
Execute the content of register using '@'  -  :@a 




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 ' ' ]

Monday, 4 June 2012

Creating A List Using vim

June 04, 2012 Posted by Dinesh No comments
Some time you may need to insert a list of ascending numbers in a file. We can do this using vim very easily.

Method 1 :

:put =range(11,15)


This will create a series of increasing numbers after the current line including 11 and 15.
output will be-     

11
12
13
14
15
 
Method 2 :
Some times if you want to add a constant predefined string before the number series, then you you can go using loop to create a list.
       
for i in range(1,6) | put ='10.168.0.'.i | endfor

Executing the command will insert the following lines after the current line

10.168.0.1
10.168.0.2
10.168.0.3
10.168.0.4
10.168.0.5
10.168.0.6
   
Method 3 : 
Another simple way is to use Ctrl+A in a macro.
for example, some times you may need to initialize a array of size 100.
array[0] = 0
array[1] = 0
array[2] = 0
... so on

type array[0] = 0; then start recording macro.
Type the following commands without pressing enter.

qa          [ record to buffer 'a' ]
Y           [ copy the current line ] 
p           [ paste the line ]
Ctrl-A      [ Ctrl+A increment the number or use Ctrl+X to decrement]
q           [ stop recording macro ] 

now type 100@a to perform the macro 100 times.

array[0] = 0;
array[1] = 0;
array[2] = 0;
array[3] = 0;
array[4] = 0;
... and so on

if you want to increase the index and assigned value also then type Ctrl+A by moving the courser to the value.