VI is my favorite editor in Unix operating system because it is powerful and ubiquitously available. If it is available, “vim” (which stands for vi improved) is a good alternative for original vi because it provides many enhanced functions. If you also want the convenience of using GUI and mouse, there is option as well “Gvim”
1. Use .vimrc to save your favorite settings
Once you found the best set of settings that you like, you can write to .vimrc in your home directory so that you can have the same settings every time you open up the vi window. Here is my .vimrc file.
1 2 3 4 5 6 7 8 9 10 |
syntax on set number set hlsearch set ruler set softtabstop=4 set shiftwidth=4 "set guifont=-Misc-Fixed-Bold-R-Normal--14-130-75-75-C-70-ISO8859-1 "set guifont=-Misc-Fixed-Bold-R-Normal--15-140-75-75-C-90-ISO8859-1 "set guifont=console8x16 set guifont=Misc\ Console\ 12 |
And here is how it looks in gvim window:
2. Search and Navigation
If you do quite a bit search in the text file such as a source code, you might like the tricks that I am going to mention.
Highlight the Search Term
If you look at the figure above careful enough, you would have noticed that all the “set” are highlighted with yellow boxes. That’s because I was searching “set” and all the terms are highlighted. You can enable it by type in the following line in command mode (or as I did, put it in the .vimrc).
1 |
:set hlsearch |
If you look at the figure above careful enough, you would have noticed that all the “set” are highlighted with yellow boxes. That’s because I was searching “set” and all the terms are highlighted. You can enable it by type in the following line in command mode (or as I did, put it in the .vimrc). To disable the highlight, you can use
1 |
:nohlsearch |
Search for whole matching word
You can use “SHIFT+8” to navigate all the words that matches the word in current cursor location. It is especially useful when I try to trace a variable in the file.
Find the matching bracket
You can use “%” to find the matching bracket: ([{}]) . When editing C source files, you can also jump at the beginning and end of C style comments /* */.
To reduce the time you need to press “%” key, you can also let the VI automatically briefly show the matching bracket as you type it.
1 2 |
set showmatch set matchtime=3 |
Navigating many source files
If you have many source files to navigate through, one thing you can do is to open up a file that contains a list of all the files. And then you can put your cursor to the file that you want to check and type “gf”. It will open the file. Once you are done, you can type “CTRL+O” to navigate back to the file list.
3. Editing the Text
Block Edit Mode
Move the cursor to one corner of the block that you want to edit and type “CTRL+v”, then you move the cursor around you will see the block marked. You can then type in “x” for cut, “y” for yank (copy), “c” for replace with a string or “r” for replace with a char.
Use VI as Hex Editor
You can even use VI as a hex editor. Use the following command to enter hex mode.
1 |
:%!xxd |
Here is the window you are going to see.
Well to be honest it is not using VI as hex editor rather pipe it to xxd. But you got the idea.
To exit the hex mode, you can use the following command
1 |
:%!xxd -r |
What’s your favorite tricks with VI?