Vim, Vigor & Vinegar
by ms_meme
Are you already a Vim user? Then this tutorial is not for you. Are you serious about learning Vim so you can do every bit of your work with Vim? Then pass up this article and head straight for an internet search. You will find plenty of sites to help and delight you.
But maybe you thought about trying Vim and read that it is really hard and that the learning curve is steep. Maybe you looked at some of the onĀline tutorials and immediately clicked the back arrow. Maybe you opened up Vim and thought, "I don't have enough vim and vigor for all this." If so, just maybe this article is for you. I did all three of the above. Then I decided that I was going to give Vim a try. This really isn't a tutorial. It is just a chat about my experiences, the sweet and the sour, with Vim. I will attempt to give you a little taste of Vim without your having to read pages and pages of tutorials, as if you are going to use Vim day and night.
You don't know what Vim is? It is a little text editor full of verve, vigor, vitality, vivaciousness, vivacity and a dash of zip. Of course you don't have to put up with all of that in order to try it. Vim may already be loaded into your programs. More Applications/Editors/Vi editor. Vi is the original application. Vim is Vi IMproved. If it is not there, look for it in Synaptic.
One of my New Year's resolutions was to spend more time in the terminal. So if you try following along with me, everything I do with be done there. Vim has a GUI that can also be used. The power of Vim is using the keyboard to do all the commands, instead of the mouse. This could save time or just be fun. It has lots and lots and lots of commands. I get lost with most of them, but you can still learn something about Vim with just a few. Open up a terminal. You will want to make a new directory/folder called vim. This will be where you will keep your saved files for this Vim practice. mkdir is the command to make that directory. To change to the vim directory, type cd vim.
In the illustration below, I typed ls after changing to the vim directory. ls is the command for a simple listing of what is in the directory. I like to practice those terminal commands to be true to my New Year's resolution. All is well, as nothing is listed in the vim directory.
Now that we are in the vim directory, we are ready to make a file. Type vim <filename> then press enter. You may name it anything, but it is best to use no spaces in the name. The name I have chosen is myfile.
The vim text editor will now open. You will see a page with a row of tildes ~ to the left. At the bottom, you will see the name of your file showing it is a new file. Vim has several modes. The default mode is NORMAL mode. It is here that the keyboard commands are given. Instead of taking your hands off the keyboard and moving them back and forth to the mouse, you will type a combination of keys to move around or edit your document. You will not be able to enter text to your document in NORMAL mode.
In order to type/edit text, you must be in INSERT mode. There are several ways to get to INSERT mode. One way is to press the i key. After you do this, you will see the word INSERT at the bottom left of the screen.
You can now type/insert whatever text you want.
You have now made a text file, but it is not yet saved. To save a file without closing, you will have to return to NORMAL mode where the commands are given. To return to NORMAL mode press the Esc key. You will notice that the INSERT disappears from the bottom of the screen and you will no longer be able to enter text.One of the things I found a bit hard was remembering if I am in INSERT or NORMAL mode. Of course, it tells you right at the bottom. I think it is more nervousness than forgetfulness. Your hands get jittery as you try to get into the Vim mood. I thought it was a bit awkward to remove my left hand from the keyboard position to tap the Esc key to get to NORMAL. You have to do this quite often. But then removing your right hand from the keyboard to get to the mouse is about eight times the distance. I can justify working in Vim right now with that ratio. To save a file without closing, type : in the NORMAL mode. You will see it appear at the bottom of the screen. Next, type w as the command to write to disk.
After you press Enter, you will see your file has now been written/saved.
Since you saved a file but did not close it, you probably want to do some more work with it. And that means returning to INSERT mode. As shown above, the letter i can be used to accomplish this. But before typing i, I want you to see what happens when I do it. I have pressed i and my cursor is on the letter/space that I last typed.
You can see below when I entered a T for my next text, the T was placed before the letter/space that the cursor is on. I don't want it there. This is because the command i inserts text before the cursor. If this happens, you may not know a command to get it in the right place.
In order to get my next sentence started in the right place, I can use another INSERT command. I returned to the NORMAL mode by pressing Esc. This time, I typed a to append text. The a command will also bring you to INSERT mode. a inserts text after the cursor. I can then continue to type. There are other ways to get your text inserted where you want it, but this just shows you the difference between i and a.
I have now added more text.
Here are some basic commands to try before saving and exiting Vim. You might want to type several lines of text before trying them. Remember you must press Esc to be in Normal mode before using the commands.
h moves cursor left j moves cursor down k moves cursor above l moves cursor right i inserts text before cursor I inserts text at beginning of a line a inserts text after cursor A inserts text at the end of a line x deletes a letter when cursor is on top of it dw deletes an entire word when cursor is placed on first letter of word d$ deletes to the end of a line from where you placed cursor. u undoes a command 0 moves cursor to beginning of a line $ moves cursor to the end of the line
While working on your file, you will want to save often using the command :w. When you are finished, you can save and quit the Vim program by typing :wq in Normal mode.
Since we began in the vim directory, the file will be saved there. Of course, you can save it to another directory, but that is another lesson. I have saved and quit using :wq. This returns me to the terminal. I am still in the vim directory. This time when I type ls, I see that myfile is in the list and all is well. I can then exit the terminal or cd to other places. If I want to work again on the file, I will cd to the vim directory and type vim <filename>. Of course I can access 'myfile' from anywhere using paths. That is another lesson.
Perhaps, while working on a file, you have made changes but decide to quit without saving these changes. :q That command may not always work. Try :q! The ! tells Vim that you mean it. I hope you have learned a little about Vim. There is a Vim tutorial with the Vim install. In a terminal, type vimtutor. It says it is a 30 minute tutorial. I have been working on it for many months and am only half way through. I think it depends how much practice you want to do with each lesson. I like to be sure I can perform each new command with ease before adding another.
My article is entitled Vim, Vigor and Vinegar. I think you can understand that Vim is a very vigorous program. But what about the vinegary part? I found Vim to be like a pickle. It can be a bit sour, but I am always wanting another bite.