2 min read

VI/VIM : how to comment multiple line and 4 other tips I use everyday

VI/VIM : how to comment multiple line and 4 other tips I use everyday

For years I remain lazy and didn't learn the basics of editing. Just the I for edit and ctrl+q!, ctrl+q, ctrl+w, ctrl+x. But I learn that you can do much more with some simple tricks.

how to exit vim

I was a heavy user of emacs but I never had a preference in the war between Vi/Emacs. I mostly use Red Hat based operating system (RHEL, CentOS or Fedora) so I start to use vi by default since it's the default text editor package.
Honestly for my usage I like it, It's enough to edit configuration file, made development in C, bash or other langage.

Comment and uncomment multiple line

To comment multiple lines go to visual block mode with ctrl + v then select the line with the arrow.
After that go to insert mode with shift + i. Type # then esc.

That's it !

To uncomment go to visual block mode again with ctrl + v then select the column with comments. After that you just have to type d in order to remove comment.

Replace multiple occurence

For replacing all word old by new in the whole document, just execute the command :%s/old/new. If you just want to replace in the current line just remove the % like that : :s/old/new.

If it's not working you may have a problem with the gdefault parameter. Just add /g at the end, this will explicitly add the global flag.

For case insensitive you can add the /i parameter at the end.

Dont forget to escape when it's needed.

Indent/Unindent multiple line

Go to visual mode with v and select the lines with the arrow, then press > to indent and < to unindent.

Copy/cut and paste one or multiple line

To copy/cut paste one line, just press twice y to copy or d to cut then press p to paste.

To do that on multiple line, go to visual mode to select a line or multiple line with v then press y to copy, x or d to cut, move where you want and then press p to paste.

Delete multiple line

If you've understand how to copy/cut and paste multiple lines, you'll already now how to do it... yes, go again to visual mode, select the lines then press d :)

Obviously I know that I'm not a rockstar but I hope theses tips if you don't already know will helps you :) that's all for today!



Feel free to correct me if you see any typo or if something seems wrong to you.
You can send me an email or comment below.

Picture : Wikimedia