Getting the job done with Vim
Last Friday, Cyril pinged for an after-office beer party. It's all legitimate for a Friday evening :-)
At my workplace I in-turn bugged Kaviraj, a networking fellow who enjoys the usual geek talks.
We finished work and reached Flying Dodo some time after 19h00. Cyril and the others had taken the table outside. We could that way enjoy the cozy weather, not too warm neither cold, listen to the live band and have a chilled beer. To make it more fun I asked for pizza.
A while later Mike joined us. Kaviraj, Mike and I got into some fun chatting about CLI tools. Mike was talking about "vi" and we shared the handy tricks when editing config files.
Commenting multiple lines in Vim
To toggle between line numbering and without line numbers, do
:set nu and
:set nu!. The line numbers are useful when editing config files, say when you need to comment a block of directives (e.g from line 15 to 25). You would do
:15,25s/^/#/g to comment and commenting out would be
In-line replacement of a word
At times you find a long word in a line which you need to replace. While your cursor is at the beginning of the word, you press
cw and enter the new word. It's replaced. Is it difficult to remember
cw ? Just remember "change word" :-)
Saving with "root" privilege
Ever edited a file and while saving you realise you actually require super-privilege? It happens when you are tinkering as a regular user and the file is owned by root. If you're a sudoer then the following could save you time:
:w !sudo tee %
! symbol allows you to execute shell commands and the
% signifies the current file. We're thus saying update by sending the content to the current file with sudo privilege.
Find and replace
To trigger a find we could simply do
/theword and press
n to hop to the next occurrence of the word. We could search the whole file for a particular word and replace all occurrences:
We could also limit the find & replace within a block of lines.
For more fine-tuning of the search, regular expressions would come handy.
Kaviraj & I left Flying Dodo at 21h00. That was a short moment having a geek chit-chat with like-minded folks.