To start off let's stop using VI and use Vim (VI iMproved). Vim gives us more features and the ability to have syntax highlighting. In most cases syntax highlighting works out of the box. In a later article I will show how to make basic customizations to your Vim environment. If syntax highlighting is not working refer to the excellent Vim documentation here. The official Vim documentation is a place you should go often to learn how to use Vim.
The next 7 commands we will learn are: :, a, A, I, $, 0, and :s/search/replace/[g][c]. here is a listing of
:$ - moves to the beginning of line . This is very useful when you are debugging a script. Often you will run a script but get an error saying something like this:

Now open the file with vim and type ":33". This will move you to the beginning of line 33. Now you can make your edit and save the file. ":


$ and 0. The next commands to learn move your cursor between the beginning and the end of the current line. You must be in command mode for this to work. To go to the beginning of a line type "0". To move to the end type "$".
a and A. the "a" command appends text after the position of your cursor. For instance, if you type "$" to get to the end of a line you do not generally want to use "i" insert. Instead you will type "a" to append after the line. (A shortcut is to use "A" which will move to the end of the line and append text.
I (notice it is capital). The "I" command moves the cursor to the beginning of the current line and inserts.
These are the rudimentary commands that will move you very close to basic productivity in Vim. The last command to learn is search and replace. It is common to need to replace some text in a file with some different text. This is common when renaming user account. After renaming a user account you need to change every instance of the name in the group file.
On this system my username is "lggodin". What if I change my username to "leogodin". With the commands we know we would have to use a combination of "/lggodin", "i", and "x" for each instance of my name in the group file. That would be cumbersome. Instead we can enter this command ":s/lggodin/leogodin/g".
The search and replace command works like this
:s/text to search for/text to replace with/" If we add g it will do a global search (replace all instances of the text found) and if we ad gc it will do a global search and ask us to confirm each change. with this simple command I can change every instance of "lggodin" to "leogodin" in the file.



Well that's it. You now now the basic Vim commands to be productive with Vim. You are not an expert but you can do production work in a production environment at a reasonable pace. In the next tutorial we will learn some more navigation using the "j", "k", "l", and ";" keys. We will also learn some basic environment setup commands and how to use the .vimrc file.
It is important to not only study Vim but to actually use vim. Without practice you will not get better. Use vim for all your text editing. Read tutorials like this one then go out and write your own tutorials. Learn new commands from the Vim documentation. If you come across any good tricks write a quick tutorial or screencast about it.

1 comment:
Hey Leo;
I wanted to drop you a quick note to thank you for your kind words about my story and to ask you if I could add you to my mailing list for when I get more of CHEMO published. Unfortunately, I didn't know of any way to contact you other than a comment =/ Sorry for cluttering this linux blog, feel free to delete the comment once you read it.
-John
Post a Comment