Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
Let The Music Play: Join EFF Today

 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, 78 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 Odds and Ends
  More vi Magic ---- Configuring vi  

vi Odds and Ends

You will find as you work with vi that you will often use the same vi commands over and again. Here too, vi can help. Because the named buffers are simply sequences of characters, you can store commands in them for later use. For example, when editing a file in vi, I needed to mark new paragraphs in some way as my word processor normally sees all end-of-line characters as new paragraphs. Therefore, I created a command that entered a "para-marker" for me.

First, I created the command. To do this, I opened up a new line in my current document and typed in the following text:


Had I typed this from command mode, it would have inserted the text "Para" at the beginning of the line. I next loaded it into a named buffer with "pdd, which deletes the line and loads it into buffer p. To execute it, I entered @p. The @ is what tells vi to execute the contents of the buffer.

Keep in mind that many commands, abbreviations, etc., are transitive. For example, when I want to add a new paragraph, I don't write Para as the only characters on a line. Instead, I use something less common: {P}. I am certain that I will never have {P} at the beginning of a line; however, there are contexts where I might have Para at the beginning of a line. Instead, I have an abbreviation, Para, that I translated to {P}.

Now, I can type in Para at the beginning of a line in input mode and it will be translated to {P}. When I execute the command I have in buffer p, it inserts Para, which is then translated to {P}.

So why don't I just have {P} in buffer p? Because the curly braces are one set of movement keys that I did not mention yet. The { moves you back to the beginning of the paragraph and } moves you forward. Because paragraphs are defined by vi as being separated by a blank line or delimited by nroff macros, I never use them (nroff is an old UNIX text processing language). Because vi sees the brackets as something special in command mode, I need to use this transitivity.

If you are a C programmer, you can take advantage of a couple of nifty tricks of vi. The first is the ability to show you matching pairs of parentheses ( () ), square brackets ([]), and curly braces ({}). In ex-mode (:), type set showmatch. Afterward, every time you enter the closing paren'thesis, bracket, or brace, you are bounced back to its match. This is useful in checking whether or not you have the right number of each.

We can also jump back and forth between these pairs by using %. No matter where we are within a curly braces pair ({}), pressing % once moves us to the first (opening) brace. Press % again and we are moved to its match (the closing brace). We can also place the cursor on the closing brace and press % to move us to the opening brace.

If you are a programmer, you may like to indent blocks of code to make things more readable. Sometimes, changes within the code may make you want to shift blocks to the left or right to keep the spacing the same. To do this, use << (two less-than signs) to move the text one "shift-width" to the left, and >> (two greater-than signs) to move the text one "shift-width" to the right. A "shift-width" is defined in ex-mode with set shiftwidth=n, where n is some number. When you shift a line, it moves left or right n characters.

To shift multiple lines, input a number before you shift. For example, if you input 23>>, you shift the next 23 lines one shiftwidth to the right.

There are a lot of settings that can be used with vi to make life easier. These are done in ex-mode, using the set command. For example, use :set autoindent to have vi automatically indent. To get a listing of options which have been changed from their default, simply input ":set" and you get something like in the following image:

Image - Vi set command. (interactive)

Inputting ":set all" will show you the value of all options. Watch out! There are a lot and typically spread across multiple screens. See the vi(C) man-page for more details of the set command and options.

Some useful set commands include:

  • wrapmargin=n

automatically "word wraps" when you get to within n spaces of the end of the line

  • showmode

tells you whether you are in insert mode

  • number

displays line numbers at the left-hand edge of the screen

  • autowrite

Saves any changes that have been made to the current file when you issue the :n, :rew, or :! command

  • ignorecase

Ignores the case of text while searching

  • list

Prints end-of-line characters such as $ and tab characters such as ^I, which are normally invisible

  • tabstop=n

Sets the number of spaces between each tab stop on the screen to n

  • shiftwidth

Sets the number of spaces << and >> shifts each line

 Previous Page
More vi Magic
  Back to Top
Table of Contents
Next Page 
Configuring 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.

The Linux Tutorial is always looking for new contributors.



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?
You can choose larger fonts by selecting a different themes.


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.04 Seconds