Vim Tutorial
|
From Linuxconfig.org
|
|
|
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 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.
|
|
Character Deletion
|
In this section you will learn how to delete characters in vim command mode. To delete character in vim you need to position cursor on the character you intent to delete and press x. Remember you need to be in command mode! If unsure press ESC key.
|
|
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 editing mode. To get into editing mode press i when in vim command mode. -- INSERT -- key word at the bottom will indicate that you are in 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 !
|
|
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
|
|
Vim Operators and Motions
Deleting Words
|
Lets see if we can also make vim delete words instead of deleting characters one by one! Vim have for deleting words very intuitive command dw which means I guess Delete Word. Lets try 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 command mode.
|
|
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 be in vim command mode. From now onwards we will refer to d as operator and to w or $ as motions. There are many other operators and motions for example you may try what d operator with e motion does.
|
|
Motions and count number
|
When in command mode and entering motion key e your cursor will be placed end 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:
|
|
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 "Delete 4 Words" and you get your command d4w. Vim Operators and Motions summary:
|
|
Deleting lines
|
Vim is also capable to delete lines. To delete single line you can use dd command. If your intention is to deleto 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:
|
|
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. Lets 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:
|
|
VIM Operators and Motions summary
|
|
Vim apprentice user level
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:
|
|
VIM replace operator
|
Replacing characters with vim editor is 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:
|
|
VIM change operator
|
Vim's replace operator allows us to replace single o multiple characters with single character type. To multiple characters you can use change operator. If you would like to change single word you can use command ce. Vim apprentice user summary:
|
|
VIM apprentice user summary
|
|
Vim Experienced user level
VIM advanced navigation
|
In many cases you will deal with large files which can have 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 operator. Here 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 issue command CTRL+g.
|
|
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 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 bof backward search use N ( SHIFT+n )key.
|
|
Vim Substitution
|
Vim is able to replace given pattern / keyword throughout whole text. Vim also gives you an option to replace your pattern within certain range. This process is called substitution. To replace first 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:
|
|
VIM experienced user summary
|
|
Vim Veteran user level
Execute external commands on shell
|
Vim editor also allows you to execute external commands while editing files. : followed by the ! allows you to to that trick. :/date will execute date command on the shell. Vim Veteran user summary:
|
|
More options to write a file
|
From 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 quiting a vim editor. :w command is used to save file without quiting 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:
|
|
Save selected text to the file
|
Lets say that you are in the situation where you have no mouse and not other windows available and you need to selected text from currently opened file and save it to other file. This situation often happens with remote ssh / telnet logins. In this case you would use v operator and highlight text with navigation keys, followed by :w <yourfile> to save selected text to different file. Vim Veteran user summary:
|
|
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 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:
|
|
VIM veteran user summary
|
|
Vim Expert user level
Using open operator
|
Switching vim into editing mode with i (insert) or a (append) operator is easy. However, sometimes you may find it easier to use o operator. o operator inserts new line below your cursor 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:
|
|
Copy and Paste
|
Previously we have seen how we can use d ( delete ) operator to delete line and paste it to other location within file. This time we will do the same but instead of deleting line we will copy it and paste it. The principle is the same as with d operator but in this case we will use y ( yank ) operator. Vim Veteran user summary:
|
|
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 read .vimrc file from you home directory. In .vimrc file you can set up background, font color and many other different settings which are beyond this tutorial.
|
|
Vim Tutorial Summary
VIM novice level Summary
|
|
VIM Operators and Motions summary
|
|
VIM apprentice user summary
|
|
VIM experienced user summary
|
|
VIM veteran user summary
|
|
VIM expert user summary
|
|














