Using Vi, The Unix Visual Editor - University Of Washington

  • About vi
  • vi is Found on Nearly Every Unix Computer

    • vi is the standard Unix editor

    • Other documents on vi:
      • How To Use The vi Editor
      • Vi Reference
      • Mastering the VI editor - University of Hawaii at Manoa
      • Vi Lovers Home Page
      • Visual Editor (vi): A Tutorial - University of Southwestern Louisiana

    vi is Powerful and Fast

    • Your terminal displays a section of the file you are editing

    • vi can do anything you want

    • You don't need to remove your fingers from the standard typing keys-the keys themselves give commands to vi

    vi Stays Out of Your Way

    • vi has no menus

    • vi commands are short

  • Starting vi
  • Open a file with vi. Type: vi myfile.txt

    • If myfile.txt does not exist, a screen will appear with just a cursor at the top followed by tildes (~) in the first column.

    • If myfile.txt does exist, the first few line of the file will appear.

    • The status line at the bottom of your screen shows error messages and provides information and feedback, including the name of the file.

  • vi Modes
  • Command Mode

    • Command mode is the mode you are in when you start (default mode)

    • Command mode is the mode in which commands are given to move around in the file, to make changes, and to leave the file

    • Commands are case sensitive: j not the same as J

    • Most commands do not appear on the screen as you type them. Some commands will appear on the last line: : / ?

    Insert (or Text) Mode

    • The mode in which text is created. (You must press <Return> at the end of each line unless you've set wrap margin.)

    • There is more than one way to get into insert mode but only one way to leave: return to command mode by pressing <Esc>

    When in doubt about which mode you are in, press <Esc>

  • Basic Cursor Movement
  • From Command Mode k Up one line j Down one line h Left one character l Right one character (or use <Spacebar>) w Right one word b Left one word

    NOTE: Many vi commands can take a leading count (e. g., 6k, 7e).

  • Entering, Deleting, and Changing Text
  • From Command Mode i Enter text entry mode x Delete a character dd Delete a line r Replace a character R Overwrite text, press <Esc> to end

  • Setting Basic Options in vi
  • Displaying Line Numbers

    From Command Mode :set nu Display line numbers :set nonu Hide line numbers

    Setting Right Margin

    From Command Mode :set wm=number Set Wrap Margin number of spaces from right edge of screen :set wm=10 Set Wrap Margin 10 spaces from right edge of screen :set wm=0 Turn off Wrap Margin

  • Exiting vi
    • To exit you must be in command mode-press <Esc> if you are not in command mode

    • You must press <Return> after commands that begin with a : (colon)

    From Command Mode ZZ Write (if there were changes), then quit :wq Write, then quit :q Quit (will only work if file has not been changed) :q! Quit without saving changes to file

  • Basics Summary
  • UNIX ---> vi file ---> COMMAND ---> i I a A o O ---> TEXT SHELL <---- ZZ <------- MODE <------ <Esc> <------ MODE

    A Basic vi Session

    1. To enter vi, type: vi filename <Return>

    2. To enter insert mode, type: i

    3. Type in the text: This is easy.

    4. To leave insert mode and return to command mode, press: <Esc>

    5. In command mode, save changes and exit vi by typing: :wq <Return>

      You are back at the Unix prompt.

    Từ khóa » Vi In Linux Meaning