Left4Code

Using :norm And :g In Vim

--| Posted: 2024-09-30

The Problem I'm Dealing With:

Basically, I have a bunch of links that are formatted like this with a bunch of forensics tools I want to document how to use for the free digital forensics course I'm trying to make on this site, and I want to add a long dash next to the name and in brackets, either and X symbol or a check mark to show if there is a lesson or guide I have made for a particular tool. I have around 50 links that look like this and don't really want to deal with manually entering these very difficult to type unicode characters for them 50 times over, so let's use Vim to alleviate some of the pain!

41             <ul>
42                 <li><a href="itscoming.html">dd </a></li>
43                 <li><a href="itscoming.html">ddc3dd </a></li>
44                 <li><a href="itscoming.html">Guymager </a></li>
45                 <li><a href="itscoming.html">Cyclone </a></li>
46                 <li><a href="itscoming.html">ddrescuer </a></li>
47                 <li><a href="itscoming.html">ftkimage </a></li>
48                 <li><a href="itscoming.html">Guymager </a></li>
49                 <hr>
50                 <li><a href="itscoming.html">GtkHash </a></li>
51                 <li><a href="itscoming.html">sha*sum </a></li>
52                 <li><a href="itscoming.html">hashboy </a></li>
53                 <li><a href="itscoming.html">hashid </a></li>
54                 <li><a href="itscoming.html">pehash </a></li>
55                 <li><a href="itscoming.html">OpenTimestamps </a></li>
56             </ul>
	    

However, I've never really done anything in terms of bulk modification outside of the normal :%s/something/somethingelse/g. It turns out, that there's a lot of ways to deal with the problem I have specifically, :%s is a way to do it, but I would rather learn something new that might help me (or you!) somewhere else where :%s doesn't work well.

:norm, My Guardian Angel.

:norm is such a cool concept in Vim and I really like it, basically what it does is it allows you to do actions you would usually use in normal mode, but using another mode like visual or :g mode for bulk (which I'll get into soon.) So actions like 04ldw can be completed using the colon key by typing :norm 04ldw and it'll do it on whatever line you're currently on.

Combining :g With :norm To Modify In Bulk

I know, I didn't explain what :g does, but it's not that confusing and from the minimal amount of information I've learned about it, it's basically just the way to apply whatever you're doing to one single element to all elements that match the one. With :%s, you can add /g to the end of it to do the same thing. For this specific problem, I'm going to combine both of these things together to quickly insert what I need at a specific position for all 50-ish links.

The Command I Used To Solve My Problem (and The Problem I Created Accidentally!)

:g/<\/a><\/li/norm $4bi& [the dash] [the symbol] 

to break down what I haven't explained already, everything after the first "/" is what I'm initially searching for and the one right before the "norm" is what I'm replacing it with. The "\" is an escape character which allows vim to interpret the slash character as a literal and not a / used to denote what Vim should be substituting and not substituting. $ means to go to the end of the line, 4b means to go back 4 words, i goes into insert mode, and everything after that is just normal text


In the process of writing this, I accidentally ran the command on the HTML page I had already added the symbols to for the course and now there's two of them, I also just did a shift + z + z on instinct because I didn't realize anything was wrong, now undo is gone, so what should I do? Fix it manually and go against what I just taught you? Or should I do this but in reverse? I think we know what the answer is.


after fiddling with it for about 5 minutes, this worked successfully!

:g/<\/a><\/li/norm $8h6dbi;] 

One very small piece of advice I can give you to use your brain and successfully come up with these kinds of scary looking commands that seem really confusing, is that you should always be more scared of the 20 minutes of annoying manual labor you'll have to do if you don't figure it out, sometimes even if it takes you longer to figure out, you'll now have the tool to do something else much faster next time.


8h means move 8 characters to the left, 6db means delete 6 words back, and the rest you should already know.

Conclusion: Vim Is An Awesome Tool If You Let It Do Work For You!

Maybe I'm just too deep into using Vim to where I don't see anything else as any more useful, but I really don't think I would have been able to do something like this using Xed. There's a lot more that Vim has to offer, so I guess you should expect more posts about it to happen eventually because writing a site like this seems to challenge every fiber of my soul sometimes, and Vim makes it just a little easier to manage.