Vi is the legacy linux text editor for very long time. Although we have many competitive text editor such as Pico or Nano but Vi still is classic text editor that very popular and widely use in these days.
Many users don't like Vi because it is hard to use. Actually although Vi has many commands but in facts we only use not more than 10 commands to working with Vi. So this article will be tell you about only important command from my experience to operate with Vi.
Vi and Vim
Many people don't know that Vi has 2 version. Vi that is original version and Vim that is enhanced and easier version. So in some system such as Debian or FreeBSD we have to manually install Vim with this command
apt-get install vim
Before we starting to use Vi we have to understand mode of Vi that we have 2 modes that we always use.
- Command Mode is the mode that we send command to do operation such as save or search.
- Entry Mode is the text editor mode.
This is basic Vi operation
- Put the command vi to enter to the Vi program or vi filename to edit filename.
- Vi we go to Command Mode when first entering program. We can use keyboard cursor keys to move the cursor to position that you want.
- When we want to edit text, use i to enter Entry Mode. You can see that Vi enter Entry Mode by seeing the bottom left that will have -- INSERT --.
- If you want to exit from Entry Mode, press Esc to entering Command Mode. You can see that Vi entering Command Mode by -- INSERT -- at the bottom left will disappear.
- Enter command :w and Enter in Command Mode to save file. You can see the command :w in the bottom left.
- Enter :q and Enter in Command Mode to exit from Vi program.
- If you want to save and exit, use :wq in Command Mode.
- Sometimes Vi will warning when you save or exit Vi eg. read only file or you still didn't save file before exit. You can append ! to the command to force ie. :w! :q! หรือ :wq!.
This is only that you want to know all about Vi and you can use Vi now. The rest will be the tips that you can use Vi with efficiency :)
Syntax highlight on Vi
We can do syntax highlight on Vi by create or edit ~/.vimrc file and put this line on
You can test syntax highlight instantly by edit source file ie. .c, .cpp, .php and you will change your cheap black & white monitor to RGB monitor :D
Some advance use of Vi
How to move the cursor in Command Mode
- G to move the cursor to the last line
- 1G to move the cursor to the first line
Actually there are many command to entering Entry Mode with various purpose
- i to insert text into the cursor is
- a to insert text after the cursor is
- o to insert text to the next line
Delete text in Command Mode
- dd to delete the whole line
- d after with number and press Enter ie. d10 and Enter will be delete 10 lines after cursor
Search and replace
- Put command /word and Enter to find word
- Press n to move cursor to the next word
- Press N to move cursor to the previous word
- :s/pattern/string/g and Enter to replace pattern with string in whole file ie. :s/wrong/right/g will replace wrong with word right in whole file