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

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.

`Tar`- An Ultimate Archive Utility

June 04, 2012 Posted by Dinesh , , No comments
How to use tar
tar  [options] [name of tar file to be created] [list of files and directories to be included]

1. Creating an archive using tar
    
      $ tar -cvf target.tar file1 file2 dir1 
      $ tar -cvzf target.tar.gz dirname/             [ creates gzipped tar archive file ]
      $ tar -cvjf target.tar.bz2 dirname/           [ creates bzipped tar archive file ]

    Note: this -cvf options alone does not provide any compressions

2 Extracting an archive
 
      $ tar -xvf target.tar
      $ tar -xvzf target.tar.gz                            [ extracts gzipped tar archive file ]
      $ tar -xvjf target.tar.bz2                          [ extracts bzipped tar archive file ]

3. Listing an archive

      $ tar -tvf target.tar
      $ tar -tvzf target.tar.gz                             [ lists gzipped tar archive file ]
      $ tar -tvjf target.tar.bz2                           [ lists bzipped tar archive file ]

4. Extract a file/directory

    $ tar -xvf target.tar /mydir/myfile
    $ tar -xvzf target.tar.gz /mydir/myfile
    $ tar -xvjf target.tar.bz2 /mydir/myfile 

    if you want to extract a group of files from the archive use -wildcard option
    $ tar -xvf target.tar --wildcards '*.cc'
 
5. Adding a file or directory to an existing archive
 
    You can add additional files to an existing tar archive by specifying -u or -r options.
    use -u option if u do not want duplicates in the tar archived file. -r option will add the file to the existed archive if it exist also.

    $ tar -rvf target.tar newfile     or $ tar -uvf target.tar newfile

    Note: this can't be done for gz or bz2 archives.
 





'cat' can be harmful..

June 04, 2012 Posted by Dinesh , , No comments
'cat' can be harmful at times. some people might have lost the data because of improper usage of cat command like cat > filename.txt, or it can do over write some existing files data without prompting the user.
to get rid out of these situations
1. open the terminal
2. execute " set -o noclobber "
that's it. now next time when cat tries to over write the existing file it will prompt the user.