Beruflich Dokumente
Kultur Dokumente
Obtaining Vim
www.geekscribes.net/blog/2010/09/12/vim-beginner-tutorial/ 1/7
8/18/12
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 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 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
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).
8/18/12
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.
www.geekscribes.net/blog/2010/09/12/vim-beginner-tutorial/
6/7
8/18/12
www.geekscribes.net/blog/2010/09/12/vim-beginner-tutorial/
7/7