Take the 2-minute tour ×

I want to underline a word in Vim. How can I do this?

After closing the file and reopening it that word should still be underlined.

share|improve this question

migrated from stackoverflow.com May 23 '10 at 19:15

This question came from our site for professional and enthusiast programmers.

Vim isn't a word processor. The files it edits are plain text. –  Isaac Rabinovitch Sep 5 '12 at 0:11
You may find this answer helpful: superuser.com/a/827369/380406 –  BPS Oct 20 at 19:53
A similar question is answered here: superuser.com/a/827369/380406 –  BPS Oct 21 at 19:07

2 Answers 2

Bearing in mind that Vim is a (some might say the) text editor and not a word processor, there are two ways this can be addressed.

Updating the file

Assuming that you want to edit the text file to underline a heading or similar. You can do this by duplicating the line using yy]p (or :t .<CR>) and replacing the text with hyphens with a :substitute command like :s!\S!-!g.

Of course you will need to :write this file to preserve these.

Formatting the display

Alternatively, you can use the :match functionality to underline the text in your editor without touching the file:

:match Underlined /someword\|otherword/

You would need to add this to a startup script (such as vimrc) to persist this formatting.

See :help :match in Vim for more information.

share|improve this answer
it's ok .... after close and reopen the word is not underlined –  ungalnanban May 22 '10 at 7:30
You could add the match command to a filetype or syntax plugin. (See :help plugin in Vim). As a hack you could add it to your .vimrc (or _vimrc on Windows). –  Johnsyweb May 22 '10 at 7:39
The :match command given is limited to underlining specific words that you define in the match command. A more general purpose method might be to match words that are written between underscores. For example, any word or phrase written between two underscores would be underlined by the command: ":match /_.*_/" Again, you would have to put the command in .vimrc (or somewhere else) to have it always work in all your files. Also, the particular example given using underscores may not be perfect, but it illustrates the idea. –  Herbert Sitz May 23 '10 at 19:51
@Herbert: Good idea. A non-greedy match might be more useful, though. Something like :match /_[^_]\{-}_/ (untested) –  Johnsyweb May 24 '10 at 4:53

There is an article Underline using dashes automatically that describes one way to underline when writing plain text files.

share|improve this answer
It's underlining hole line. I don't want to underline hole line eg: this is sample line // underline sample only –  ungalnanban May 22 '10 at 7:24

Your Answer

