Unix - Viewing and Editing Files
TechNotes      |     Unix Home
Editing Commands (VI):
 To vi a file
Open the file
  1. > vi filename
 To close the file
After you've entered the file with vi, you have options for closing
To close without saving
  1. > :q
 
To close and save
  1. > :wq
 
To force any command use !
To close and force the save
  1. > :wq!
To end any VI command
  • press the ESC button
 To insert text
  • place the cursor where you want to insert the text to the left
    > type i
  • to append text after the cursor
    > type a
  • to open a line below the cursor (insert a new line)
    > type o
  • to open a line before the cursor
    > type O
 To undo
  • the last change to the file.
    > type u
 To Delete
  • To delete a character, place cursor before the desired text.
    > type x
    - continue typing x to delete as many characters as desired
    - or -
    - to delete from the cursor to the end of the line
    > type D
  • To delete an entire line. Place cursor on that line
    > type dd
  • To delete multiple lines. Place cursor on the 1st line to be deleted
    > type #dd
    - where # is the number of lines to be deleted
  • To delete a word. Place cursor on the word
    > type dw
 To replace text
  • To enter replace mode, place cursor at begining of text to be replaced
    > type R
  • To replace a character, place cursor before the character
    > type rx
    - where x is the character being added
  • To replace a word, place cursor before the character
    > type cw
    -
  • To replace the CASE of a letter
    > type -
To fix a broken line  Sometimes a line that should be a continous line gets broken. The easiest way to fix this is to put the cursor at the break and press shift + j. Then remove any extra space that may have been created.
 
Global VI Commands
 Search
  • To search the entire file
    > /text
    - where text is what you want to find. ie: /pwpunx
    - This can be a single character, or a word
  • To repeat the last search
    > type n
    - n = next
 Find and Replace
  • To find and replace one string at a time
    > :g/string1/s//string2
 
Viewing Files:
 To view a file without edit permissions
This is just looking at the file, it will display the entire file on the screen
  1. > more filename
 To view the tail end of a file
To view the last 100 lines of a file
  1. > tail -100 filename