Sie sind auf Seite 1von 7

8/18/12

Vim: Beginners Tutorial GeekScribes

Vim: Beginners Tutorial [1]


September 12, 2010 by Inf Want to read our new posts as they are published? Subscribe to our feed [2] or by email [3] for instant updates. It's free! Vi is a text editor that is usually found under Unix, Linux and other open-source OS environments. Vi is one of the greatest text editors ever created by the human hand. Just for fun, go scream the quoted line to a crowd of Emacs fans and watch as all hell breaks loose! I do not vouch for your safety! But just like Emacs, Vi too is a great text editor. Each has its advantages, and disadvantages (Vi High learning curve, Emacs Pinky Syndrome). But once you have mastered them, they are really powerful editors. Choose one of them great editors (whichever you like), and master it. To help you get started with Vi, I have written this small tutorial, because most beginners are usually stopped in their tracks by their first encounter with Vi. Now, this article will deal with Vim, which is actually Vi Improved. In my opinion, it is a bit friendlier than the traditional Vi, while including all the greatness of Vi. So lets start, shall we?

Why Vim over Vi?


Vim has more features than Vi, such as code completion, syntax highlighting, folding and extended undo/redo facilities. So if you have to choose, give Vim preference. It just makes life easier. There are not many differences in terms of usability (both have a relatively high learning curve! ), and Vim has a Vi-compatibility mode if you want the old Vi flavors.

Obtaining Vim
www.geekscribes.net/blog/2010/09/12/vim-beginner-tutorial/ 1/7

8/18/12

Vim: Beginners Tutorial GeekScribes

Usually, if youre using Linux, you wont have anything to do. Vi would probably be installed by default. Nowadays, Vim is included too. If you dont have it, that is, typing vi or vim in a terminal doesnt give you anything except an error about package missing or something like that, then you can install it. The easiest way would be via your package manager, so open Synaptic, Apt or whatever you use to get packages, and install it. There are too many distributions out there for me to list instructions for all. Just check your distros docs on how to get software installed. It usually involves clicking on Package Manager or similar in the main menu, typing the root password and just searching and installing. After that, follow the rest of the tutorial. If you are using Windows, dont despair. Vim is also available for your OS, and its called gVim. Just download it from the provided link, install it and youre off. The difference is that instead of running in a terminal, gVim has its own GUI (hence, graphical Vim), though it comes with a console version if you want that.

Getting Started
Simple enough. Open a terminal on Linux. Then type: vim afilename (afilename: a path to a file that already exists, which you want to edit) or just vim which will create a blank session. On Windows, just open gVim. Double-click on its icon or whatever, just like you run a program on Windows. The icon should usually be on the desktop. Thats the place where instructions diverged. As from now, its the same. Heh, thats one thing at least thats similar on Windows and Linux. You will be presented with this screen if you just typed vim:

www.geekscribes.net/blog/2010/09/12/vim-beginner-tutorial/

2/7

8/18/12

Vim: Beginners Tutorial GeekScribes

Vim Main Window Thats your main Vim window. This is where all the typing takes place. If you wanted to edit a file and typed the first command, your files contents would be displayed here. For this lesson, lets just assume you want to create a new file, so you just typed vim at the terminal or opened gVim directly. So you see your main (blank) window. If you try typing, youll probably see nothing appear. This is kind of strange for new users, because they think that when they type, it should appear, and so Vim is not working. Dont worry, its perfectly normal. Its just that youre not in the right mode.

Modes
Vim has two main modes: insert mode and command mode. When you start Vim, youre in the latter command mode. This mode allows you to execute program-related commands, such as saving the file, or quitting the editor. Insert mode allows you to do typing-related operations. However, as youll soon see, there are some overlaps. You can do some modifying directly from command mode. But thats for later. When you just started Vim, youre in command mode. To go into insert mode, press the letter i key on your keyboard. Now you can start typing. To return to the command mode, press the Esc key. Now you know how to move between the two modes. Go to insert mode (itll indicate so in the status bar), then type a few letters, or maybe a sentence. Then, press
www.geekscribes.net/blog/2010/09/12/vim-beginner-tutorial/ 3/7

8/18/12

Vim: Beginners Tutorial GeekScribes

Esc to return to command mode.

Vim Insert Mode Vim commands are usually prefixed with a :, a semi-colon, for example :wq. So after typing a few letters, we wish to save the file. Well now use the command mode. Ensure you are not in Insert mode (there wont be INSERT written in the status bar). Type the command :w somefilename.txt, including the semi-colon but without the quotes. Then press enter. You can now find your file in the location where you started Vim, which is often your Home directory on Linux. Just go there and find your file.

www.geekscribes.net/blog/2010/09/12/vim-beginner-tutorial/

4/7

8/18/12

Vim: Beginners Tutorial GeekScribes

Vim Command Mode

Navigation
Vi was designed to greatly facilitate navigation through a body of text without having to resort to a mouse. Using Vi or Vim, your hands will rarely leave the keyboard when you need to move through a file, or execute a command; its all done from the keyboard itself. Navigation is usually done in command mode, so if you were inserting text, return to command mode by pressing escape. Okay, what will follow is weird, I know. Whenever I say, like, X-key, it means the letter X key on your keyboard. Lowercase X, unless otherwise mentioned. To move one character left, press the H-key. To move one character right, press the L-key. Up and down are K-key and J-key respectively. As you can see, all those keys are on the home-row of your keyboard, so navigation is done with minimum finger movements. You can also use the arrow keys to navigate while being in command mode. This implies moving the arm a bit, so its not as efficient as using H-L-K-J keys. To move one word left or right, press the B-key or W-key respectively. To move to the start or end of a line, press the ^-key (6-key on most keyb0ard) or the $-key (4-key on most keyboards).

Editing in Command Mode


You have to perform these commands while in command mode. So, to be on the safe side, just press Esc key. It will return you to command mode. Pressing the X-key will delete the character under the cursor. Pressing the C-key will change the character under the cursor, and drop into insert mode to allow you to type a new character. Pressing the D-key twice (dd) will delete the current line. Undo / Redo: Press U-key / Press Ctrl+R-key. (Note, Ctrl is often abbreviated as ^ in cheatsheets, so Redo would be denoted as ^r)
www.geekscribes.net/blog/2010/09/12/vim-beginner-tutorial/ 5/7

8/18/12

Vim: Beginners Tutorial GeekScribes

Copy-paste: Press the Y-key twice (yy) to copy current line. Press the P-key to paste the contents after the cursor. Note copy is called yank in Vi terminology.

Some Command Mode Commands


In command mode, you can use the following to perform file/editor related operations. Here are some commands to get you started. /wordtosearch (note, no : at the start), will allow you to search for wordtosearch. Press nkey for next occurrence, and N-key (Shift+N) for previous occurrence. Note that searching is case-sensitive. You can enable/disable case-sensitivity with :set ignorecase/noignorecase when you are in command mode. Search and replace is more complex, so Ill leave that out because it involves the concept of ranges. :w is used to write to file. It is similar to the Save command. It can be followed by a filename e.g. :w afile :wq will write to the file and exit the editor afterward. :wq! will forcefully write and exit, even if there might be problems. :q will quit the editor. :q! will forcefully quit the editor. Thats it for this tutorial. I hope it will help you get started in your journey to knowing how to use Vi and Vim. Vim is a very advanced editor with an enormous amount of features. What I have listed here is just a small fraction of what you can do with Vim. But since this is a beginners tutorial and since I am not a Vim guru myself, this is where Ill stop this tutorial. If you have any comments or tricks for Vim beginners, do leave a comment. Thank you for reading!

Other interesting posts:


1. http://www.geekscribes.net/blog/2010/09/12/vim-beginner-tutorial/ 2. http://feeds2.feedburner.com/Geekscribes 3. http://feedburner.google.com/fb/a/mailverify?uri=Geekscribes&loc=en_US

www.geekscribes.net/blog/2010/09/12/vim-beginner-tutorial/

6/7

8/18/12

Vim: Beginners Tutorial GeekScribes

www.geekscribes.net/blog/2010/09/12/vim-beginner-tutorial/

7/7

Das könnte Ihnen auch gefallen