Thursday, April 14, 2011

QuickStart: VIM (Vi)


VIM (Vi IMproved) is installed by default, in one way or another, on most every flavor of UNIX and Linux. This is one of its best features. If you take a minute to learn it, you will be able to quickly edit text files from the command line on any *NIX box you come across. There are plenty of sites out there that list all the different vi commands, but they can be overwhelming. This post is meant to get you using vi and VIM, so I'll show you the basics.

First things first, to start the program, simply fire up a terminal and type vi.

synapsys@host:~$ vi

If you want to open up a particular file for editing, simply add the filename after the command.

synapsys@host:~$ vi examples.desktop


When vi first opens a file, it is in "command mode." In this mode we can issue commands to save, quit, etc. In order to change to "insert mode" we simply hit the "i" key. To return to command mode, hit the <Esc> key. Navigate to the spot that you want to edit with the arrow keys, then hit the "i" key to change to insert mode, then start typing. If you are getting characters like A, B, C, or D, when you press the arrow keys, you probably have a limited version of vi installed called vim-tiny (in Ubuntu.) To fix this, you will need to install the full version of vim. When you are done editing the text, press the <Esc> key to return to command mode. The only commands you need to know, for now, are as follows:

 :q!
  Quit without saving
 :w filename 
  Save as filename
 :wq
  Write changes to the file and quit 

If you want to open a new file for editing in vi, simply use this command:

synapsys@host:~$ vi newfile

That's it, you know enough to get started using this great program. For more information, see this site, and/or read the fine man page. 

No comments:

Post a Comment