Vim – highlight search keyword

 

By default search keyword won’t be highlighted in vim with Ubuntu OS.

To highlight search keyword we need to set hlsearch flag in vimrc.

Open /etc/vim/vimrc with sudo and append following line in it:

set hlsearch

After that save & exit from vimrc file. This will enable search keyword highlight across all vim sessions.

If you want to enable highlighting only for current session run following command in vim after opening a file:

:set hlsearch

vim convert to lowercase or uppercase

 

By using simple substitution we can convert  text in vim either to lowercase or to uppercase.

I have a following text in in one of file:

aBcDeFgH
iJkLmNoP
qRsTuVwX
yZ

To convert complete text to uppercase use following substitution code:

:%s/.*/\U&/g

or

:%s/[a-z]/\U&/g

Similarly to convert complete text to lowercase use substitution code:

:%s/.*/\L&/g

or

:%s/[A-Z]/\L&/g

Vim case insensitive search

 

By default vim editor is enabled to search case sensitive search.

Example:

/linux

Above example will looks for linux only not Linux or LINUX.

To enable case insensitive search use \c at start/end of your search key word.

Example:

/\clinux

or

/linux\c

Any of above example will ignore case and will search for linux.

vim count occurrences of a word

 

To count occurrences of a word in vim run following expression:

:%s/done//gn

Here I am opening a file with vim editor and calculating number occurrences of a word “done”.

It will display count in status line without changing buffer. This expression is supported by recent versions of vim only.

In older versions of vim use following expression:

:%s/done/foo/g

This expression will edit file and shows us count. After viewing count in status line press u to undo changes.