|[Home] [Credit Search] [Category Browser] [Staff Roll Call]||The LINUX.COM Article Archive|
|Originally Published: Thursday, 17 August 2000||Author: Jim Jones|
|Published to: learn_articles_firststep/General||Page: 1/1 - [Printable]|
Easing Into Editors: Vim
Most work done in Linux is facilitated through the use of one of its many editors. You could be writing a quick note, trying to get that darned X windows configuration file tweaked just so, or programming the next great Open Source widget. This series will take you through the fundamentals of working with files in the myriad of editors available to the Linux user. This first installment will deal with one of the "standard" editors found on most Linux systems, vim.
|Page 1 of 1|
When you get down to brass tacks, most work done in Linux is facilitated through the use of one of its many editors. You could be writing a quick note, trying to get that darned X windows configuration file tweaked just so, or programming the next great Open Source widget. All of it done, in one way or another in one of Linux's many editors. For added complexity, you are probably new to Linux. You haven't written notes outside of post-its, much less having any idea what an "X windows configuration file" might be (it tells Linux how to set up the X windows system, a Graphical User Interface or GUI, for the display hardware of the computer Linux is running on), nor have you programmed anything other than your coffee maker. That's all right. This series will take you through the fundamentals of working with files in the myriad of editors available to the Linux user. This first installment will deal with one of the "standard" editors found on most Linux systems, vim.
From There to Here:
First, a little history. The vi editor (precursor to clones like elvis, ni and, of course, vim) sprang, in very Greek fashion, from the head of the standard Unix editor of the time called ex and its precursor, ed. While no one ever claimed that Unix or Unix-like text editors were non-esoteric, vim's precursors were difficult to use with even that in mind. The ed and ex editors were line-oriented editors (allowing the user to only see whichever line they were currently working with). This made it difficult to edit a line in context with its surroundings. The ex editors advantage was that it had a visual display mode that you could get to by giving the ex editor the vi command. This became such a popular feature of the editor that people would set it so that ex started up in "visual mode" by default.
The vim editor is one of the most popular clones of the original vi editor. Vim stands for "vi improved". Vim's biggest improvement over its ancestors is its support for color syntax highlighting of program code. This feature improves programming productivity by giving visual cues to a word or command's significance with respect to the language being used by the programmer. Other people (like myself, for instance) like vim because it is less picky about the the current mode it is in (more on this later) when editing a file.
However, even with these improvements, it should be noted that vim is not particularly well suited for text layout or formatting. Its sole job is to work with text in a format independent way and it does a darn good job at it. Thus making it one of the most popular editors (and a Unix/Linux standard) that is found on virtually every system out in the known world.
A Few Words About Modes:
The vim editor operates in one of three modes at any one time. Each mode, including input mode, command mode, and status line mode (aka last line mode), is used for specific operations within the editor. In vim, command mode is used to issue short commands (of 1 to a couple characters each) which allow you to manipulate the text or editor environment in a number of ways. Input mode, is used for inputting text into the editor's work buffer (basically the "workspace" of vim before it is committed to the hard drive through a "save" operation). Finally, status line mode is used for issuing longer, more complex commands to the editor. These commands are echoed back to the user in the status line of the editor (demarcated by a ":" at the bottom of the screen) and are executed when the user hits the enter key.
Switching between modes is relatively easy and quite necessary to make use of the full complement of vim's tools and abilities. To change to command mode, you generally just have to hit the
I think that covers the basics and the background. Now, let's get down to the nitty gritty.
Starting the vim Editor:
First, let's start up vim in a terminal session. The syntax for creating a file (or working with a file already in existence) is:
For our purposes, we will be working with a file name fstep.vim, so we type:
This will open the vim editor for working with the fstep.vim file. You should see something that looks something like this:
Other vim startup options include: starting vim in read only mode (
Now let's look at adding some text to fstep.vim.
Adding Text in vim:
Let's add the party line definition of Linux to our file. We'll borrow the initial definition from the FirstStep section of Linux.com (http://www.linux.com/firststep/#whatislinux). In case you haven't seen it, here's what it says:
Linux, the most popular Unix variant for PCs, is the only operating system not from Microsoft that is gaining significant market share. Created by Linus Torvalds and developed collaboratively over the Internet, Linux is the great success story of Open Source software development.
We can do this by entering input mode via the
To add the Linux definition to fstep.vim, type
We've done pretty much so far, but now we need to cover some of the mechanics that you will use to actually edit a file. Editing, of course, requires being able to move to the trouble spot and excise or repair whatever "damage" exists (very much like a surgeon, to be sure). In the next section we will cover cursor movement.
Moving the Cursor in vim:
Basic cursor movement in vim is pretty straight forward. While in command mode (press the
Larger documents may require extensive editing, for which movements of one space at a time simply won't do. For these, we require a broader set of movement tools. Need I say that vim shall provide? Vim has conventions for moving forward or backward one word at a time (
Let's cover a few commands for cutting, pasting and removing text and then we'll apply what we've learned to fstep.vim.
Some Editing Commands for vim:
Most anyone who has worked with any processor is aware that copying and pasting text is a pretty standard feature across applications and some operating systems. Many know that the standard keyboard shortcut for copying is
To "copy" text in vim, you perform a "yank" command. Vim's version of pasting is achieved through a "put" command. You can yank the current line that the cursor is placed on by typing
To "paste" your "yanked" text (this is starting to sound odd), simply move your cursor to the point you want to "put" the new text and type
To get rid of text, vim offers a host of tools almost identical in nature to the yank command set. To delete text in vim, you use several variations of the
Now, let's apply some what we've learned
An Example in vim:
We're going to change the first line of our example into two seperate sentences (this is already sounding like a standardized test). When we last left fstep.vim, it looked like this:
In order to edit the first sentence we are going to have to get the cursor up there. First, hit the
When you are all done it should look something like this:
That's it for now. I hope you will give vim a chance in your future explorations with Linux. It is handy to know since it (or one of its clones) is installed by default on almost every Linux and Unix system out there and can come in handy in a pinch. While this is by no means an exhaustive look at the editor, it should give you enough commands to get you started and keep you from pulling your hair out. For further information on vim you can type
Raised by wolves in the northern forests of Germany during his father's stint in the US army, Jim decided to throw off the "easy life" of a lean and mean wild child in order to explore the theoretical possibilities of actually graduating college with a computer science/art degree and no money. Having survived that adventure by the skin of his loans, he is now slowly working towards his lifetime goal of becoming one heck of a geek. Writing for Linux.com is the next step in that evolution. Email: firstname.lastname@example.org
|Page 1 of 1|