Vi in a Nutshell

linuxmisc

The unix text editor vi probably has more features than Microsoft Word yet I've only been using about 5 of them. Recently I've been learning a bit more about it. I now know some of the more handy features thanks to the VI reference in forth edition of Unix in a Nutshell which was just released in October.

The reason I think vi is an important editor to learn is that it is typically installed on any unix system (including Mac OSX) by default. Other unix text editors such as emacs aren't installed by default as often.

Basic VI features (beginners only)

Using vi is pretty easy if you know a few things about it, if you don't you probably won't even be able to edit or save a file. Lets start by editing a file /tmp/somefile

vi /tmp/somefile

Vi starts up in command mode, you can give VI commands to run such as delete this line, search for this, etc.

To start we want to give the insert command so hit i, you should see -- INSERT -- at the bottom of the screen. Go ahead and write a haiku or something.

Now to save it you need to go back into command mode this is done by hitting ESC now type wq (this means write and quit).

Ok that's the basics you can get pretty far just knowing that.

Handy Features in VI

Here's a list of some of the commands that I have found to be quite handy:

  • Search :/pattern - search for pattern in the file. This can be a regular expression. Hit n to go to the next match.

  • Find and Replace :s/find/replace/g
  • Go to End of Line $
  • Beginning of Line ^
  • Last line in the file L
  • Middle of the file M
  • Top of the file (Home) - H
  • Go to line 10 :10
  • Delete current line dd
  • Copy or yank a line Y - you can use 5Y to copy 5 lines. Also try yG, and y$.
  • Paste p
  • Undo u
  • Redo Ctrl+R

Have any other handy vi tips? post them in the comments.



Related Entries

13 people found this page useful, what do you think?

Trackbacks

Trackback Address: 504/905B96A394F4BA6354316DA64BC3E6FE

Comments

On 12/01/2005 at 3:25:21 PM EST steve wrote:
1
You can use the % key to toggle between (), {} and [].

On 12/01/2005 at 3:33:48 PM EST Pete Freitag wrote:
2
Thanks steve, that one is really handy for programming.

On 12/01/2005 at 4:50:55 PM EST Scott Fitchet wrote:
3
My favorite vi tip is: "pico".

J/K: I'd love to use vi full time it just always seemed like a pain in the ass.

On 12/01/2005 at 6:09:18 PM EST Pete Freitag wrote:
4
Yeah nano, is another easy to use editor for unix. vi does have a lot of power though.

On 12/01/2005 at 6:19:30 PM EST steve wrote:
5
Sometimes it's handy to see non-printed characters such as tabs and carriage returns. You can display them with :set list and switch back to normal with :set nolist

It came in handy in the days of Makefiles and also for checking shell scripts to make sure there were no spaces/tabs following a \ for line continuation.

On 12/02/2005 at 6:10:59 AM EST Koen wrote:
6
I always use this (http://tnerual.eriogerg.free.fr/vim.html) reference card. It's for vim though. ;)

On 12/03/2005 at 7:06:34 PM EST John wrote:
7
Hi Your terminology is incorrect for the commands H, M & L. Those commands don't take you to the top, middle & bottom of the current file but instead take you to the top, middle & bottom of the current screen. For example if you are editing a 100 line file and the screen is displaying lines 20 - 45 pressing H would place the cursor on line 20 (top of the displayed screen), M would place the cursor on line 33 and L would place the cursor on line 45. You could also try {gq} to format a paragraph

On 12/03/2005 at 7:15:30 PM EST John wrote:
8
when editing a file and you're on a line with say "echo $somevariable" and you want to get to where somevariable is defined, place the cursor on the s of somevariable and press gD it will take you to the definition. Also you can make a macro of commands and bind them to a key by typing :map , listofcommands then whenever you press , (in command mode) vi will issue the command.

On 12/03/2005 at 8:29:41 PM EST Pete Freitag wrote:
9
John - thanks for sharing your knowledge. I'll update the post to reflect that when I get a chance.

On 12/04/2005 at 1:33:02 PM EST Ryan Guill wrote:
10
I am new to vi, actually just got ubuntu installed on an external hd yesterday. In my run-ins so far with it, it seems very very similar to vim from the command line. Are they the same thing?

On 12/04/2005 at 2:28:07 PM EST steve wrote:
11
VIM = Vi IMproved

On 12/12/2005 at 12:21:08 AM EST dawookie wrote:
12
in command mode: O - Opens a line above the cursor, moves the cursor there, and changes to edit mode. o - Opens a line below the cursor, moves the cursor there, and changes to edit mode. a - Appends text starting at the next character to the right. A - Appends text starting just after the last character on a line. . - Re does the last editing action. n - finds the next occurrence of the last search.

On 12/19/2005 at 8:55:04 AM EST Darkglam wrote:
13
Other basic trick:

dw -> delete word.

joe( Joe's Own Editor) fan here.

On 05/16/2006 at 1:05:24 PM EDT Anonymous wrote:
14
moves the cursor there, and changes to edit mode. a - Appends text starting at the next character to the right. A - Appends text starting just after

On 05/16/2006 at 1:08:07 PM EDT Anonymous wrote:
15
The reason I think vi is an important editor to learn is that it is typically installed on any unix system (including Mac OSX) by default. Other unix text editors such as emacs aren't installed by default as often.

Basic VI features (beginners only) Using vi is pretty easy if you know a few things about it, if you don't you probably won't even be able to edit or save a file. Lets start by editing a file /tmp/somefile

vi /tmp/somefileVi starts up in command mode, you can give VI commands to run such as delete this line, search for this, etc.

To start we want to give the insert command so hit i, you should see -- INSERT -- at the bottom of the screen. Go ahead and write a haiku or something.

On 01/05/2007 at 5:09:34 PM EST Adam Ness wrote:
16
I've been getting "back to my roots" editing PHP code with GVIM at home, but I was tossing around the idea of using it at work with my ColdFusion code... Do you know of any VIM modefiles for ColdFusion?

On 07/08/2007 at 1:02:51 PM EDT Michael wrote:
17
When I am editting and working on a commented line (starting in #) and I hit enter, this version of Vim automatically starts the next line with a #. Convenient, except for the times I cut and paste, and there's one commented line. Then all pasted after the commented one are given #'s at the beginning of the line and I have to go back and remove them all. Annoying.

Is there any way to turn this feature off?

On 09/11/2007 at 2:09:47 PM EDT kirit wrote:
18
use 50% to go to exactly half of the file, 20% will move cursor to 20%th line of the file.??? 1G will move to the beginning of the file, G will move to the end of file. kcmakwana@rediffmail.com

On 10/06/2007 at 10:47:54 AM EDT PikesPeakCNC wrote:
19
A really handy feature is the '.' key (a period). It repeats the last edit, either new text or a delete. For instance, use 'dw' to delete a word, then '.' will continue to delete the next word each time it is pressed. If you want to change all occurances of some word, search for it, make the change, and then use 'n' to find the next occurance. Repeated use of 'n.' will find and replace.

One step further is to map another key to a sequence of keys. Use ":map z n." to make a press of the z key do the same as "n.". Now you can hold the z key down and FLY through your file. This is especially handy for lists.

On 08/27/2009 at 11:01:31 AM EDT impu wrote:
20
hii!! i hav just installed ubuntu 9.04.. but vi doesnot seem to work..pressing i or a.. doesnot show INSERT below.. though we can type something.. and back space doesnt delete anything.. and pressing the up down arrow keys prints characters.. can anyone help??

On 04/07/2010 at 11:51:14 PM EDT Elaina wrote:
21
The command to write and quit is ESC :wq At least I needed the colon.

Post a Comment




  



Spell Checker by Foundeo

Recent Entries



foundeo


did you hack my cf?