Friday, January 15, 2010

7 More Commands Towards VI/Vim Excellence

In my first post "The 7 Command VI Tutorial" I showed how you can use VI with only 7 commands. If you have not read that tutorial you should before reading this one. With the initial 7 commands (i, x, :w, :q, /, n, esc) we learned how to perform basic editing on text files. This is a good start but we need a little more before we can become productive in VI. This tutorial will show us the next level of commands needed to reach basic productivity. Let's get started!

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. ":" is very valuable for anyone who expects to do any scripting on Linux.



$ 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.

Friday, August 31, 2007

The 7 Command VI Tutorial

Summary:
Seven commands is not enough to be productive in VI/VIM. However, seven commands will allow you to create and edit text files quickly. Learning too many commands early on can make it difficult to learn. This tutorial will get you started using VI or VIM and allow you to improve your skills moving forward. Check back because this tutorial will be complimented with more advanced tutorials.

Commands:
OK, lets begin. I know you don't believe that seven commands will allow you to use VI but you will be surprised. Without further ado, here are the commands:
i - Insert
x - Delete
:w - Write to file (or save if you will)
:q - Quit
/ - search
n - next
esc - Change mode

Modes:
The first thing you need to know is that VI has two modes, insert and command. Insert mode allows you to enter text as you are typing. Command mode allows you to run commands. At first it may be difficult to grasp this concept but it is incredibly efficient once you get used to it. To enter insert mode you press "i" to insert text before the cursor. To get back to command mode you press the "esc" key. When in doubt press the "esc" key and start everything from command mode.

1) Let's try it.
At a command prompt type in "vi test.txt". This will start vi with a blank file named test.txt.

2) Now type in "i". This will allow you to type. Type the following text exactly as it is spelled here. "Taking Cassandra to the End Of the World Party by Feer Before th march of Flames". If it doesn't seem to be working press esc and start over.


3) This text doesn't look too good does it? Press "esc" to get out of text mode. You'll notice that you can move around within the text using your arrow keys. Move your arrow key until your cursor is on the second e in the word Feer. If you are pressing arrow keys but the cursor isn't moving or you are seeing funny characters press "esc". Once you are on the second e of "Feer" press "x". This will delete the "e". Now press "i" to insert and type "a". The word should now spell "Fear" instead of "Feer". Press escape to get back to command mode and use the arrows, "x", and "i" to fix the rest of the sentence. It should now read "Taking Cassandra to the End Of the World Party by Fear Before the March of Flames"

4) We are almost finished. We have an incomplete word. The band name is "Fear Before the March of Flames. Not "Fear before th March of Flames". Use your arrow keys to place the cursor just after "th" and press i to insert. Now type "e" and press "esc".

5) Now that our text is the way we want it, let's save the file. From command mode type ":w". This will save the file.

6) OK, so we've created a text file, entered text, fixed text, and saved the file. Not bad for our first 10 minutes. What if we want to search for text? That is easy in VI. To search for text just enter a backslash followed by the text you want to search for. Let's search for the word "the". To do that enter "/the" and press enter.



To find the next iteration of the word "the" press "n".

7) So here we are. We are VI noobies who can effectively work with text. That's a great start. There is only one thing left to do. Let's quit VI. Type ":wq". This will save our file and exit VIM.

8) So what are you waiting for? Go listen to the song. It truly rocks. You have to hear it. What? You don't know where to listen to it? OK, I'll give you the link "http://www.last.fm/music/Fear+Before+the+March+of+Flames".


*** Important note. This blog is in no way affiliated with "Fear Before the March of Flames". I hope they will take this as free publicity (hopefully more than just my Mom will read this) and not sue me.