To Study VI Editor in Detail | Operating System Lab | Linux/ Unix | VI Editor - IndianTechnoEra
Latest update Android YouTube

To Study VI Editor in Detail | Operating System Lab | Linux/ Unix | VI Editor

To study vi-editor in detail | Operating System Lab | Linux/ Unix | VI Editor , Operating System Lab, Linux, Unix, VI Editor
Admin
Vi-Editor


UNIX: vi Editor


General Introduction:


The vi editor (short for visual editor) is a screen editor which is available on almost all Unix systems.

Once you have learned vi, you will find that it is a fast and powerful editor.

vi has no menus but instead uses combinations of keystrokes in order to accomplish commands.



Starting vi:


To start using vi, at the Unix prompt type vi followed by a file name.

If you wish to edit an existing file, type in its name; if you are creating a new file, type in the name you wish to give to the new file.


%vi filename


vi's Modes and Moods


vi has two modes: the command mode and the insert mode. It is essential that you know which mode you are in at any given point in time.

When you are in command mode, the letters of the keyboard will be interpreted as commands.

When you are in insert mode the same letters of the keyboard will type or edit text.. vi



    General Command Information:


    As mentioned previously, vi uses letters as commands. It is important to note that in general vi commands: 

    • are case sensitive - lowercase and uppercase command letters do different things
    • are not displayed on the screen when you type them 

    • generally do not require a Return after you type the command.


    You will see some commands which start with a colon (:). These commands ex commands which are used by the ex-editor. ex is the true editor which lies underneath vi -- in other words, vi is the interface for the ex-editor.


    Moving One Character at a Time:


    Try using your direction keys to move up, down, left and right in your file. Sometimes, you may find that the direction keys don't work. If that is the case, to move the cursor one character at the time, you may use the h, j, k, and l keys. These keys move you in the following directions:


    h left one space

    l right one space

    j down one space

    k up one space



    If you move the cursor as far as you can in any direction, you may see a screen flash or hear a beep.


    Moving among Words and Lines:


    While these four keys (or your direction keys) can move you just about anywhere you want to go in your file, there are some shortcut keys that you can use to move a little more quickly through a document. To move more quickly among words, you might use the following:


    • moves the cursor forward one word

    • moves the cursor backward one word (if in the middle of a word, b will move you to the beginning of the current word).

    • moves to the end of a word.

    Command Keys and Case:


    You will find when using vi that lower case and upper case command keys are interpreted differently. For example, when using the lower case w, b, and e commands, words will be defined by a space or a punctuation mark. On the other hand, W, B, and E commands may be used to move between words also, but these commands ignore punctuation.


    Shortcuts:



    Two short cuts for moving quickly on a line include the $ and the 0 (zero) keys. The $ key will move you to the end of a line, while the 0 will move you quickly to the beginning of a line.


    Screen Movement:


    To move the cursor to a line within your current screen use the following keys:


    To moves the cursor to the top line of the screen.

    M moves the cursor to the middle line of the screen.

    L moves the cursor to the last line of the screen.


    To scroll through the file and see other screens use:

    ctrl-f scrolls down one screen

    ctrl-b scrolls up one screen

    ctrl-u scrolls up a half a screen

    ctrl-d scrolls down a half a screen


    Two other useful commands for moving quickly from one end to the other of a document are G to move to the end of the file and 1G to move to the beginning of the file. If you precede G with a number, you can move to a specific line in the document (e.g. 15G would move you to line 15).


    Deleting (or Cutting) Characters, Words, and Lines:


    To delete a character, first place your cursor on that character. Then, you may use any of the following commands deletes the character under the cursor deletes the character to the left of your cursor. dw deletes from the character selected to the end of the word.

    deletes all the current line. deletes from the current character to the end of the line.


    Preceding the command with a number will delete multiple characters.

    For example, 10x will delete the character selected and the next 9 characters; 10X will delete the 10 characters to the left of the currently selected character. The command 5dw will delete 5 words, while 4dd deletes four lines.


    Replacing or Changing Characters, Words, and Lines:


    When you are using the following commands to replace text, you will be put temporarily into insert mode so that you can change a character, word, line, or paragraph of text. replaces the current character with the next character you enter/type. Once you enter the character you are returned to command mode. puts you in overtype mode until you hit ESC which will then return you to command mode. cw changes and replaces the current word with text that you type. A dollar sign marks the end of the text you're changing. Pressing ESC when you finish will return you to command mode.


    Inserting Text:


    If you wish to insert new text in a line, first position the cursor to the right of where you wish the inserted text to appear. Type i to get into insert mode and then type in the desired text (note that the text is inserted before the cursor). Press ESC to return to command mode.


    Inserting a Blank Line:


    To insert a blank line below the line your cursor is currently located on, use the o key and then hit ESC to return to the command mode . Use O to insert a line above the line the cursor is located on.


    Appending Text:


    You can use the append command to add text at any place in your file. Append (a) works very much like Insert (i) except that it insert text after the cursor rather than before it. Append is probably used most often for adding text to the end of a line. Simply place your cursor where you wish to append text and press a. Once you've finished appending, press ESC to go back to command mode.


    Joining Lines:


    Since vi does not use automatic word wrap, it is not unusual in editing lines to end up with lines that are too short and that might be improved if joined together. To do this, place your cursor on the first line to be joined and type J. As with other commands, you can precede J with a number to join multiple lines (4J joins 4 lines).


    Undoing:


    Be sure to remember this command. When you make a mistake you can undo it. DO NOT move the cursor from the line where you made the change. Then try using one of the following two commands: undoes the last change you made anywhere in the file. Using u again will "undo the undo".

    undoes all recent changes to the current line. You can not have moved from the line to recover the original line.


      Closing and Saving Files


      When you edit a file in vi, you are actually editing a copy of the file rather than the original. The following sections describe methods you might use when closing a file, quitting vi, or both.


      Quitting and Saving a File:


      The command ZZ (notice that it is in uppercase) will allow you to quit vi and save the edits made to a file. You will then return to a Unix prompt. Note that you can also use the following commands:

      :w to save your file but not quit vi (this is good to do periodically in case of machine of machine crash!). :q to quit if you haven't made any edits. :wq to quit and save edits (basically the same as ZZ).


      Quitting without Saving Edits:


      Sometimes, when you create a mess (when you first start using vi this is easy to do!) you may wish to erase all edits made to the file and either start over or quit. To do this, you can choose from the following two commands:

      :e! reads the original file back in so that you can start over.

      :q! wipes out all edits and allows you to exit from vi.



      More about Combining Commands, Objects, and Numbers


      Now that you've learned some basic vi commands you might wish to expand your skills by trying some fancy combination steps. Some commands are generally used in combination with a text object. We've already seen some examples of this. For example, when you use the command dw to delete a word, that combines the delete (d) command with the word (w) text object. When you wish to delete multiple words, you might add a number to this combination. If you wished to delete 2 words you might use 2dw or d2w. Either of these combinations would work. So, as you can see, the general format for a command can be


      (number) (command) (text object) or (command) (number) (text object)


      You might wish to try out some of the following combinations of commands and objects:


      Command

      d (delete)


      y (yank/copy)


      c (change)

      Text Object

      w (word to the left)


      b (word to the right or backward)


      e (end of word)


      H (top of the screen)

      L (bottom of the screen)

      M (middle of the screen)


      0 (zero - first character on a line)


      $ (end of a line)

      ( (previous sentence)


      ) (next sentence)

      [ (previous section)

      ] (next section)


      Repeating a Command:


      If you are doing repetitive editing, you may wish to use the same command over and over. vi will allow you to use the dot (.) to repeat the last basic command you issued. If for example, you wished to deleted several lines, you could use dd and then . (dot) in quick succession to delete a few lines.


      Useful vi Commands




      Cut/Paste Commands:



      x

      dw

      dd

      D

      d$

      :u


      p,P

      J


      "[a-z]nyy


      "[a-z]p/P


      delete one character (destructive backspace)


      delete the current word (Note: ndw deletes n numbered words)


      delete the current line (Note: ndd deletes n numbered lines)


      delete all content to the right of the cursor same as above undo last command


      paste line starting one line below/above current cursor location


      combine the contents of two lines yank next n lines into named buffer [a-z]


      place the contents of selected buffer below/above the current line


      Extensions to the Above Commands:






      :3,18d 

      16,25m30 

      23,29co62


      delete lines 3 through 18

      move lines 16 through 25 to after line 30

      copy specified lines and place after line 62




      Cursor Relocation commands:


      :[n]

      shift g

      h/l/j/k

      ^f/^b

      ^u/^d

      $

      0


      goto line [n]

      place cursor on last line of text

      move cursor left, right, down and up

      move forward, backward in text, one page

      move up, down one half page

      move to end of line

      move to beginning of line


      Extensions to the Above:


      b    move backwards one word (Note: nb moves back n number of words

      e    move to end of current word

      (    move to beginning of curent block

      )    move to the end of current block


      Searching and Substitution commands:


      / [string]

      ? [string]

      n

      N

      cw

      c$

      c0

      :1,$s/s1/s2/g

      r


      search forward for string

      search backwards for string

      repeat last search

      repeat search in opposite direction

      change the contents of the current word, (use ESC to stop replacement mode)

      Replace all content to the right of cursor (exit replacement mode with ESC)

      Replace all content to the left of cursor (exit with ESC) (Yow!) global replacement of string1 with string2 replace current character with next character typed



      Entering the Insert Mode:

      i

      I

      a

      A

      o/O

      ESC


      Begin inserting text at current cursor location

      Begin inserting text at the beginning of the current line

      Begin appending text, one character to the right of current cursor location

      Begin appending text at the end of the current line

      Begin entering text one line below\above current line

      Exit insertion mode and return to command mode


      Exiting and Entering VI:


      ZZ

      :wq

      :e!

      :q

      :w


      save file and exit VI

      same as above

      return to last saved version of current file

      quit without save, (Note :q! is required if changes have been made)

      write without exit (:w! to force write


      Implementation:




      This site has been visited times.


      إرسال تعليق

      Feel free to ask your query...
      Cookie Consent
      We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
      Oops!
      It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
      AdBlock Detected!
      We have detected that you are using adblocking plugin in your browser.
      The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
      Site is Blocked
      Sorry! This site is not available in your country.