Using a Text Editor

Text Editors in Unix

Being able to use a text editor is probably one of the most critical skills to have as a system administrator. You constantly need to edit con fig files, write scripts or make changes to system files …….. all of which require you to use a text editor.

  • The three most popular editors available today include

Vi(or) Vim:      Text editor with great flexibility.

Emacs  :           similar to vi, an advanced text editor with many features.

Nano   :           A basic text editor for quick editing

  • Vi (or) Vim Editor:
  • Using this editor to create new files, open the files and modifying the data into a existing files.
  • The Vi Editor is most popular
  • It has three modes:

    • Command mode
    • Insert mode
    • Execution (or) colon mode

  • By default mode is command mode

Syn:  Vim [arguments]   [file]

Arguments:                 -           R    -----> opens a file in read –only mode

-           o    -----> Open two files at a time

+      -----> Starts at the end of the file

+          <rum>    -----> Start at line <num>

 

1

 

Insert mode options:

i --------> To begin insert mode at the current cursor position.

I -------->To Insert at the beginning of the current line

a-------->To append to the next word’s letter.

--------> To append at the end of the line

o  -------->To insert a new line below the cursor position.

O  -------->  To Insert a new line above the cursor position.

 

Commands for command mode:

e  --------> Moves to the end of a word

b  --------> Moves to the beginning of a word.

$  -------->Moves to the end of a line.

˄   -------->Moves to the beginning of a line

H -------->Moves to the first line onscreen

M --------> Moves to the middle line onscreen

L  -------->Moves to the last line onscreen

x (nx) -------->Deletes current character.

dd (ndd)-------->Deletes the current line

dw (ndw)-------->Deletes current word.

Yy(nyy)  --------> yanks (copies) the current line

p -------->paste below the cursor line

P  -------->paste above the cursor line

U-------->undo the last action.

gg(ngg)-------->go to beginning of the file

G -------->End of the file.

W (n)-------->To move the cursor forward, word by word.

b(n) --------> To move the cursor back word, word by word

Ctrl +f -------->To forward one page.

Ctrl +b -------->To back word one page.

/  -------->To search a word in the file.

n -------->Find next occurrence of search word.

N --------> find previous occurrence of search word.

.  --------> Repeat last command action.

Commands for last line mode:

:q  -------->To quit without saving

:w   -------->To save the changes

 

:wq    -------->To save & quit

 

:wq!  (or)   :x  -------->  To save & quit with forcefully.

 

:set nu  (or)    :se nu -------> To setting line numbers.

 

 

:set nonu   (or)   :se nonu------> To remove line numbers.

 

:n  ------>Jumps to line n

:$d ------>To delete last line

:! <unixcmd> ------> To execute unix cmds

:x  ------>To give password to the file and remove password.

:/storing / ------>search a word in the file.

Desired to gain proficiency on Linux ? 

Explore the blog post on Linux training online to become a pro in Linux.

  • To find & Replace:

: % S/root/dog/             To replace storing “dog” for the first instance

: %s/root/dog/g              For each instance of a line.

: %s/root/dog/gi             To ignore case sensitive

: %s/root/dog/gc             ask for confirmation

 

 

  • Executing unix commands in vi:

Any unix command can be Executed from the vi command line by typing an “!” before the unix command.

EX:

: ! Pwd

: r ! data           Reads the results from the date command into a new line following the cursor

: r ! cat file1

  • I want to copy 1,4 lines to paste after 10th line:

:  1,4 CO 10

  • I want to move 3,7 lines after 8th line:

: 3,7 mo 8

  • I want to copy 1,30 lines create a new file:

: 1,30 w test1

  • I want to append the data into a existing file:

:  8,20 w >> test1

  • I want to insert end of the line (or) we require line

: r/etc/passwd

 

Managing two files at time:

$vim    -0 file1 file2

(or)

$vim file1 file2

Options:

:n    ----> edit next file (file2)

:rew   ----> Rewind to the file (file1)

(or)

  • To move one file to another file (ctrl + W)

Press two times

For indepth understanding of Linux click on