[Linux] Vi editor basics
June 21, 2011 Leave a Comment
In everyday in our Linux life we may need to use vi editor. So do I. The main cause of this post is to know how simple vi editor is! Installing Vi editor sudo apt-get install vim *it will also load color of you vi edior Opening a file vi filename Creating text Edit modes: These keys enter editing modes and type in the text of your document. i Insert before current cursor position I Insert at beginning of current line a Insert (append) after current cursor position A Append to end of line r Replace 1 character R Replace mode <ESC> Terminate insertion or overwrite mode Going to VISUAL mode: If you want visual then from command mode just hit "v" v it will allow to switch in visual mode Deletion of text x Delete single character dd Delete current line and put in buffer ndd Delete n lines (n is a number) and put them in buffer J Attaches the next line to the end of the current line (deletes carriage return). Undo: u Undo last command cut and paste yy Yank current line into buffer nyy Yank n lines into buffer p Put the contents of the buffer after the current line P Put the contents of the buffer before the current line cursor positioning ^d Page down ^u Page up :n Position cursor at line n :$ Position cursor at end of file ^g Display current line number h,j,k,l Left,Down,Up, and Right respectivly. Your arrow keys should also work if your keyboard mappings are anywhere near sane. string substitution :n1,n2:s/string1/string2/[g] Substitute string2 for string1 on lines n1 to n2. If g is included (meaning global), all instances of string1 on each line are substituted. If g is not included, only the first instance per matching line is substituted. ^ matches start of line . matches any single character $ matches end of line These and other "special characters" (like the forward slash) can be "escaped" with \ i.e to match the string "/usr/STRIM100/SOFT" say "\/usr\/STRIM100\/SOFT" Examples: :1,$:s/dog/cat/g Substitute 'cat' for 'dog', every instance for the entire file - lines 1 to $ (end of file) :23,25:/frog/bird/ Substitute 'bird' for 'frog' on lines 23 through 25. Only the first instance on each line is substituted. Saving and quitting and other "ex" commands These commands are all prefixed by pressing colon (:) and then entered in the lower left corner of the window. They are called "ex" commands because they are commands of the ex text editor - the precursor line editor to the screen editor vi. You cannot enter an "ex" command when you are in an edit mode (typing text onto the screen) Press <ESC> to exit from an editing mode. :w Write the current file. :w new.file Write the file to the name 'new.file'. :w! existing.file Overwrite an existing file with the file currently being edited. :wq Write the file and quit. :q Quit. :q! Quit with no changes. :e filename Open the file 'filename' for editing. :set number Turns on line numbering :set nonumber Turns off line numbering i think it will help someone!![]()
Advertisement


