vi editor help

Find and replace (substitutions)

  • This is normally done with ex editor command. Following is the syntax.
  • Replacing everywhere in the file.
  • :1,$s/pattern/replacement/g
Explanation
  • : indicates the start of an ex editor command
  • 1,$ - from the first line to the last line. Alternatively, you can use % (for all lines of a file)
  • / used to separate pattern and replacement. Here, you can use regular expressions.
  • g stands for all occurences of the pattern on a line

How do I see line numbers ?

  • :set nu
  • :set nonu
    does the opposite.

Indentation for formatting your programs/scripts

  • :set autoindent

Saving vi/vim preferences

You can create a file (a hidden file) by name .exrc and write all your ex commands one per line. You can copy-paste the following two lines as .exrc. These options/preferences will work next time you open vi.

  • set autoindent
  • set nu

Moving around quickly

  • Last line - shift+g
  • First line - gg
  • One screen forward - ctrl+f
  • One screen backward - ctrl+b
  • Half screen forward (down) - ctrl+d
  • Half screen backward (up) - ctrl+u
  • 50th line - 50,shift+g
  • Move forward a word - w
  • Move back a word - b
  • To start of line - 0
  • To end of line - $
  • Down a line - j
  • Up a line - k
  • One char left - h
  • One char right - l

Deleting content

  • Delete a character - x
  • Delete a word - dw
  • Delete a line - dd
You can multiply these commands with numbers ! e.g. '5dw' deletes 5 words.

Chaging content

  • Change word - cw
  • Change line - cc

Inserting content

You can go into insert mode by following ways
  • Insert where cursor is - i
  • From start of line - I (shift+i)
  • Next line - o
  • Previous line - O (shift+o)
  • Next character - a
  • End of line - A (shift+a)

Copying and pasting

The command to copy content is 'y' (yank).
  • Copy a line - yy
  • Copy a word - yw

Copy 5 lines - 5yy as explained above. Multiplication applies here as well.
Paste - p
Paste before the cursor position/previous line - P (shift+p)

Saving and reading files

  • :r filename - read file named filename and insert after current line (the line with cursor)
  • :w - write current contents to file named in original vi call
  • :w newfile - write current contents to a new file named newfile
  • :12,35w smallfile - write the contents of the lines numbered 12 through 35 to a new file named smallfile
  • :w! prevfile - write current contents over a pre-existing file named prevfile

Executing os commands while editing in vi

You can read the results of any linux command while editing in vi. An exclamation mark (!) informs ex to create a shell and to regard what follows as a linux command.

  • If you are editing and want to check the time and date without exiting vi, you can enter
    :!date
    Time and date will appear on the screen, then press RETURN to continue editing at the same placein file.
  • If you want to execute several commands without returning to vi editing in between, you can create a shell with the ex command
    :!sh
    When you want to exit the shell and return to vi press CTRL-D
  • You can combine linux command with :read, to read the result of command in your file. By default, it will appear after the current line.
    :r !date - will give date and time after a current line.
    By preceding the :r command with a line address, you can read the result of the command at any point in your file. :10r !date - will give result of date command after 10th line in your file.

Back to Linux Tips

More Info

Don't forget to see our Tools' page and linux resources page
For Linux commands see - Useful Linux Tips

gnuServices interests

gnuServices tools...

Report Generation

Open Source

Report Generation is an integral part of enterprise application. We write Shell scripts for automation and Perl scripts for Report Generation of various kinds. More…

Knowledge Base

Website Applications

With Perl, PHP - We use CSS layouts which is the latest trends in web-designing. This has many advantages over the old-school web-design practices. If you would like to learn more about what it is, please visit www.zeldman.com. More…