LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Linux Answers > Applications / GUI / Multimedia
User Name
Password

Notices


By Defunct. at 2004-08-14 21:33
-------------
Introduction:
-------------

Vim stands for "vi improved", and is a *nix environment CLI (command line interface) command used for creating and editing plain text files. Vim is not a hard application to use, but can be quite discouraging at first glance from it's clear lack of graphical features that usually adorn most text editors and *word programs so commonly used today. However, this doesn't change the fact that if you are in a *nix environment, the CLI and such methods of editing text files will become a necessary skill that you will use on a regular basis.

What's been improved about vi to make vim? Multi-level undo, multi windows and buffers, syntax highlighting, command line editing, filename completion, on-line help, and visual selection are among some other modifications.

To read more instruction and information than what will be explained here about vim, don't forget about the manual:

Code:
$ man vim
------------------
Basic Command Use:




------------------

**From this section on, it's assumed you have basic CLI knowledge and know how to use general commands.**

Running from your shell/terminal of choice, vim is started by issuing the vim command, followed by a file name.

Code:
$ vim mynewfile
Alternatively, if you already have a file in your directory named mynewfile, running the same command above will allow you to edit it's contents.

A list of other actions while in vim:
up arrow -- moves cursor up one line;
down arrow -- moves cursor down one line;
right arrow -- moves cursor right one character space;
left arrow -- moves cursor left one character space;
shift + c -- enters to "Insert" mode (write/edit mode);
i -- enters into Insert mode, wherever the cursor may be;
o -- enters into Insert mode, with a new line printed;
ctrl + c -- exits from "Insert";

(Out of "insert" mode)
ZZ -- exit and save any changes
x -- delete one character

ctrl + z -- exits vim and returns to the directory;
:q! -- forces an exit without saving changes [q - quit];
:wq -- exits and saves changes [w - write, q - quit].

As always, files are case sensitive, so make sure you watch out for that.

---------------
Active Example:
---------------

For an active example, a new text file will be made. It will be named "clown", and contain the second verse of the poem "Clown in the Moon".



Code:
$ vim clown
Now you are taken to a a screen that will look like:

Code:
~
~
~
~
"clown" [New File]

Each tilde shows one line, with the words ""clown" [New File]" down the very bottom of the screen. Now, to enter the text of the second verse of the poem "Clown in the Moon", the user has to enter "Insert mode". Looking at the small list of commands provided, this is accomplished by pressing shift + c, or "i".

You will know when you are in "Insert mode" when the words ""clown" [New File]" are replaced with "-- INSERT --".

Code:
~
~
~
~
-- INSERT --
Any key you press now will show up on the screen, starting from the top right hand corner.
The verse being used is:

"I think that if I touched the earth,
it would crumble;
It is so sad and beautiful,
so tremulously like a dream."

So, just type that in (with or without quotes, matters not). When you are done it should look like:

Code:
I think that if I touched the earth,
it would crumble;
It is so sad and beautiful,
so tremulously like a dream.
~
~
-- INSERT --

Basic key strokes are the same as usual; return makes a new line, del deletes characters, etc., so there's nothing new in that area. After entering the data to the new file, there are a few things that can be done. Pressing ctrl + c or ESC will exit you from "Insert mode", and effectively prevent you from typing; if you do not wish to keep the content of the file, you can type :q! and nothing will be written to the file, and you will return to the directory. Similarly, pressing ctrl + z will also do this. Typing ZZ will write the data and quit to the directory, as will typing :wq

Assuming the file is to be saved, pressing ESC followed by :wq will save the file and put you back in the directory. To view your work to make sure it is correct just use cat.

Code:
$ cat clown
I think that if I touched the earth,
it would crumble;
It is so sad and beautiful,
so tremulously like a dream.
$ _
----------------------------
Editing an Existing an File:
----------------------------

Code:
$ vim clown
Seeing as this file already exists, using vim on it will simply open it up and allow you to view and/or edit it. There is a small typo in the file 'clown', so to fix that up simply issue the vim command on the file

Code:
$ vim clown
And following the usual commands of ctrl + c or "i", move the cursor along to the typo ("thikn", second word, first line) and edit it with the correct spelling, "think". Write and quit using :wq

-------
Ending:
-------

Although CLI can be a little intimidating and daunting to people new to the Linux OS, enough practice and use with it will remove the majority (if not all) of those Windows-habits of returning to GUI. As you can see, it really is not a difficult skill to learn; and with the internet, resources are much more easily available with a little effort.

by yapp on Wed, 2004-09-15 03:50
The article says you can use "Ctrl+Z" to quit vim, but this is not true. It will suspend VIM in the background, and you can use "fg" in bash to pull vim to the foreground again..!

by ror on Fri, 2004-11-05 17:44
Also it makes no note of vimtutor an excellent inbuilt interactive tutorial to vim.

by shshjun on Thu, 2005-05-12 12:18
> and with the internet, resources are much more easily available with a little effort.

is there a CLI fast way to do this? maybe i can use xemacs or something? just found this irony -- ss

by Kahless on Wed, 2005-07-06 14:19
a cli way to do what? use the internet?


links or lynx <website.com>

by stevenjoseph on Thu, 2005-12-22 19:17
three more post till i can post an external link

by geoff_f on Sun, 2006-04-30 23:49
[edit: entered in error]

by geoff_f on Tue, 2006-05-02 06:46
[edit: entered in error]

by Keiran on Mon, 2009-11-02 07:58
Thx ror - that vimtutor tip is priceless

by rupal.chawla on Mon, 2014-09-29 06:09
Vi is a useful but a troublesome editor for Linux.
Always make a copy of original file before making the changes.

by jefro on Mon, 2014-09-29 20:33
Hello and welcome to LQ.


Ok, thanks.


A 10 year old post by the way.


  



All times are GMT -5. The time now is 06:44 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration