The script uses <tt>matchadd()</tt> which requires [[Where to download Vim|Vim 7.2]] (actually version 7.1.040 or later). You will probably need gvim to display the colors specified in the script. In addition, the script assumes you have a numeric keypad and a system that allows you to map keys on that keypad (for example, the command <tt>:map&nbsp;<k0>&nbsp;:echo&nbsp;'Hello'<CR></tt> would display ''Hello'' when you press <tt>0</tt> on the numeric keypad).

+

The script uses <code>matchadd()</code> which requires [[Where to download Vim|Vim 7.2]] (actually version 7.1.040 or later). You will probably need gvim to display the colors specified in the script. In addition, the script assumes you have a numeric keypad and a system that allows you to map keys on that keypad (for example, the command <code>:map&nbsp;<k0>&nbsp;:echo&nbsp;'Hello'<CR></code> would display ''Hello'' when you press <code>0</code> on the numeric keypad).

−

You need the script (<tt>highlights.vim</tt>) and the file that defines the highlight groups (<tt>highlights.csv</tt>); these are given below.

+

You need the script (<code>highlights.vim</code>) and the file that defines the highlight groups (<code>highlights.csv</code>); these are given below.

The script defines highlight groups named <tt>hl1</tt>, <tt>hl2</tt>, ... <tt>hl9</tt> (and more). Once enabled, pressing one of the keys <tt>1</tt>, <tt>2</tt>, ... <tt>9</tt> on the numeric keypad will highlight the word under the cursor with the colors defined in the corresponding highlight group (for example, press <tt>4</tt> on the keypad to highlight the current word with the <tt>hl4</tt> group).

+

The script defines highlight groups named <code>hl1</code>, <code>hl2</code>, ... <code>hl9</code> (and more). Once enabled, pressing one of the keys <code>1</code>, <code>2</code>, ... <code>9</code> on the numeric keypad will highlight the word under the cursor with the colors defined in the corresponding highlight group (for example, press <code>4</code> on the keypad to highlight the current word with the <code>hl4</code> group).

−

Type <tt>\m</tt> (assuming the default backslash leader key) to toggle mapping of the keypad on/off. At startup, the numeric keypad operates normally because only <tt>\m</tt> is mapped.

+

Type <code>\m</code> (assuming the default backslash leader key) to toggle mapping of the keypad on/off. At startup, the numeric keypad operates normally because only <code>\m</code> is mapped.

−

After typing <tt>\m</tt> to enable the mappings, you could use the keypad to:

+

After typing <code>\m</code> to enable the mappings, you could use the keypad to:

−

*Press <tt>1</tt> to highlight the current visually selected text, or the current word (if nothing is selected). Highlight group <tt>hl1</tt> is used.

+

*Press <code>1</code> to highlight the current visually selected text, or the current word (if nothing is selected). Highlight group <code>hl1</code> is used.

*Press <tt>0</tt> to remove all highlights from the current visually selected text, or the current word.

+

*Press <code>0</code> to remove all highlights from the current visually selected text, or the current word.

−

For example, if you place the cursor on the word "rain" and press <tt>4</tt> on the keypad, all occurrences of "rain" will be highlighted (use <tt>:hi hl4</tt> to show the colors). No highlighting will occur in words like "rainbow".

+

For example, if you place the cursor on the word "rain" and press <code>4</code> on the keypad, all occurrences of "rain" will be highlighted (use <code>:hi hl4</code> to show the colors). No highlighting will occur in words like "rainbow".

−

Alternatively, place the cursor on "rain" and press <tt>viw</tt> to select the word, then <tt>4</tt>. All occurrences of the visually selected text will be highlighted, including the "rain" in "rainbow".

+

Alternatively, place the cursor on "rain" and press <code>viw</code> to select the word, then <code>4</code>. All occurrences of the visually selected text will be highlighted, including the "rain" in "rainbow".

−

Each window has its own set of highlights: if you use <tt>:split</tt> to show two windows, you can have one set of highlights in the top window, and a different set in the other window.

+

Each window has its own set of highlights: if you use <code>:split</code> to show two windows, you can have one set of highlights in the top window, and a different set in the other window.

With the mappings enabled, you could use the keypad to:

With the mappings enabled, you could use the keypad to:

−

*Press <tt>-</tt> to remove all highlights in the current window.

+

*Press <code>-</code> to remove all highlights in the current window.

−

*Press <tt>+</tt> to restore the highlighting when <tt>-</tt> was last used in the current window.

+

*Press <code>+</code> to restore the highlighting when <code>-</code> was last used in the current window.

−

*Press <tt>*</tt> to restore the highlighting when <tt>-</tt> was last used in any window.

+

*Press <code>*</code> to restore the highlighting when <code>-</code> was last used in any window.

For example, suppose you are displaying two windows and you want the same highlighting in each window. In the first window, apply the wanted highlighting. Then (using the numeric keypad keys):

For example, suppose you are displaying two windows and you want the same highlighting in each window. In the first window, apply the wanted highlighting. Then (using the numeric keypad keys):

−

*Press <tt>-</tt> to remove all highlights in the first window.

+

*Press <code>-</code> to remove all highlights in the first window.

−

*Press <tt>+</tt> to restore them.

+

*Press <code>+</code> to restore them.

−

*Switch to the other window and press <tt>*</tt> to apply the highlights from the first window.

+

*Switch to the other window and press <code>*</code> to apply the highlights from the first window.

===Searching===

===Searching===

−

After using <tt>\m</tt> to enable mappings, you can search for the next or previous occurrence of highlighted text. Patterns highlighted with the <tt>:match</tt> or <tt>:2match</tt> commands are also found.

+

After using <code>\m</code> to enable mappings, you can search for the next or previous occurrence of highlighted text. Patterns highlighted with the <code>:match</code> or <code>:2match</code> commands are also found.

−

Type <tt>\f</tt> to find the next match, or <tt>\F</tt> to find backwards. This has no effect on search highlighting or history.

+

Type <code>\f</code> to find the next match, or <code>\F</code> to find backwards. This has no effect on search highlighting or history.

−

Alternatively, type <tt>\n</tt> or <tt>\N</tt> to search forwards or backwards. Now you can press the normal search keys <tt>n</tt> or <tt>N</tt> to find the next or previous occurrence.

+

Alternatively, type <code>\n</code> or <code>\N</code> to search forwards or backwards. Now you can press the normal search keys <code>n</code> or <code>N</code> to find the next or previous occurrence.

===Highlight command===

===Highlight command===

−

The script defines a <tt>:Highlight ''n'' ''pattern''</tt> command where ''n'' is a number (1..99) and ''pattern'' is the text you want highlighted. Depending on your system, you may be able to use the abbreviation <tt>:Hi</tt> rather than <tt>:Highlight</tt>.

+

The script defines a <code>:Highlight ''n'' ''pattern''</code> command where ''n'' is a number (1..99) and ''pattern'' is the text you want highlighted. Depending on your system, you may be able to use the abbreviation <code>:Hi</code> rather than <code>:Highlight</code>.

The following examples use different colors to highlight various patterns:

The following examples use different colors to highlight various patterns:

Line 63:

Line 63:

</pre>

</pre>

−

The second example is the same as placing the cursor on "hello" then pressing <tt>5</tt> on the numeric keypad. The last example highlights <tt>th</tt> occurring at the beginning of a word, not case sensitive.

+

The second example is the same as placing the cursor on "hello" then pressing <code>5</code> on the numeric keypad. The last example highlights <code>th</code> occurring at the beginning of a word, not case sensitive.

The following command will list all active highlights (group names and patterns):

The following command will list all active highlights (group names and patterns):

Line 71:

Line 71:

===Other commands===

===Other commands===

−

After using <tt>\m</tt> to enable the keypad mappings, you can press 1..9 on the keypad to highlight the visual selection (if any), or the current word. For example, pressing <tt>4</tt> on the keypad will highlight using the <tt>hl4</tt> group.

+

After using <code>\m</code> to enable the keypad mappings, you can press 1..9 on the keypad to highlight the visual selection (if any), or the current word. For example, pressing <code>4</code> on the keypad will highlight using the <code>hl4</code> group.

−

In addition, you can press a digit 1..9 on the main keyboard before pressing 1..9 on the keypad. For example, if you press <tt>2</tt> on the main keyboard before <tt>4</tt> on the keypad, the <tt>hl24</tt> highlight group will be used.

+

In addition, you can press a digit 1..9 on the main keyboard before pressing 1..9 on the keypad. For example, if you press <code>2</code> on the main keyboard before <code>4</code> on the keypad, the <code>hl24</code> highlight group will be used.

−

The <tt>:Hclear</tt> command can clear highlights. After typing <tt>:Hclear</tt>, you can enter an argument, or you can press Space then Tab for command completion (highlighted patterns). Examples:

+

The <code>:Hclear</code> command can clear highlights. After typing <code>:Hclear</code>, you can enter an argument, or you can press Space then Tab for command completion (highlighted patterns). Examples:

<pre>

<pre>

" Remove highlight for visual selection (if any), or current word.

" Remove highlight for visual selection (if any), or current word.

Line 98:

Line 98:

</pre>

</pre>

−

Replace <tt>anyname</tt> with any name you would like to use. These commands use uppercase global variables (<tt>anyname</tt> is converted to uppercase), which Vim will save providing you have the <tt>!</tt> flag in the <tt>'viminfo'</tt> option (<tt>:set viminfo^=!</tt>).

+

Replace <code>anyname</code> with any name you would like to use. These commands use uppercase global variables (<code>anyname</code> is converted to uppercase), which Vim will save providing you have the <code>!</code> flag in the <code>'viminfo'</code> option (<code>:set viminfo^=!</code>).

−

After typing <tt>:Hsave</tt> or <tt>:Hrestore</tt>, you can press Space then Tab for command completion.

+

After typing <code>:Hsave</code> or <code>:Hrestore</code>, you can press Space then Tab for command completion.

===Example===

===Example===

Line 111:

Line 111:

</pre>

</pre>

−

Use <tt>:sp</tt> to split the window and view the above text in both windows. Use <tt>\m</tt> to enable mappings, then apply some matches in the top window. Switch to the bottom window and apply some different matches: each window maintains a separate list of highlight matches.

+

Use <code>:sp</code> to split the window and view the above text in both windows. Use <code>\m</code> to enable mappings, then apply some matches in the top window. Switch to the bottom window and apply some different matches: each window maintains a separate list of highlight matches.

−

In the next step, you must use the <tt>-+*</tt> keys on the numeric keypad. While in the bottom window, press <tt>-</tt> to clear highlighting in that window. If wanted, press <tt>+</tt> to restore highlighting. In the top window, press <tt>*</tt> to apply the highlighting from the bottom window.

+

In the next step, you must use the <code>-+*</code> keys on the numeric keypad. While in the bottom window, press <code>-</code> to clear highlighting in that window. If wanted, press <code>+</code> to restore highlighting. In the top window, press <code>*</code> to apply the highlighting from the bottom window.

The script reads a [[CSV]] file with the same directory/name as the script, but with <tt>.csv</tt> extension. Each line should consist of five comma-separated fields, where the first field is a number from 1 to 99 inclusive (any other lines are ignored).

+

The script reads a [[CSV]] file with the same directory/name as the script, but with <code>.csv</code> extension. Each line should consist of five comma-separated fields, where the first field is a number from 1 to 99 inclusive (any other lines are ignored).

−

For example, the line "<tt>5,white,blue,black,green</tt>" would define a highlight group named <tt>hl5</tt> which color terminals would display as white on blue, and gvim would display as black on green.

+

For example, the line "<code>5,white,blue,black,green</code>" would define a highlight group named <code>hl5</code> which color terminals would display as white on blue, and gvim would display as black on green.

Revision as of 07:55, July 11, 2012

Vim provides a simple way to highlight all occurrences matching your last search, but it doesn't provide an easy way to highlight more than one search. This tip provides a script to make it easy to highlight multiple words in different colors. In addition, you can search for the next highlighted word.

Contents

Requirements

The script uses matchadd() which requires Vim 7.2 (actually version 7.1.040 or later). You will probably need gvim to display the colors specified in the script. In addition, the script assumes you have a numeric keypad and a system that allows you to map keys on that keypad (for example, the command :map <k0> :echo 'Hello'<CR> would display Hello when you press 0 on the numeric keypad).

You need the script (highlights.vim) and the file that defines the highlight groups (highlights.csv); these are given below.

Usage

The script defines highlight groups named hl1, hl2, ... hl9 (and more). Once enabled, pressing one of the keys 1, 2, ... 9 on the numeric keypad will highlight the word under the cursor with the colors defined in the corresponding highlight group (for example, press 4 on the keypad to highlight the current word with the hl4 group).

Type \m (assuming the default backslash leader key) to toggle mapping of the keypad on/off. At startup, the numeric keypad operates normally because only \m is mapped.

After typing \m to enable the mappings, you could use the keypad to:

Press 1 to highlight the current visually selected text, or the current word (if nothing is selected). Highlight group hl1 is used.

Press 2 for highlight hl2, 3 for highlight hl3, etc.

Press 0 to remove all highlights from the current visually selected text, or the current word.

For example, if you place the cursor on the word "rain" and press 4 on the keypad, all occurrences of "rain" will be highlighted (use :hi hl4 to show the colors). No highlighting will occur in words like "rainbow".

Alternatively, place the cursor on "rain" and press viw to select the word, then 4. All occurrences of the visually selected text will be highlighted, including the "rain" in "rainbow".

Each window has its own set of highlights: if you use :split to show two windows, you can have one set of highlights in the top window, and a different set in the other window.

With the mappings enabled, you could use the keypad to:

Press - to remove all highlights in the current window.

Press + to restore the highlighting when - was last used in the current window.

Press * to restore the highlighting when - was last used in any window.

For example, suppose you are displaying two windows and you want the same highlighting in each window. In the first window, apply the wanted highlighting. Then (using the numeric keypad keys):

Press - to remove all highlights in the first window.

Press + to restore them.

Switch to the other window and press * to apply the highlights from the first window.

Searching

After using \m to enable mappings, you can search for the next or previous occurrence of highlighted text. Patterns highlighted with the :match or :2match commands are also found.

Type \f to find the next match, or \F to find backwards. This has no effect on search highlighting or history.

Alternatively, type \n or \N to search forwards or backwards. Now you can press the normal search keys n or N to find the next or previous occurrence.

Highlight command

The script defines a :Highlight npattern command where n is a number (1..99) and pattern is the text you want highlighted. Depending on your system, you may be able to use the abbreviation :Hi rather than :Highlight.

The following examples use different colors to highlight various patterns:

The second example is the same as placing the cursor on "hello" then pressing 5 on the numeric keypad. The last example highlights th occurring at the beginning of a word, not case sensitive.

The following command will list all active highlights (group names and patterns):

:Highlight

Other commands

After using \m to enable the keypad mappings, you can press 1..9 on the keypad to highlight the visual selection (if any), or the current word. For example, pressing 4 on the keypad will highlight using the hl4 group.

In addition, you can press a digit 1..9 on the main keyboard before pressing 1..9 on the keypad. For example, if you press 2 on the main keyboard before 4 on the keypad, the hl24 highlight group will be used.

The :Hclear command can clear highlights. After typing :Hclear, you can enter an argument, or you can press Space then Tab for command completion (highlighted patterns). Examples:

The following command will create a scratch buffer showing all the defined highlights:

:Hsample

The following commands can be used to save or restore the current highlights:

:Hsave anyname
:Hrestore anyname

Replace anyname with any name you would like to use. These commands use uppercase global variables (anyname is converted to uppercase), which Vim will save providing you have the ! flag in the 'viminfo' option (:set viminfo^=!).

After typing :Hsave or :Hrestore, you can press Space then Tab for command completion.

Use :sp to split the window and view the above text in both windows. Use \m to enable mappings, then apply some matches in the top window. Switch to the bottom window and apply some different matches: each window maintains a separate list of highlight matches.

In the next step, you must use the -+* keys on the numeric keypad. While in the bottom window, press - to clear highlighting in that window. If wanted, press + to restore highlighting. In the top window, press * to apply the highlighting from the bottom window.

Defining highlight groups

The script reads a CSV file with the same directory/name as the script, but with .csv extension. Each line should consist of five comma-separated fields, where the first field is a number from 1 to 99 inclusive (any other lines are ignored).

For example, the line "5,white,blue,black,green" would define a highlight group named hl5 which color terminals would display as white on blue, and gvim would display as black on green.

References

Comments

A recent edit added a note regarding the script that "Selected text can not be highlighted in unmodifiable buffers; "normal! gy" does not work". I guess "gvy" was intended, rather that "gy". However, I can select text in a Help buffer (which is ":set nomodifiable"), then highlight all occurrences of that text using the script in this tip. I have removed the note pending some further information (what is it exactly that does not work?). JohnBeckett 04:09, March 5, 2011 (UTC)