To change tab space width to 4 spaces add following code in /etc/vim/vimrc file
filetype plugin indent on
"to show existing tab with 4 spaces width
"when indenting with '>', use 4 spaces width
"On pressing tab, insert 4 spaces
This will convert all existing tabs to 4 spaces width & also all new tabs with 4 spaces width.
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:
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:
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:
To convert complete text to uppercase use following substitution code:
Similarly to convert complete text to lowercase use substitution code:
By default vim editor is enabled to search case sensitive search.
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.
Any of above example will ignore case and will search for linux.
Here I am showing couple of examples to comment several line at once using vim editor.
I have a text file with 15 line in it.
To comment from line number 5 to 10:
To comment 5 lines after line number 5 and including line number 5:
To comment all lines:
To convert all text in vim editor to lower case use following expression:
To convert all text in vim editor to upper case use following expression:
To count occurrences of a word in vim run following expression:
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:
This expression will edit file and shows us count. After viewing count in status line press u to undo changes.
By mistake if an uncomfortable editor is set for crontab, we can change it with following command:
The output of above command will asks you to select any editor from list as shown below:
Select an editor. To change later, run ‘select-editor’.
2. /bin/nano <—- easiest
Choose 1-4 : 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.
To edit content between specific line numbers use following expression in vim editor
In above expression add s after end line number.
Add # character at a starting of the line from line number 3 10 use following expression in vim:
To replace all foo’s with bar’s from line 3 to 10 use following expressin:
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
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