4

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.

3

2 Answers 2

6

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.

5
  • it's ok .... after close and reopen the word is not underlined
    – ungalnanban
    May 22, 2010 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, 2010 at 7:39
  • 2
    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. May 23, 2010 at 19:51
  • @Herbert: Good idea. A non-greedy match might be more useful, though. Something like :match /_[^_]\{-}_/ (untested)
    – johnsyweb
    May 24, 2010 at 4:53
  • 1
    You also can use the diagraph: insert mode: <C-k>'- result: Though still using two lines. Jan 19, 2017 at 12:58
2

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

2
  • 1
    It's underlining hole line. I don't want to underline hole line eg: this is sample line // underline sample only
    – ungalnanban
    May 22, 2010 at 7:24
  • You also can use the diagraph: insert mode: <C-k>'- result: Though still using two lines. Jan 19, 2017 at 12:58

You must log in to answer this question.