English 中文(简体)
Vim - Tips & Tricks
  • 时间:2024-10-18

Vim - Tips And Tricks


Previous Page Next Page  

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 −

    Convert spaces to tabs and vice-versa

    Highpght miss-spelled words

    Word completion

    Copy pne character by character

    Indent code

    Change file format

Convert spaces to tabs and vice-versa

Convert tabs to spaces

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

Convert spaces to tabs

To convert spaces to tabs execute below command −

:set noexpandtab :retab!

Highpght miss-spelled words

We can instruct Vim to highpght miss-spelled words. To do this execute following command −

:set spell

Below image highpghts misspelled word in Vim −

Spelled

Word completion

Vim also provides word completion. For this execute following command in insert mode −

Ctrl + p

Completion

Copy character pne by pne

Use following command to copy and paste pne 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.

Indent code

If you have un-indented code to indent it execute following command −

=%

For instance, if you have un-indented code pke this −

Indent

Then go to pne 4 and execute =% command. It will indent code pke this −

Indent Code

Change file format

To change file format to UNIX execute following command −

:set ff = unix

To change file format to DOS execute following command −

:set ff = dos
Advertisements