- i inser mode
- a append mode
- x delete character
- :d delete line
- yy copy line
- p or P paste lines
- /pattern find pattern
- :wq write and quit
- :q! quit
Join facebook group THE HACKER DEVIL
GO VI:FOR NERDS AND NOVICES
So in this post we are going to cover using VI EDITOR. Ok, please don't misundertook this article is for geeks only. In fact i am going to show you here, it doesn't need a geek to take advantage of editing capability of VI editor. It is said that VI editor has several commands even i don't have a doubt about that but i have used more than 20 different types of Linux distros each having its own VI editor and came to the conclusion that it's not neccessary to remember all commands of VI editor to take control over it, in fact most of the commands which we say will perform an operation on text doesn't work at all on every VI editor of different distros, might sound strange but believe me it's true. Here i'll tell you which commands actually work equally on all flavors and which of their counterparts doesn't, please don't take your time to learn those counterparts since they will only waste your time and you will irritate yourself like me if found any flavor not supporting that command. Please note that i'll be keeping stuff so simple that even a kid can grasp of, so even if you are not interested in learning VI editor in easy way please spend a little time, it might happen you may thank me someday for this tutorial because this tutorial is 100% for novice and nerds to VI.
First of all “WHAT THE HELL IS VI ? ”
Ans: VI is full text based editor for UNIX and Linux environment. Though VI may appear you junky but in fact it is one of the most robust text editors available which only takes input via console and hence it works in different modes (example:insert mode, append mode, buffer mode etc.). So skipping unneccessary details lets move on to the real stuff.
Start vi by typing “vi filename” on terminal
if you forgot to mention filename it'll then ask you for confirmation just press enter and go ahead
Once you are inside vi editor you have to make commands work in modes, please note that you have to press “escape” each time to switch modes. For convinience we will term mode after esc as “shell mode”.
So first mode is insert or append mode:
To insert character before cursor insert mode is used, for switching to insert mode press “esc” then ' i '. Then type your data and to switch again press “esc”.
To insert character after cursor append mode is used, for switching to append mode press “esc” then ' a '. Then type your data and to switch again press “esc”.
Deleting characters, single line or multiple lines:
Press esc to enter shell mode if you make any mistake then move your cursor to character you want to delete then press ' x ' as many times you want to delete characters.
Now if you want to delete lines just remember this command “ :d ”, this command can be used to delete single line as well as multiple lines.
To delete single line use this command as “ :d ” or “ :0d ” similar operation can also be performed by “ dd ” command, choice is yours.
To delete multiple lines use this command as “ :3,7d ”, this will delete all 7 lines holding line 3 as offset.
Now you'll ask what if i want to delete till end of file from offset. Most will answer just use this command, “ :dG ”, but this is crap doesn't really work on all, similarly “ :d0 ” doesn't delete till origin. Better select an offset and you can use an approximate value such that it must be greater than total lines in file, in such case editor will discard the lines which doesn't really exist and clear all text till end of file.
In fact when we use “ :d ” command, lines are not deleted they get cut and are stored in buffer until you paste them. If you don't paste them then they get discarded. To swicth to shell mode press “ esc ” then use following commands to paste,
“ p ” paste below current postion of cursor
“ P ” paster above current position of cursor
Above commands can also be used to paste copied buffer.
To copy a line go to the end of line then press “ yy ”, that will copy your line. Copying multiple lines use “ [number of lines]yy ” command for example “ 3yy ” that will copy 3 lines from current position of cursor(Note:Copying multiple commands doesn't really work equally on all).
“ /pattern ” will find pattern in forward direction
“ ?pattern ” will find pattern in backward direction
To go to next occurance use ' n ' and to go to previous occurance use ' N '.
Note:Once you enter pattern mode you don't need to press “ esc ” if you want to serach next occurance.
Saving and exiting:
While saving please make sure you are on last line and and the line you are on, is blank. Both condition must be satisfied i am asking you to do so becuase sometimes when you leave cursor somewhere between text before quitting, all lines after that line are discarded while saving file. Don't think it's a joke, it's a bug in coding of some platforms.
“ :wq ” where w means write and q means quit
“ :q! ” quit without saving
“ ctrl+U ” move one page up
“ ctrl+D ” move one page down
“ u ” undo
“ * ” redo
These commands may not work everywhere and don't even have alternatives.
So what you are supposed to keep in mind:
Above commands are 100% asured to work equally on each platform may be Linux or UNIX. For one who are visual learner can also download a swf file where i demonstrated commonly used vi commands.