Vim text editor

From RedBrick Wiki
Jump to: navigation, search

What is Vim

Vim is a text editor. Vim was created as an extended version of the vi editor, with many additional features designed to be helpful in editing program source code, its full name is Vi IMproved. While Vim is cross-platform, it is most popular on Unix-like operating systems.

Interface

Vim's interface is based not on menus or icons but on commands given in a text user interface. For many users, Vim may present a steep learning curve, meaning that learning is slow initially but once the user gets a grasp of the basics they progress quickly and their editing becomes more efficient.

The Basics

to start vim and edit a file (new or existing)

   vim file_name

to start vim and open a new file ( You can give the stuff you typed in a name when later you save it as a file.)

   vim

to exit vim and save your document first press ESC then

   :wq

to exit, saving as a new filename again press 'ESC then

   :wq <filename>

to exit without saving (force quit) press ESC then

   :q!

===Editing=== When in vim you will notice that you cannot enter any text, this is because you are in the command mode of the editor, to enter the insert mode press 'i' or 'insert'. To return to the command mode hit 'ESC'

Deleting

OK, the 'delete' key always works, as does the 'backspace'. However, vim has another deletion method through the command mode that doesn't require entering the insert mode.

1. move the cursor to the character you want to delete

2. press x , this character disappears

Searching for Words

Suppose you want to find all the words apple in your file 1. Make sure you are in command mode

   by hitting ESC

2. type

   /apple

followed by ENTER to find an occurrence of apple. When you type the slash, it and the following characters will be shown on the bottom of the screen. After you press ENTER, the cursor will go to the first occurrence of apple if found, and the target will be highlighted.

3. after you got the first apple, you can keep typing

   n

to find other apples

Substituting words

First make sure you're in command mode by pressing ESC.

  • replace first occurrence of old in current line with new
   :s/old/new/
  • replace all occurrence of old in current line with new
   :s/old/new/g
  • replace the first occurrence of old in each line between line n1 and n2 with new
   :n1,n2s/old/new/
  • replace all occurrence of oldbetween line n1 and n2 with new
   :n1,n2s/old/new/g
  • replace all occurrence of old in the whole buffer with new, prompt for confirmation.
   :1,$s/old/new/gc
  • replace all occurrence of old in the whole buffer with new, prompt for confirmation.
   :%s/old/new/gc