Unleash your inner root

`vim`: Multi-Line Edits!

Greetings IT overlords! Just a quick tip for those vim users out there who, like myself, are on a never ending journey of ultimate vim mastery. Sometimes in vim, you may need to comment, un-comment, or otherwise perform some edit that needs to take place on many lines. This can often be a time consuming action of deleting one line at a time, or inserting one comment symbol per line at a time. It sucks. Don’t do it, if you do you will make baby Tuxus cry.

To perform this magical bit of vim-wizardry, you need to be introduced to two elements.

The first is visual mode.

“but wait” you may be thinking, “I already know about visual mode, it lets me select a bunch of words/lines at the same time.” yes yes, You are correct. However, some out there may not, as I was not until fairly recently, aware of the different types of visual mode.

There are three types of visual mode, accessed as such:

v         # Standard Visual select
<shift>-v #Line Visual select
<ctrl>-v  # Block Visual Select
Standard Visual Select

Standard-Visual Select

Line Visual Select

Line Select

Block Visual Select #1

Block Visual Select #1

Block Visual Select #2

Block Visual Select #2

As you can see with the Block Visual select, You can select the same amount of characters PER LINE. Meaning you can select 1 character downward on each line, and inch forward one character at a time.

Now. After selecting all the lines as in “Block Visual Select #1” you are ready for the second element (remember I said there were two things you needed to know?)

 <shift>-i #enters a special insert mode when selecting things in visual mode. 

And type in whatever you wish you insert, Such as “#”:

Entering insert mode

Then press

 <esc>  #exits insert mode, and applies insert to all selected lines

Exiting Insert mode

As you can see this allows you to insert multiple lines into a text file by only typing them once. You could also insert spaces, or any amount of text/formatting that you need to accomplish. Also using Block visual select you could issue standard delete commands to delete comments from multiple lines.

Make the OS yours to command, don’t do things the hard way!

Brandon.Graves • November 13, 2015

Previous Post

Next Post

Leave a Reply

Your email address will not be published / Required fields are marked *

%d bloggers like this: