Now we got fair idea about Vim. Let us discuss few tips and tricks of Vim to improve productivity. In this section, we will discuss following items −
If you are editing a file and you want to convert entered tab character to spaces then execute following command −
:set expandtab
Note that, this command will not change existing tabs to spaces. To achieve this execute following command −
:set tabstop = 4 shiftwidth = 4 expandtab :retab
In above command we are instructing Vim to convert tabs into 4 spaces
To convert spaces to tabs execute below command −
:set noexpandtab :retab!
We can instruct Vim to highlight miss-spelled words. To do this execute following command −
:set spell
Below image highlights misspelled word in Vim −
Vim also provides word completion. For this execute following command in insert mode −
Ctrl + p
Use following command to copy and paste line character by character −
Sr.No | Command & Description |
---|---|
1 | Ctrl + y
Copy and paste text located above the cursor |
2 | Ctrl + e
Copy and paste text located below the cursor |
Note the we have to use these commands in insert mode.
If you have un-indented code to indent it execute following command −
=%
For instance, if you have un-indented code like this −
Then go to line 4 and execute =% command. It will indent code like this −
To change file format to UNIX execute following command −
:set ff = unix
To change file format to DOS execute following command −
:set ff = dos