Friday, 9 November 2012

LINUX EDITOR


LINUX EDITOR


Linux-based System supports C programming. We have to deal with files whether it is creating a new file, altering or editing the existing file. So, we need an Editor to create or edit the files. All the programs in the Linux are written in C language using these editors.

“EDITORS are the platform to Create or Edit the files.”



TYPES OF LINUX EDITORS:

There are basically two types of Editors used in the Linux-based System. These are as given below:
1.   TEXTUAL EDITOR: vi/ vim editor, emacs, pico, etc.
2. GRAPHICAL EDITOR: gVim, Gedit, xedit, Vedit, emacs/ xemacs, jot (SGI), dtpad(CDE), etc.

TEXTUAL EDITOR generally works on three modes:
1.  Command Mode.
2. Text Mode.
3. Command Line Mode.

GRAPHICAL EDITOR provide generally user friendly interface to create or edit the files. These editors offer many Menu and toolbar items to facilitate user to edit files rapidly. It also allow user to move cursor via mouse accordingly.



VI/ VIM EDITOR:

The most commonly used editor in Linux is ‘vi editor’. It is also known as “Visual Editor”. The text can be written into the file in Text Mode whereas all other operation such as save, copy, paste, delete, insert, quit, etc. can be performed in the Command mode. We typically press ESC button in order to work in the command mode.   

The main difference between the vi editor and vim editor is that- the Arrow keys cannot be used for the cursor movement in the vi editor. It usually append ‘A’, ‘B’, ‘C’ or ‘D’ with respect to each stroke of UP, DOWN, RIGHT or LEFT Arrow key in the next line to the cursor. Whereas this limitation or drawback of vi editor is seems to be eliminated in the vim editor, that means arrow keys can be used for cursor movement.



INSERTING TEXT IN VI/ VIM EDITOR:

To insert text into the vi/ vim editor some keys are predefined to be used before editing the file. One of the key has to e pressed:

“a”    :     append text after the cursor position.
“I”     :     insert text before the cursor position.
“r”    :     replace the text at the cursor position.
“A”    :     append text from the end of the line.
“I”     :     insert text from the beginning of the line.
“o”    :     open a new line after current line.
“O”    :     open a new line before current line.



ALTERING OR DELETING THE TEXT:

“D”    :     deletes the text from the cursor position to the end of current line.
“dd”   :     deletes the current line.
“x”    :     deletes a single character at the cursor point.
“dw”  :     deletes a single word after the cursor.
“v”    :     select the multiple lines from the cursor point.
“yy”   :     copy the text in current line.
“y”    :     copy the selected text.
“p”    :     paste the text in the next line to the cursor position.



FILE SAVE OR UPDATE:

“:w”   :     to save the file.
“:q”   :     to quit the file if no change is occured.
“:!”    :     do the task force- fully.
“:w!”  :     save the file forcibly.
“:q!”  :     quit the file forcibly without saving changes.
“:wq”  :     save and quit the file.



CREATING OR OPENING A FILE:

To create a new text file by using vi editor. For example- to create a new text file at location “/home/keertendra/Desktop/”   
$ vi /home/Keertendra/Desktop/file_name

Or if we want to create this file in the present working directory then use:
$ vi file_name

To open an existing file, then same command can be used. For example- to open multiple files at a time then we have,
     $ vi –o file1_path file2_path …





A SAMPLE C PROGRAM USING VI EDITOR:

Now let us see how to write a simple C program using vi editor and then see will see some commands to compile and run this sample program….

Step1: firstly, create a new file “add.c on the desktop using vi editor, say
           $ vi /home/Keertendra/Desktop/add.c

Step2: now bring the vi editor in the text mode by pressing “a” to append the text into the file. Then, write the program as shown below:
          
//this is a sample program to add two numbers and to print the result…
       #include<stdio.h>
       Void main()
       {
           Int A=0,B=0,C=0;
           Printf(“\nEnter the two numbers A and B for addition=”);
           Scanf(“%d%d”,&A,&B);
           C=A+B;
           Printf(“\nThe result of the addition of two numbers A and B is given by C=%d\n”,C);
       }

Step 3: after finishing writing the program just change the mode to command mode by pressing ESC button and type
           :wq (to save and quit the file)

Step 4: Now compile the file by using a command shown below
           $ gcc –o exefile_name file_name.c (here, $ gcc –o add add.c)

Step 5: If there is any error then debug the error and repeat the above two step and if not then, run the program by using a command,
           $ ./exefile_name    (here, $ ./add)




About the Author

keertendra

Author & Editor

Hobbyst Researcher and working in electronics domain, M.Tech in VLSI Design and Embedded Systems.

Post a Comment

 
2embeddedrobotics © 2015 - Designed by Templateism.com