Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Vi(Vim) How-To

Share your HowTo, Documentation, Tips and Tricks. Not for support questions!.
Post Reply
Message
Author
User avatar
dawgie
Posts: 430
Joined: 2004-06-16 21:30
Location: New Hampshire USA

Vi(Vim) How-To

#1 Post by dawgie »

Vi basic basics
Sooner or later, you will find yourself editing a file with vi.
Vi is the first full screen editor for computers. Vi is the standard editor that is installed by default on any UNIX system. Because it uses very little resources, It is very fast even on old computers and slow network connections. Vi is very powerful, with almost unlimited features. The program that is installed on your machine is probably vim. Vim stands for (Vi)mproved.

Because vi has so many features, trying to read the documentation can be like "drinking from a firehose". You can use vi as an file editor by learning a few basic commands.

The basics

Vi runs in two modes:
command mode, and insert (edit) mode.
When vi starts, it is running in command mode.

Starting vi
Start vi from the console using the following command:
$ vi
or to use vi to edit a named file:
$vi filename


Inserting text
To begin inserting text, you press Esc key, then the i key, for insert.
i
Once you have pressed i you will see the cursor change and you will see --INSERT-- in the lower left corner of the screen if you are using a newer version of vi (vim). You now can start inserting text.

Saving (writing) changes
To save changes you made to a file, you need to be in command mode. To leave insert mode and enter command mode, you press the Esc key. The cursor will change again, indicating that you have now switched to command mode. You can write (save changes) to the file that you were working on with:
:w
or
:w filename

Quitting vi
To quit vi, you need to be in command mode. To leave insert mode and enter command mode, you press the Esc key. The cursor will change again, indicating that you have
now switched to command mode. You can quit vi with:
:q
or
quit vi and save changes:
:wq
quit without saving any changes with:
:q!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
At this point, you have enough information to begin using vi.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Read on for a few more basic commands.

A Few More Useful Commands:
(Remember that to enter command mode press the Esc key.)

Copy And Paste
To copy (yank) a line:
yy
To paste a line:
p

Search
/string_to_search
to search the next instance of the same string
/

Undo And Redo
undo
:u
redo
:red
undo everything
:U

Retrieve (open) The Contents of A File
:r filename

Run An External Shell Command
:! command



Help
You can access help with
:help
:q will close the help window


Vim Documentation page:
http://vimdoc.sourceforge.net/


Vim Tutorial:
$ vimtutor

Note
There are two types of commands:
External commands are preceded with a ":" internal commands are not preceded with a ":".

There are many, many more commands and features. Even the few basic commands that I have described can be elaborated upon to provide many more functions.

adssse
Posts: 71
Joined: 2006-01-17 05:36

#2 Post by adssse »

Thanks for posting. I use vi or vim on almost a daily basis. I know that some prefer emacs, nano, etc. but I will stick with vi/vim.

User avatar
simen
Posts: 203
Joined: 2005-07-02 15:00
Location: Norway

#3 Post by simen »

I try not to post redundant messages, but since it's to give due praise to dawgie, I'll do an exception. I've never used vi before (except by accident :wink: ). Emacs is my editor of choice, but when I'm in a shell, I often find myself running nano. After reading your consice and to-the-point post, I'll be sure to switch to vi. Being explained the basic idea of command mode vs. edit mode seems to unlock this esoteric editor. Thanks!

--Simen

User avatar
Kaitlyn
Posts: 129
Joined: 2005-12-13 14:35
Location: Big Orange

#4 Post by Kaitlyn »

If, after using this topic, you find you like using vim and want to go further with it, check out http://tiger.la.asu.edu/Quick_Ref/vim_quickref.pdf

Post Reply