Vim Basics

Want to support HowtoForge? Become a subscriber!
 
Submitted by bad_crow (Contact Author) (Forums) on Thu, 2009-08-06 12:20. :: Linux

Vim Basics

This tutorial is going to speak about vim basic use. Vim is a powerful text editor used in CLI (command line interface). Because linux uses a lot of configuration files, you'll often need to edit them and vim is a great tool to do so.

Vim has a particular working method. Indeed there are to main mods : command and other mods.

Command mod lets you select the mod you want to enter to, save, quit, copy, paste and that kind of things but you can't edit the file in this mod.

There are several other mods. I'll only speak about those I often use.

Insert mod : lets you insert text in a document. Shotcurt : "i" (insert where the cursor is) or "o" (insert at the beginning of the following line).

Visual mod : permits to select the text like you would do with a mouse but using the keyboard instead of the mouse. Useful to copy several lines for example. Shotcurt : V

Let's now speak about the command mod.

A command begins with the symbol ":".

When you are in another mod you can use the escape key (sometimes you'll need to hit it twice) to come back to command mod at any time.

save : :w
save and exit : :wq
exit : :q
force : ! (example :w! :q!)
vertical split : open a document and then type :vsplit /path-to-document/document and this will open the specified document and split the screen so you can see both documents.
copy : y
copy a line : yy
paste : p
cut : d
cut a line : dd

I repeat these are very basic commands for vim, but they are very useful, and I hope this will help you configuring your Linux.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by hardly (registered user) on Tue, 2009-08-11 23:59.

Another handy tool in vim that I use often is the Replace function. While in navigation mode press the r key and then the key you want to replace the selected character with. This will replace one character at a time.

Capital R will stay in replace mode allowing you to type extended lengths of text over other text. ESC to exit. Using capital R will also allow you to backspace over text you have typed to reveal the original text. This will always impress onlookers. 

Just now after typing this comment I actually began to type :wq. lol

Submitted by Anonymous (not registered) on Thu, 2009-08-13 15:26.

Hi Hardly,

 >Using capital R will also allow you to backspace over text you have typed to reveal the original text. This will always impress onlookers. 

 

Could you please explain this a little bit more as I didn't catch it (maybe because not native English speaking)?

 Best regards, xcomm

Submitted by Allenace (not registered) on Fri, 2009-08-07 23:20.

Quit save and exit with
shift+zz

Submitted by Anonymous (not registered) on Thu, 2009-08-06 22:00.
This "tutorial" was not very helpful, mainly because it doesn't address one of the most non-intuitive and confusing aspects of vim: how to move the fricking cursor.
Submitted by Anonymous (not registered) on Sun, 2009-08-09 12:50.

Moving the cursor in vim is very easy (unlike in vi), simply use the arrow keys, whether in insert mode or not.

 The "Home" and "End" keys also work as expected (move to beginning and end of line).

The "Delete" key also works as expected (delete the caracter under the cursor).

 

One key I use also in non insert mode is the "a" key, it works like "i" to switch to insert mode, but inserts after current character, very useful for appending text at the end of a line.

Submitted by kswan (not registered) on Sat, 2009-08-08 03:40.

Actually, in vim the arrow keys work so that is very intuitive.  In vi the arrow keys normally don't work, but the vi navigation works in vim (j - Down, k - Up, h - Left, l - Right).

I thought this tutorial did cover the most important and unintuitive part of vim, namely how to exit.

The referral by another commenter to vimtutor is also beneficial.

Submitted by Angel (not registered) on Thu, 2009-08-06 18:56.

Excellent article for beginners like me, just want to add the following:

When you use the vsplit command and open two documents you can go between first and second file with Control+w+(rightarrow or leftarrow)

When you use the visual mod and you want to copy, cut or paste some text you can use the same commands from the command mod, you can also copy cut or paste text between files.

If you wish you can add this comments in the original post.

Thanks and sorry for the bad english.

Submitted by Adam (not registered) on Thu, 2009-08-06 13:53.

In your command prompt type vimtutor

Its the best vim tutorial to learn by doing.

-Adam

Submitted by nick (not registered) on Thu, 2009-08-06 12:23.

you can also use ':x' for save and exit. and 'x' to delete current character.