A Quick Guide to vi
Pages: 1, 2
Saving a File
If you start vi by specifying a filename, you can simply save the file by pressing :w and return. Note that pressing ":" when in Command mode will bring you to the bottom of the screen. This is where you can issue further commands.
If you start vi without any parameters, you need to provide a filename:
~
~
:w newfile.txt
You can also use this method to save the file into another file. Note that if the filename you specify already exists, vi will not overwrite the existing file. Instead, use :w! to overwrite the existing file.
|
|
Inserting a File
There may be times where you need to add the content of another file into your current file. In this case, you can use :r, like this:
~
~
:r anotherfile.txt
Simply first position the cursor at the line where you want to insert the file and then press :r.
Quitting vi
To exit from vi, use the :q command. If the file has been modified and you have not saved the changes, vi will not allow you to quit. To quit without saving, use :q!.
|
Related Reading Learning the vi Editor |
Opening a File
If you want to edit another file while you are still in vi, you can use :e to open another file for editing:
~
~
:e textfile.txt
Copying, Cutting, and Pasting Text
To copy a block of text, you can use the y (for yank) command. To yank the whole line, press yy.
|
yy copies the line "vi is an editor in Unix"
|
To copy a word, use yw.
|
yw copies the word "editor" |
To copy from the cursor to the end of the line, use y$.
|
y$ copies the line "editor in Unix"
|
To paste yanked text, use the p (for put) command.
|
Continuing from the previous y$ example, P (capital p) results in:
p (small p) becomes:
|
Note that for cut operations, you can use the d command (see "Deleting Text") to cut a block of text and p to paste the block of text.
Undoing and Redoing
You can undo your action by pressing u. Note that you can only undo the last action. To redo the same action, use the period (.).
Changing and Replacing Text
You can change the text in your file by using the c command. For example, cw allows you to change the entire word:
|
cw Solid (and then press escape) will replace the word "Real" with the word "Solid" |
To change the last three words, use c3b.
To change the words starting from the current cursor position to the end of the line, use c$.
To change the words starting from the beginning of the line to the current cursor position, use c0 (numeric zero).
To replace a single character, position the cursor at the character you want to replace and type r, followed by a character.
Searching for Text
To search for a particular piece of text in vi, use the / command followed by the text for which to search. For example, /in will look for the first occurrence of the word "in". To repeat the search, press / and then Enter.
To replace all occurrence of a word in a document, you can use the s command. For example, :s/in/on will replace the first occurrence of the word "in" with the word "on".
:s/in/on/g will replace all occurrences of the word "in" with the word "on" on the current line.
:1,45s/in/on/g will replace all occurrences of the word "in" with the word "on" from line 1 to line 45.
:%s/in/on/g will replace all occurrences of the word "in" with the word "on" in the entire file.
Jumping to a Line
If you are editing a large file, you can jump directly to a line by typing the line number. For example, :4 will position the cursor directly on line four. This is useful when you are debugging a program and the compiler has indicated an error on a particular line.
I hope you find this quick tutorial to vi a useful one. Remember, the more you use vi the more you will enjoy it. Have fun!
Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to the Mac DevCenter.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 12 of 12.
-
Re: or *don't* use pico
2003-02-26 05:20:01 anonymous2 [Reply | View]
> beware of pico. It does strange things will long
> lines, which is not entirely surprising when its
> major function is to compose email messages.
> What *will* be surprising is the havoc you can
> wreak by having your system configuration files
> broken in all the wrong places!!
The default Mac OS X /usr/bin/pico is compiled with the -w flag so this is not a problem. If you compile your own pine & pico (which I did), then you need to use "pico -w filename" if you used pico to edit config files.
Good thing to be aware of though,
-anonymous
-
ZZ save & quit
2003-02-25 13:38:18 anonymous2 [Reply | View]
ZZ (shift-z twice in a row while in command mode) will save a document and quit, handy for quickie edits. I like it much more than :wq!
-
A good general resource for vi (vim)
2003-02-24 13:28:27 anonymous2 [Reply | View]
You should also check out this nice tutorial on the use of vi (vim) available here: http://www.systemtoolbox.com/article.php?articles_id=57
It is very thorough and offers another exercise to further help you grasp vi (vim) concepts.
It is also available under and open content boilerplate license.
Check it out and let the author know what you think.
-
Swapping letters
2003-02-24 12:18:48 anonymous2 [Reply | View]
If you swap letters when typing: 'hte', then go to the first of the two letters ('h') and type 'xp'
The x cuts the letter (sliding the next letter ('t') back under the cursor), the p pastes the just-cut letter after the cursor, leaving you with 'the'.
Another typo fix: the '~' inverts case.
-
or *don't* use pico
2003-02-23 18:47:31 anonymous2 [Reply | View]
Wrong forum for the sort of comments we're seeing here (I know, I know...) but if you're "just looking to edit a few config files" beware of pico. It does strange things will long lines, which is not entirely surprising when its major function is to compose email messages. What *will* be surprising is the havoc you can wreak by having your system configuration files broken in all the wrong places!!
Just learn vi. Then learn vim. Life in your terminal will be much better.
-
Or just use pico...
2003-02-22 21:16:35 anonymous2 [Reply | View]
Skip the masochism/learning curve of vi and just use pico.
I have no doubt that someone well-versed in the arcane arts of vi or emacs can out-perform someone using something "simple" like pico.
But if you're just editing a couple of config files or need to quickly whip up a web page on the commandline...use pico.
-
Use vilearn.
2003-02-22 19:58:30 anonymous2 [Reply | View]
There's a tutorial for using vi that actually has the user in vi during the instruction process. It's a shell script and a collection of text files. There's a Mac OS X installer package for it here:
http://vilearn.org
Frankly, it's much more intuitive than this article, since you're in vi while you read the tutorial.
-
gViM and ViM on OS X
2003-02-22 09:48:55 anonymous2 [Reply | View]
ViM is Vi-Mproved (Vi Improved) It's like Vi but on steroids! Vi is great but ViM is awesome!
ViM has a Vi compatibility mode that switches off all the extras and behaves exactly like Vi. It's possible to execute the command vi and get ViM in vi compatible mode. (how it's done on RedHat and other Linux distro's) Then execute vim and get the console ViM. In addition you get a graphical Aqua ViM for OS X. Finally, it can also work in X11 as well. (you'll have to compile it yourself to get all three at once, probably having two different gVim executables; one for X11 and one for Carbon OS X)
You can install gViM, which will give you a graphical ViM. It's been ported via Carbon. The latest binary version can be found here http://macvim.swdev.org/OSX/ There's even instructions for compiling it yourself so you can include different options.
Here http://vim.sf.net/ is the main ViM homepage where you can get scripts and tips from thousands of ViM users.
ViM is orphanware meaning all they ask is that you donate some money to the Ugandan orphans by either making a donation or buying the New Riders book. You can read the book online here http://www.newriders.com/books/opl/ebooks/0735710015.html or you can download a PDF of the book here http://www.truth.sk/vim/vimbook-OPL.pdf
Of course if you prefer EMACS then you can also find an OS X Carbon port here: http://www.porkrind.org/emacs/
No need to start the vi/ViM v.s. EMACS wars on this articles comments. Try both, give both a fair shot and choose what suits you best. EMACS is more powerful than ViM. It can do a whole lot more. EMACS is the kitchen sink of tools. ViM only wishes to be an editor. EMACS is more of an evironment that can be or do whatever you can imagine.
Console EMACS is already included in OS X as GNU Emacs 21.1.1. The next major version of EMACS will include EMACS Carbon in the main source tree. The link above is for the beta of that version.
Enjoy!
-
another handy one
2003-02-21 20:20:33 anonymous2 [Reply | View]
OK, two:
1. install vim.
2. do something like
:%!grep foo
and you'll be left with only the
lines that have foo in them. It's
a unixy filter thing.
-
Best trick...
2003-02-21 17:22:01 anonymous2 [Reply | View]
(I'm amazed there's no "best trick is to enter
:!exec emacs -nw
while in command mode" posts yet), and I'm having similar trouble resisting the temptation to name "install vim" as the best hint for anyone who's learnt a little vi and is sick to death of arrow keys not working correctly in insert mode and so forth. So I'll only do that pa(ren)thetically.)
Given all that... maybe a couple of little tiplets?
Instead of / and enter as "find next", simply use "n".
Including the output of a command in your file is quite fun: eg, to insert the date:
:r!date
or :r!cal for a minicalendar.
And so forth.








<http://www.entropy.ch/software/macosx/welcome.html#vim>
and include these 2 lines in my .vimrc
set term=builtin_beos-ansi
syntax on
and it's heavenly!