1. Introduction
This tutorial has been written for both vi and vim. It starts with real basics, such as cursor navigation and ends with more advanced techniques like merging files.
For every section of this tutorial there is a short video with hints to help you understand how vim / vi works. Even that I have divided this tutorial into parts from novice to the expert user, there is plenty more what vim can do to make your work with vim editor easier and more efficient.
However completing this tutorial you will give sufficient knowledge about vim / vi and its features for your daily tasks.
2. Vim Novice level ( Vim Basics )
2.1. Moving cursor around
In vim you can move cursor around with following keys h, l, k, j which is left, right, up and down respectively.
You can move cursor around also with arrow keys, however this is possible only if they are available.
Vim was designed for all kinds of terminals where arrow keys may not be available for you. Moreover, once you get used to using vim with h, l, k, j you will move more quickly than using arrow keys.
Open some text file and try use above keys now:
vim yourfile.txt
NOTE: You do not have to create a file prior to execution of the above command. If the file does not exists it will be created. If the file does exists it will be opened.
Vim Novice level summary:At this point of this vim tutorial you have learned:
Moving around with cursor:
h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN |
2.2. Exiting from vim
At the moment, you know how to open a file with vim and how to navigate vim cursor around. What you now need to know is how to exit from vim editor without saving changes. Vim editor has two modes:
- command mode
- editing mode
Command mode is the one where we can instruct vim editor to exit to the command line ( shell ). To do that we need press ESC and type :q!. Play video and watch bottom of the video screen for entered commands.
|
2.3. Character Deletion
In this section you will learn how to delete characters in a vim command mode. To delete character in vim, you need to position cursor on the character you are intent to delete and press x. Remember you need to be in a command mode! If unsure press ESC key.
Vim Novice level summary:At this point of this vim tutorial you have learned:
|
2.4. Inserting Text
Well, you have already learned couple things about vim! Now its time to insert some text. In order to insert some text you need to switch vim into an editing mode. To get into editing mode press, i when in a vim command mode. -- INSERT -- key word at the bottom will indicate that you are in an inserting mode. See video for demonstration. When you become familiar with i ( insert mode ) use instead i letter a ( append ) and see what it does !
Vim Novice level summary:At this point, of this vim tutorial you have learned:
|
2.5. Saving edited file
Now you know, have to move cursor, delete characters insert text and exit file without changes with :q! vim command. Exiting vim editor without changes does not always produce desired outcome. To save changes for file you are editing use :wq command in vim command mode. There is also quicker way to do it, which is by key strokes SHIFT+zz . Watch video for demonstration of both :wq and SHIFT+zz commands. Remember you need to be in vim's command mode to execute those commands !
Vim Novice level summary:At this point, of this vim tutorial you have learned:
|
2.6. VIM novice level Summary
|
|
3. Vim Operators and Motions
3.1. Deleting Words
Let's see if we can also make vim delete words instead of deleting characters one by one! Vim has for deleting words very intuitive command dw which means I guess Delete Word. Let's tryto delete some words. In order to do so navigate cursor at the beginning of the word you want to delete and enter command dw in a command mode.
Vim Operators and Motions summary:At this point of this vim tutorial you have learned:
|
3.2. Delete to the end of the line
In fact, the vim editor in the previous example did delete all characters from your cursor to the first space. That is the reason that you need to navigate to the beginning of the word you want to delete. With command d$ you can instruct vim to delete all characters to the end of the line. Place you cursor anywhere in the text and enter d$ command. Remember you need to bein a vim command mode.
From now onwards we will refer to d as an operator and to w or $ as a motion. There are many other operators and motions, for example you may try what d operator with e motion does.
Vim Operators and Motions summary:At this point of this vim tutorial you have learned:
|
3.3. Motions and count number
When in a command mode and entering motion key e your cursor will be placed to the end of the word. When entering motion key w your cursor will be placed at the beginning of the following word. To do some magic trick you can use count number before motion key, which will multiply your action. For example, to jump to the end of the 8th word you will enter count 8 and motion e thus: 8e . Try it and also try to play with w motion and count number. If you want to navigate to the end of the line it would be silly to count words and then enter count and motion key-strokes. To navigate to the end of the line use $ motion and to navigate vim to the beginning of the line enter 0 motion.
Vim Operators and Motions summary:At this point, of this vim tutorial you have learned:
|
3.4. Deleting multiple words
No you have seen how to jump over multiple words by using vim's motions with combination of counts. Let's combine this acquired knowledge with operator like d ( delete ). For example, to delete 4 words we can use command d4w. It is very easy to remember this just take first characters from words "Delete 4 Words" and you get your command d4w.
Vim Operators and Motions summary:At this point, of this vim tutorial you have learned:
|
3.5. Deleting lines
Vim is also capable to delete lines. To delete single line you can use dd command. If your intention is to delete multiple lines add count number in front of the actual command. For example, to delete 100 you can use command 100dd. Remember that the first line which will be deleted, is the one with your cursor on it.!
Vim Operators and Motions summary:At this point, of this vim tutorial you have learned:
|
3.6. Vim undo command
You have already learned how to delete characters, words as well as whole lines. With all this processing power you are very likely to make a mistake. Let's say you wanted to delete just 2 lines not 4. Simple help comes in form of vim's undo command u. Once you have made a mistake go to command mode and press u to undo your previous changes.
Vim Operators and Motions summary:At this point of this vim tutorial you have learned:
|
3.7. VIM Operators and Motions summary
|
|
4. Vim apprentice user level
4.1. Paste command
Congratulations, you have reached apprentice user level in using vim text editor! Let's continue this vim tutorial with paste command. When deleting lines, words or characters with d command, vim actually saves them into cache memory for later use. Having said that, you can also think of d command as cut. To use your cache memory in this concept you can use p ( paste/put)command. Try delete line with dd command, move you cursor where you intent to insert this and hit key p to paste it.
Vim apprentice user summary:At this point of this vim tutorial you have learned:
|
4.2. VIM replace operator
Replacing characters with vim editor is a very easy task. Simply get vim to the editing mode, remove character and insert replacement character. Well, this approach is logical but, there is a way to do it faster with r ( replace ) operator. Move you cursor on the character you would like to replace first enter r command followed by character which should be there instead.rt command will replace current character with t.
Vim apprentice user summary:At this point of this vim tutorial you have learned:
|
4.3. VIM change operator
Vim's replace operator allows us to replace single o multiple characters with a single character type. To multiple characters you can use a change operator. If you would like to change a single word you can use command ce.
Vim apprentice user summary:At this point of this vim tutorial you have learned:
|
4.4. VIM apprentice user summary
|
|
5. Vim Experienced user level
5.1. VIM advanced navigation
In many cases you will deal with large files which can contain thousands of lines. To navigate vim cursor to line 3500 with j key will take you whole morning including your lunch break. Fortunately vim developers equipped vim with g an operator. This is how you use it. To get to the end of the file hit G ( SHIFT+g ), to get to the beginning of the file use gg command, to navigate you cursor on n line use nG. If you are unsure how many lines your file has, you can use a CTRL+g key combination.
Vim experienced user summary:At this point of this vim tutorial you have learned:
|
5.2. Searching text with vim
Navigating with vim's g operator is easy, but what if you do not know what line you need to edit? In this case vim allows you to search for a given keyword or phrase. You can use / or ? characters to search for keywords or phrases. /edit will search for a key word edit in forward direction. In case you use ?edit vim will search for key word edit in backward direction. You text may include more keywords, to search all of them keep pressing n key ( next ) or for backward search use N ( SHIFT+n )key.
Vim experienced user summary:At this point, of this vim tutorial you have learned:
|
5.3. Vim Substitution
Vim can replace given a pattern keyword throughout whole text. Vim also gives you an option to replace your pattern within certain range. This process is called substitution. To replacefirst occurrence of keyword bash with perl in your document you can use command :s/bash/perl/. If there is a more then one keyword of bash on the same line you can use command :s/bash/perl/g. This command can be used to replace keyword bash with perl in whole document: :%s/bash/perl/g . There may be a situation where you need to substitute keyword in certain line range only. In this case you replace % with line range. :20,40s/bash/perl/g this will substitute bash for perl only between lines 20 and 40.
Vim experienced user summary:At this point, of this vim tutorial you have learned:
|
5.4. VIM experienced user summary
|
|
6. Vim Veteran user level
6.1. Execute external commands on shell
Vim editor also allows you to execute external commands while editing files. : followed by the ! allows you to do that trick. :!date will execute date command on the shell.
Vim Veteran user summary:At this point, of this vim tutorial you have learned:
|
6.2. More options to write a file
For some reason, you may need to save your current progress on editing file to some different file, perhaps as a backup, or you just want to save current progress without quitting a vimeditor. :w command is used to save file without quitting vim. If you would like to save your current progress to some different file, for example, temp.txt you will do it with command :w temp.txt.
Vim Veteran user summary:At this point of, this vim tutorial you have learned:
|
6.3. Save selected text to the file
Let's say that you are in the situation where you have no mouse and no other windows available, and you need to selected text from currently opened file and save it to another file. This situation often happens with remote ssh / telnet logins. In this case you would use a v operator and highlight text with navigation keys, followed by :w to save selected text to different file.
Vim Veteran user summary:At this point, of this vim tutorial you have learned:
|
6.4. Retrieve content from file
Vim also allows you to append / merge content of one file into your currently opened file. For this you can use a r operator. :r file.txt will retrieve content of the file.txt and insert it to your currently opened file. The place where the text will be inserted depends on you cursor position.
Vim Veteran user summary:At this point, of this vim tutorial you have learned:
|
6.5. VIM veteran user summary
|
|
7. Vim Expert user level
7.1. Using open operator
Switching vim into an editing mode with i (insert) or a (append) operator is easy. However, sometimes you may find it easier to use an o operator. o operator inserts new line below yourcursor and switch vim into the editing mode. O (SHIFT+o) operator inserts new line above your cursor and switch vim into editing mode.
Vim Veteran user summary:At this point, of this vim tutorial you have learned:
|
7.2. Copy and Paste
Previously, we have seen how we can use d ( delete ) operator to delete line and paste it to another location within file. This time we will do the same but instead of deleting line, wewill copy it and paste it. The principle is the same as with a d operator but in this case we will use y ( yank ) operator.
Vim Veteran user summary:At this point, of this vim tutorial you have learned:
|
7.3. Customize vim's environment
Vim is extended version of its older brother vi and this part of the tutorial applies only to vim. Every time you start up vim it reads a .vimrc file from you home directory. In .vimrc file you can set up a background, font color and many other different settings which are beyond this tutorial.
Vim Veteran user summary:At this point, of this vim tutorial you have learned:
Congratulations ! You are now expert in using vim text editor and your expert level feedback on this vim tutorial is much appreciated: lubos(at)linuxconfig.org |
8. Vim Tutorial Summary
8.1. VIM novice level Summary
|
|
8.2. VIM Operators and Motions summary
|
|
8.3. VIM apprentice user summary
|
|
8.4. VIM experienced user summary
|
|
8.5. VIM veteran user summary
|
|
8.6. VIM expert user summary
|
No comments:
Post a Comment