Hacker News new | ask | show | jobs
by roryokane 4801 days ago
Here’s an example of powerful customization. I recently wrote a macro in Vim that, when run in a particular Visual Basic file, would search for a certain part of the file with an SQL string, delete everything in the file but the SQL string (just temporarily), and reformat the SQL string for easy viewing. I needed a macro because that Visual Basic file was compiled from another source, so it kept getting rebuilt to the full version but with different SQL, and I had to keep on using my macro to view the new SQL.

The main reason this was easier in Vim than it would be in Sublime Text is that I could edit the macro after the fact – the macro was just text. All macros are stored in a register, and registers are basically like multiple clipboards, so to edit the macro, I just had to paste the contents, change the text, and copy it back to the same register. Because of that, I could develop the macro bit by bit. At first, I just made the macro delete the parts of the file other than the string with SQL. Then, when I realized that I was going to be viewing the SQL in this file many more times, I edited the macro’s register. I was able to develop the macro bit by bit. And I could even save the macro in a text file for me to load later. Basically, I used Vim macros as a substitute for a scripting language – Vim has convenient text-manipulation functions built-in, and text manipulation and viewing were my goals.

Another way Vim made writing the macro easier is that it has more powerful macro primitives. It is easier to, for instance, select all the text in the next quoted string on this line, because Vim’s text-manipulation primitives are more granular. I think Sublime Text can select the cursor’s current “block”, but not a double-quoted string specifically.

Here’s the final macro (^M carriage return characters were turned into actual newlines, and ^[ and ^V characters are invisible):

    /History Section goes here
    
    f"vi":s/\( \+\)\(	\+\)/\r\2/g
    gv--dgg/\.replace(
    hhi
    --
    dG:setf sql
    gg/"Select
    a
    -V-I--
    
    

.