|
Home | Switchboard | Unix Administration | Red Hat | TCP/IP Networks | Neoliberalism | Toxic Managers |
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and bastardization of classic Unix |
[Dec. 20, 2000] Some VI-related stories from LinuxToday
GNULinux.com: A Quick & Dirty Vi Tutorial(Jul 15, 2000)
Linux.ie: VI Tutorial III(Jul 08, 2000)
dotcomma: vi For New Users(Jun 11, 2000)
RootPrompt.org: The VI Editor [Tutorial](Apr 05, 2000)
DevShed: Vi 101(Mar 28, 2000)
PSCU: Vi Strain(Nov 28, 1999)
linux.ie: vi tutorial, episode 2 - filters, word count and more(Oct 29,
1999)
Linux.ie: vi Tutorial(Sep 22, 1999)
[Aug 19, 2000] Introduction to Programming in C/C++ with Vim Written By: Kmj
... ... ...
Ctags is a program that comes with vim. Basically, it's purpose is to help a programmer get to various parts of his program with relative ease. The typical method of running ctags is by simply typing the following in your source directory:
[/home/someuser/src]$ ctags *This will create a 'tags' file in you current directory with the following information for all C or C++ files in you directory. This file contains a listing of the following objects:
macros defined by #define enumerated values function definitions, prototypes, and declarations class, enum, struct, and union names namespaces typedefs variables (definitions and declarations) class, struct, and union membersVim then uses this file to help you locate these tagged items. This can be done in a few ways. First, one can open vim to the location of a tagged object. To do this, run vim with the '-t' flag and the tag-name as an argument. For example:
[/home/someuser/src]$ vi -t foo_barwould open to the file containing the definition of foo_bar at that line.
If you are already in vi, you can enter the command by the tag-name:
:ta foo_barThis may take you out of the file you are currently in, unless the 'autowrite' option is enabled. (It is off by default.) For more information on autowrite, type ':h autowrite' from with vi command-mode.
The final way to jump to a tagged location is by typing 'Ctrl-]' in command mode while the cursor is on a specific word. For example, if you are looking at my program, and you come across a point where I call foo_bar(), you can type 'Ctrl-]', while the cursor is somewhere on that word, and it will jump to that definition. Note that 'Ctrl-]' is the escape character for telnet, so this may cause some issues if your editing files remotely. Type ':h^]' for more information.
Ctags can also be used with other languages (java, fortran, ...and more) and editors (emacs, NEdit, ...and more). When set up properly, this tool can make your job tremendously easier, especially when you have to jump into a large ongoing project head-first.
For more information: View the man page, man ctags, or view the vim help, :h ctags.
c-style indenting
Vi has various methods of implementing auto-indenting. The best for C and C++ programmers is, obviously, cindent mode. This is a very versatile tool which gives the programmer much control over the look and feel of his source code, without any effort (except the effort of initial setup, of course). To enable c-indenting, just type ':set cindent' from the command mode. The most important thing to note is that cindenting makes use of shiftwidth, not tabstops. The default shiftwidth is 8. In order to change this, enter ':set shiftwidth=x' where x is the desired number of spaces to shift.
The default cindent options tend to be nice, but if you find your program indenting in some way that is annoying, you can modify the behaviour. To set the cindent options, type ':set cino=<string>', where <string> is a list defining exactly how you want your cindent options to behave. There are quite a few different types of indenting which can be done, and vim's help does a good job explaining them, so I won't go over them here. To view the help for the possible values of cinoptions, type ':h cinoptions-values'. To view the current values, simply type ':set cino'. Most likely there will be none, since everything starts at a default value.
For more information, check out the following: ':h shiftwidth', ':h cindent', ':h cinoptions', ':h cinoptions-values', ':h cinkeys', and ':h cinwords'.
syntax highlighting
Programmers who are used to integrated development environments know the beauty of syntax highlighting. It doesn't just make your code more readable; it also helps prevent annoying commenting and string errors. Vim has syntax highlighting for a number of languages, including C and C++ of course. To enable it, type ':syntax on'. Using it is as simple as that if you're happy with the default values. Vim's syntax highlighting tools can be quite complex, with a number of different things to play around with. To view information on syntax hilighting, type ':h syntax', which will lead you to vim's extensive help system. Syntax hilighting with color terminals and with gvim is nice, but if you don't have color, vim uses underlining, boldface, etc. To me, this is pretty ugly.
For more information: ':h syntax', ':h syn-qstart', ':h syntax-printing'
edit-compile-edit, a.k.a. Quickfix
This is a really nifty feature. Basically, by typing one command, you can cause vim to attempt to make the program you're working on, then open to whatever file first compiler error is in at the line of that error. The command to execute is ':mak' (or ':make'). Vim will run whatever program is denoted by the value of 'makeprg'. The default value for 'makeprg' is 'make'. You can change this, if you wish, by typing ':set makprg=<string>', where <string> denotes the desired command. Vim uses the 'errorformat' value to figure out how to understand the output from the compiler. Since different compilers have different output format's, you'll probably have to enter the format string. The method used is rather similar to C-style formatting with scanf. The most important format specifiers are %f, meaning filename, %l, meaing line-number, and %m, meaning message.
GCC's format string: %f:%l:\%mThis can become quite complex with different compilers, but fortunately, vim has a world of information in their help at ':h errorformat'.
For more information, check out: ':h quickfix', ':h mak', ':h makeprg', ':h errorfile', ':h errorformat'.
useful keystrokes
There are certain command-mode keystrokes that are especially useful for programmers. Below is a small subset of these:
Moving around within functions:
[ [ = Go to previous first-column '{'; equivalent to ?^{
] ] = Go to next first-column '{'; equivalent to /^{
[ ] = Go to previous first-column '}'; equivalent to ?^}
] ] = Go to next first-column '}'; equivalent to /^}{ = Go to previous blank line.
} = Go to next blank line.gd = Go to definition of current local variable (current = cursor is on it)
* = Go to next instance of current word
# = Go to previous instance of current word
'' = Go to location where last search was started.Parenthesis Matching:
% Takes you to the matching parenthesis, curly brace, or bracket, depending on what you are on. This always comes in handy as a quick double-check.Substution:
Vim has powerful substution capabilities, with a very simple interface. No annoying GUI to get in the way (though you may need to keep a cheat-sheet handy). To search for and replace text, use the following command:
: [address] s/<pattern>/<string>/[g|c|N] (where N is an integer value).This finds one (or more) instance of the grep-style regular expression represented by <pattern>, and substitutes it with <string>. 'address', 'g', and 'N' are modifiers which determine which and how many occurrences of <pattern> are replaced.
g = Global: Replace all occurrences of <pattern> on the line.
c = Cond. Ask before making each replacement.
N = Nth Replace only the Nth occurrence of <pattern> on the line.
(No modifier implies N=1, the first occurrence on that line)[address values] -May be one specifier or two separated by a comma. (below, x represents an integer).
. = The current line
$ = The last line in the file
% = Entire file
x = The xth line of the file
+x = x lines after the current line
-x = x lines before the current lineThe comma may separate any of the above in order to specify a range. All lines within the given range will undergo the substitution. The best reference I have found for substituting can be found at the Vi Helpfile, linked below.
Miscellany
Vim has so many nifty little things, it would be impossible to list them all. Here are a few more things that are worth taking a look at.
Include File Searching- ':h include-search' Bookmarking- 'mx' to set, ' 'x' to return; (x can be any letter, must stay within file to remember) "Clipboard" buffers- ' "xY ' to cut or copy to buffer x (Y represents any normal deletion or yank command), ' "xZ ' to paste contents of x (Z represents pasting... p or P); (x can be any letter, can switch to another file(:e filename) and still maintain contents of buffers). Comment Specifiers- ':h comments' .vimrc- Don't forget your .vimrc!!! (Located in your home directory). You'll find it very handy to add many of the above things to your .vimrc file, so that vim "remembers" everything you want it to. (You may find that some of them are already there.) Remember that you don't need to prepend a ':' to the command when adding them to the .vimrc.
Indenting Your Code and Checking
The VI editor has features to help programmers format their code neatly. There is a variable that to set up the indentation for each level of nesting in code. In order to set it up, see the customization section of this tutorial. For example, the command to set the shift width to 4 characters is :set sw=4.
The following commands indent your lines or remove the indentation, and can be specified with count:
- <<
Shifts the current line to the left by one shift width.- >>
Shifts the current line to the right by one shift width.The VI editor also has a helpful feature which checks your source code for any hanging parentheses or braces. The % command will look for the left parenthesis or brace corresponding to a particular right parenthesis or brace and vice versa. Place the cursor onto a parenthesis or brace and type % to move the cursor to the corresponding parenthesis or brace. This is useful to check for unclosed parentheses or braces. If a parenthesis or brace exists without a matching parenthesis or brace, VI will beep at you to indicate that no matching symbol was found.
OK, here's another fun little item that can save you HTML authors out there a bit of time. I've been doing a bit of writing over the past year and repeatedly come back to VIM as my hands-down favorite editor for simply getting things done. It's one of those editors which, if you keep you fingers on the keyboard and not have to peek all the time or feel compelled to push a mouse or other small rodent around to get work done, let's you get things done VERY quickly. Now, you can say the same thing about Emacs I suspect, but since I'm still using 'ol VI, you're stuck having to listen to me talk about this for the moment :-)
Anyway, one of the cool things about VI is the ":so" command which basically let's you create a series of command-mode commands which you save to a text file, and then you can simply have VI "source" this in order to run the commands.
Since there's nothing like a good example to clear things up, let's see how to use this with doing a bit of pre-formatting for HTML.
As you all know, there are several characters which are not "allowed" to be directly included in the text portion of an HTML document -- these are the "reserved" characters and they include:
the ampersand character: &
the less-than character: <
the greater-than character: >
the double-quote character: "
When a browser parses the HTML document these characters are used to convey formatting or other information and so cannot be directly input.
The problem comes in when you want to include text in an HTML document. Specifically, I include a lot of email in the 'ol LG. Problem is, that much of it contains so-called "reserved characters" -- those characters enumerated above that should not be directly entered into the text. In the past I've manually searched through each letter trying to ferret these out but, as with most Linux things, there's a MUCH easier and more efficient way to do this.
As your Uncle Louigi says, "Use your head, not your back..."
So, the first thing to note is that the first step in the email->HTML conversion is converting these characters to either a "Character Reference" or an "Entity Reference" (you know -- you have to use " for the (") double-quote character). You can easily do this using something like:
:%s/\&/\&/g
This particular command does a global substitution for the current file by converting every instance of "&" to "&". Do this for the other three reserved characters and you'll be on your way to dropping your email (or any other text document for that matter) into your HTML docs.
Now, you could obviously just enter these commands in by hand each time you want to do this, but hey, this is Linux and being lazy is OK -- as long as you're smart about it :-)
So, instead of typing in a set of commands to convert these characters to their Entity Reference format, we'll just set up a macro file which can be sourced for any text file. Using something like:
%s/\&/\&/g %s/</\</g %s/>/\>/g %s/\"/\"/g
will let you convert these automatically. So, how do you do this? Easy:
and Presto!! VI will grind away for a moment and all of the changes will be made for you.
Sourcing a macro file is one way to improve the speed and efficiency of doing things under VI(M) -- especially if you find yourself doing tasks that required frequent repetition. One small suggestion is to put these macros in their own directory off your HOME directory so that they are easily accessible from anywhere in the directory tree. To specify such a macro you could enter:
:so ~/macros/htmlconv
and the tilde (~) would be automagically expanded out to point to your home directory.
Anyway, this is one of those "tip of the iceburg" types of things because there is a HUGE amount of mischief you can get into using macros :-)
Have fun!
John
[July 18, 1999] VI paragraph formatting!
Date: Wed, 17 Apr 1996 15:22:58 CDT
From: Pat Traynor <[email protected]>
To: [email protected]
Subject: vi justification
Here's a modification on a vi tip that was offered a few issues ago.
In my .exrc file, I have the following entry:
map ^P {!}fmt^M
(The "^P" and "^M" are actual control characters that I had to escape with a <ctl>-V.)
When the cursor is anywhere within a paragraph and I hit a <ctl>-P, it re-aligns all of the text. It's just the thing for those occasions when you've typed in a long paragraph and then decide to delete or add several words near the top.
I won't go into the explanation of what everything does, since that has already been done. But rather than typing
:10,50! fmt
which requires that you know what the first and last lines of the paragraph are, the '{' moves you to the top of the paragraph, and the trailing '}' means to do this function up to the end of this paragraph.
--pat--
--
Pat Traynor
[email protected]
[July 14, 1999] EXT2
February 99 Issue -- interview with Bram Moolenaar creator of vim.
Although it's hard to teach old dog now tricks vim 6.0 will support folding
:-)
[May 20, 1999]
vile-perl-api --
Vile-Perl interface reference
Vi users who write "mappings" to abbreviate a series of commands know how tedious it can be to remember the keystrokes for this. Vim makes it easy with "macro recording": The letter 'q' (which is unused in Vi, btw) is used to start the recording; the next letter is the name of a register which is to store the keystrokes. Whatever you type after that is stored within the register until you type another 'q'. That's it. You can then play back this sequence with the command '@' followed by the register letter/name.
Example: Start recording into register 'a' G jump to last line of current buffer ?^-- $^M search backward for a line with sigdashes d/^$^M delete unto next empty line q end recording This simply deletes the signature from the current file. You can now edit another file with a signature and have it removed by simply replaying the macro with "@a".
Society
Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers : Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism : The Iron Law of Oligarchy : Libertarian Philosophy
Quotes
War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda : SE quotes : Language Design and Programming Quotes : Random IT-related quotes : Somerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose Bierce : Bernard Shaw : Mark Twain Quotes
Bulletin:
Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 : Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law
History:
Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds : Larry Wall : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOS : Programming Languages History : PL/1 : Simula 67 : C : History of GCC development : Scripting Languages : Perl history : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history
Classic books:
The Peter Principle : Parkinson Law : 1984 : The Mythical Man-Month : How to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite
Most popular humor pages:
Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor
The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D
Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.
This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...
|
You can use PayPal to to buy a cup of coffee for authors of this site |
Disclaimer:
The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.
Last modified: March 12, 2019