Tuesday, March 27, 2012

Basic Unix Commands - Part2

Basic Unix Commands:


Editors:


1. vi editor
2. emacs
3. pico
4.joe
and some other. But minimal installation of unix has the vi editor.


VI EDITOR:


vi editor has two modes:


      1. edit or insert mode
      2. command mode


Default mode is command mode.


To insert press  a,A,i,I,o,O


To comeback to command mode:
  
   Press Esc Key


To create or edit a file using vi:


vi filename


vi file1


To save the file and reside in the same vi then use --- Esc key   :w   (write)


To save and quit  ----- :wq


To quit without saving  ---- :q!


To delete a line --- use dd  --- Everything from the cursor position & command mode.
Delete 3 lines  ---- use 3dd 


Copy a line  ---use yy
copy 5 lines  --- use 5yy   


To paste the line  --- use p
to paste 3 times  --- use 3p


To search a word  ---- forward search use /pattern for ex /new 
                                   reverse search ?pattern


To search and replace:


:s/senthil/kumar/  --- This command will change the senthil to kumar in the first occurrence.  
:%s/senthil/kumar --- This command will change the senthil to kumar in first occurrence in whole file.
:1,$s/senthil/kumar --- same
:1,$s/senthil/kumar/g   ---- Whole file senthil word changed to kumar.
:1,15s/senthil/kumar/g  --- from 1st to 15th line changed
:1,15s/senthil/kumar/gi   ---- g for global  and i for ignore case 


To execute the unix commands in the vi:
:sh date
:sh cal


To print the command output in the file:


:.!date
:.!ls -l
:.!who


To copy a file contents to another file:
:w new filename   ---- whole file
:1,10 w newfilename   --- from 1 to 10 lines are copied in newfilename


To read a contents from another file:


:r oldfilename


vi options :


-r   --- recovery
-R  -- readonly


Vi has some set commands:


:set nu  --- setting line numbers
:set nonu  --- unsetting the line numbers
:set ic  --- setting ignore case
:set ab sk senthilkumar  ---- setting abbreviation


$HOME/.exrc file invoked by the vi.


From linux :


vimtutor  ----- It is the utility to study and practice the vi editor.


All the best .................


Senthilkumar Muthusamy


9865831007









2 comments: