Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents
Up to --> Editing Files

· Vi
· vi Basics
· Changing Text in vi
· Moving Around in vi
· Searching in vi
· vi Buffers
· vi Magic
· Command Output in vi
· More vi Magic
· vi Odds and Ends
· Configuring vi

Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
Copyright Info
Terms of Use
Privacy Info
Masthead / Impressum
Your Account

Private Messages

News Archive
Submit News
User Articles
Web Links


The Web

Who's Online
There are currently, 75 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

Linux Tutorial - Editing Files - Vi - vi Magic
  vi Buffers ---- Command Output in vi  

vi Magic

I imagine that long before now, you have wondered how to turn on all that magic I said that vi could do. Okay, let's do it.

The first thing I want to talk about is abbreviations. You can tell vi that when you type in a specific set of characters it is supposed to automagically change it to something else. For example, we could have vi always change USA to United States of America. This is done with the abbr command.

To create a new abbreviation, you must get into ex-mode by pressing the colon (:) in command mode. Next, type in abbr, followed by what you want to type in, and what vi should change it to. For example:

:abbr USA United States of America

Note that the abbreviation cannot contain any spaces because vi interprets everything after the second word as being part of the expansion.

If we later decide we don't want that abbreviation anymore, we enter

:unabbr USA

Because it is likely that we will want to use the abbreviation USA, it is not a good idea to use an abbreviation that would normally occur, such as USA. It would be better, instead, to use an abbreviation that doesn't occur normally, like Usa. Keep in mind, that abbreviations only apply to complete words. Therefore, something like the name "Sousa" won't be translated to "SoUSA." In addition, when your abbreviation is followed by a space, Tab, Enter, or Esc, the change is made.

Lets take this one step further. What if we were always spelling "the" as "teh." We could then create an abbreviation

:abbr teh the

Every time we misspell "the" as "teh," vi would automatically correct it. If we had a whole list of words that we regularly misspelled and created similar abbreviations, then every time we entered one of these misspelled words, it would be replaced with the correctly spelled word. Wouldn't that be automatic spell correction?

If we ever want to "force" the spelling to be a particular way (that is, turn off the abbreviation momentarily), we simply follow the abbreviation with a Ctrl-V. This tells vi to ignore the special meaning of the following character. Because the next character is a white space, which would force the expansion of the abbreviation (which makes the white space special in this case), "turning off" the white space keeps the abbreviation from being expanded.

We can also use vi to re-map certain sequences. For example, I have created a command so that all I need to do to save a file is Ctrl-W (for write). If I want to save the file and quit, I enter Ctrl-X with the "map" command.

The most common maps that I have seen have used control sequences, because most of the other characters are already taken up. Therefore, we need to side-step a moment. First, we need to know how to access control characters from within vi. This is done in either command mode or input mode by first pressing Ctrl-V and then pressing the control character we want. So to get Ctrl-W, I would type Ctrl-V, then Ctrl-W. This would appear on the screen as ^W. This looks like two characters, but if you inserted it into a text and moved over it with the cursor, you would realize that vi sees it as only one character. Note that although I pressed the lowercase w, it will appear as uppercase on the screen.

So, to map Ctrl-W so that every time we press it, we write our current file to disk, the command would be

map ^W :w^M

This means that when we press Ctrl-W, vi interprets it as though we actually typed :w and pressed Enter (the Ctrl-M, ^M). The Enter at the end of the command is a good idea because you usually want the command to be executed right away. Otherwise, you would have to press Enter yourself.

Also keep in mind that this can be used with the function keys. Because I am accustomed to many Windows and DOS applications in which the F2 key means to save, I map F2 to Ctrl-V, then F2. It looks like this:

map ^[[N :w^M
(The ^[[N is what the F2 key displays on the screen)

If we want, we can also use shifted function characters. Therefore, we can map Shift-F2 to something else. Or, for that matter, we can also use shifted and control function keys.

It has been my experience that, for the most part, if you use Shift and Ctrl with non-function keys, vi only sees Ctrl and not Shift. Also, Alt may not work because on the system console, Alt plus a function key tells the system to switch to multiscreens.

I try not to use the same key sequences that vi already does. First, it confuses me because I often forget that I remapped something. Second, the real vi commands are then inaccessible. However, if you are used to a different command set (that is, from a different editor), you can "program" vi to behave like that other editor.

Never define a mapping that contains its own name, as this ends up recursively expanding the abbreviation. The classic example is :map! n banana. Every time you typed in the word "banana," you'd get


and depending on what version you were running, vi would catch the fact that this is an infinite translation and stop.

 Previous Page
vi Buffers
  Back to Top
Table of Contents
Next Page 
Command Output in vi


Test Your Knowledge

User Comments:

You can only add comments if you are logged in.

Copyright 2002-2009 by James Mohr. Licensed under modified GNU Free Documentation License (Portions of this material originally published by Prentice Hall, Pearson Education, Inc). See here for details. All rights reserved.



Security Code
Security Code
Type Security Code

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!

Amazon Wish List

Did You Know?
The Linux Tutorial can use your help.


Tell a Friend About Us

Bookmark and Share

Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.14 Seconds