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.

Ubuntu change crontab editor permanently

 

By mistake if an uncomfortable editor is set for crontab, we can change it with following command:

$ select-editor

The output of above command will asks you to select any editor from list as shown below:

Output:

Select an editor. To change later, run ‘select-editor’.
1. /bin/ed
2. /bin/nano <—- easiest
3. /usr/bin/vim.basic
4. /usr/bin/vim.tiny

Choose 1-4 [2]: 4

Since I am comfortable with vim chosen option 4, which will set my crontab editor as vim.tiny.

After choosing an option just press enter and open crontab, it should opened with the editor you have selected. In my case it opened with vim.tiny.

There is one more post in my blog which will set the crontab editor only for that session.

Here is the link to change crontab editor for current session only.

-Sany

VIM edit only between specific line numbers

To edit content between specific line numbers use following expression in vim editor

:startLineNumber,endLineNumbers/search/replace/g

In above expression add s after end line number.

Example:

Add # character at a starting of the line from line number 3 10 use following expression in vim:

:3,10s/^/#/g

To replace all foo’s with bar’s from line 3 to 10 use following expressin:

:3,10s/foo/bar/g

We can achieve same functionalities with sed expression also with following expressions:

$ sed -i "3,10s/^/#/g" inputFileName
$ sed -i "3,10s/foo/bar/g" inputFileName

-Sany

By Sandeep Posted in sed, vim

vim E35: No previous regular expression

 

vim E35: No previous regular expression error is because of file permission to .viminfo file which is in you home directory.

By default vim will store previous searches or vim related history in .viminfo file.

.viminfo file permission should be 666, else it wont allow to write vim history in this file.

Change permission of .viminfo with following command:

$ sudo chmod 666 .viminfo

-Sany

Update default editor for sudoedit to vim

 

sudoedit is a command to edit other user/root files. By default editor for sudoedit command is nano editor. I am not much familiar with nano editor very well, so I thought to change the default editor form nano to vim.

Following is the procedure to update editor:

we need to use update-alternatives command to do modifications.

$ sudo update-alternatives –config editor
The above command will show some options as shown below

There are 3 choices for the alternative editor (providing /usr/bin/editor).

Selection    Path                Priority   Status
————————————————————
* 0            /bin/nano            40        auto mode
1            /bin/nano            40        manual mode
2            /usr/bin/vim.basic   30        manual mode
3            /usr/bin/vim.tiny    10        manual mode

Press enter to keep the current choice[*], or type selection number:

where * stands for default editor.

Here select and type the number related to vim editor, then press enter that will update the default editor.

To conform about modifications again run the update-alternatives command.

$ sudo update-alternatives –config editor
There are 3 choices for the alternative editor (providing /usr/bin/editor).

Selection    Path                Priority   Status
————————————————————
0            /bin/nano            40        auto mode
1            /bin/nano            40        manual mode
* 2            /usr/bin/vim.basic   30        manual mode
3            /usr/bin/vim.tiny    10        manual mode

Press enter to keep the current choice[*], or type selection number:

We can conform that the editor is changed form nano to vim.basic

-Sany