The Vi editor is a programmer’s text editor. The VIM editor is a “Vi IMproved” editor.
In short, the Vim editor is an improved version of the Vi editor, with additional features.
On many distributions of Linux, the Vi editor defaults to the Vim editor; their function is primarily the same.
The good news, is that you do not have to be a programmer — or have any programming knowledge/aspirations — to use the Vi/Vim editors. They’re just editors. Specifically, they’re command line text editors.
Of all of the Linux command line text editors available, Vi is my personal favorite. I have never needed an alternative.
It offers enough features to accommodate all that I need to accomplish, and enough ease-of-use that I picked up its basic usage very quickly.
The Difference Between Vi & Vim
As mentioned, Vim is little more than an advanced version of Vi, with additional features; besides those features, their function is the same.
Some of Vim’s additional features are:
- Syntax Highlighting
- Unlimited (Multilevel) Undo/Redo
- Split Screen for Multiple File Editing
- Unicode Support
- Auto-completion
- Spellchecker
- Ability to Edit Archive Files
- Plugins (Customization, Etc.)
Vi Modes
To open vi, you can type vi
at the command prompt and hit Enter, or you can include the name of the file to edit vi myfile.txt
(or vim myfile.txt
) and hit Enter.
If you use a file name that does not already exist, it will be created in your current directory — assuming you save your changes when you exit vi.
VI has three modes — command mode, insert mode, and line mode.
Command Mode
Vi opens to command mode. Commands can be entered to navigate, modify, delete, search, and replace the contents of an open file, to name a few.
Anything typed while in command mode will not be inserted into the file, but will instead allow tasks to be performed, by interpreting keystrokes and combinations of keystrokes as commands.
While in command mode, what you type will sometimes show up temporarily on the bottom left-hand-side of your screen. This is especially common while typing in searches, etc.
Insert Mode
Insert mode is what I like to call “normal editing mode”.
When in insert mode, you can type in content, and perform normal editing tasks.
Anything you type, will be added into your file.
Line Mode
Line mode is much the same as command mode, except the commands begin with full-colons (:) and perform tasks such as saving, editing, and some navigation.
Using Modes
To use the various modes, you need to know how to switch between them.
Vi opens to command mode, so you can immediately begin entering commands.
To switch to insert mode, simply press the i key, and you will enter insert mode at your cursor’s current location.
Some other options to open insert mode are the I, a, A, o, O, r and R keys, which each enter insert mode in different positions or with different intents.
To return to command mode, hit the Esc key.
Line mode can be invoked any time that you are in command mode, by typing in a command beginning with a full-colon.
Type in :w
and hit Enter to save the file (write the changes).
Type in :q
and hit Enter to exit (quit) Vi.
Type in :wq
and hit Enter to save the file and exit Vi simultaneously. Typing ZZ does the same, without the need for Enter.
Type in :q!
and hit Enter to exit Vi without saving changes to the file.
With this knowledge of modes, you already have all the knowledge you need, to begin using Vi successfully!
Using Vi Commands
A large part of the reason that I like VI so much, is because of all the commands that it utilizes to perform various tasks. They’re like keyboard shortcuts.
Yes, this does mean more memorization, if you plan to be a master at using Vi. But even if you learn no more than a few basic VI commands, they go a long way toward productivity.
Vi commands are case-sensitive, so if you ever forget and type in the wrong case, the result may not be what you expected. On the plus side, this diversity allows for so many more command possibilities!
The Esc key can be used, not only to escape insert mode, but also while already in command mode, to cancel whatever you have been doing/typing and return to the default state. The Del key does the same.
To navigate Vi, both in insert and command modes, the Up/Down/Left/Right arrow keys can be used. In command mode, the k/j/h/l keys do the same thing, allowing you to keep your hands in the typing position.
There are multiple navigational commands that allow you to move forward or backward by words, sentences, paragraphs, lines, to specific lines and by screen (or half screen).
To search forward in a file, type a forward slash /, followed by the string to search for, and Enter. Example: /search for this string
To search backward in a file, type a question mark ?, followed by the string to search for, and Enter. Example: ?found me
Pressing n will take you to the next instance of the string in the current search direction.
Typing u will undo the last change. Typing u again will undo the last undo. Ctrl + R will redo a change.
Vi allows for only a single level of undo. Vim allows for multiple (unlimited?) levels. Pressing U will undo all changes to a current line.
Summary
There are many, many more Vi commands than what are covered here. This gives you only a brief sampling of commands available; more will follow, next week, in the form of a cheat sheet.
In the meantime, start experimenting, and see how quickly these command become second nature. Next thing you know, you might start trying to use them in your GUI text editors!
No Comments Yet