Scripting Editors: It Doesn't Have to be VIM

Putting a pin in the text editor holy war – do you use VIM, or Emacs?! We may point out hipster tendencies by using these “way better” vintage technologies, but there is still extreme value here. What you’re missing out on is having scripting in your editor. This will add years to your life. If you ever had the need to bulk edit files in your repository, or felt monotony while making a code update, read on. I choose VIM and I’m going to walk you through one catastrophy of a development day. If you can do this in your editor, you keep using that. This will presume you can reasonably navigate VIM, but should show the potential of using any of these technologies.

So let’s say you find yourself in a project with 128 YAML files that all need the same update. Do you manually edit each file, with your mouse? Brutal. Do you write a python script to parse every YAML file, edit it and write it back in place? Savage. This certainly is some project that predates you. People have probably left the company because of these 128 files, and now you need to update them? Enter Editor Scripting.

Case Study

Edit 128 YAML files without losing your mind. Remove the bad_values key and all it’s child values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
file: 1
bad_values: #
value: "1" # delete these lines in many files.
author: last guy who worked here #
rest:
of:
- file
bad_values1: #
value: "1" # delete these lines in many files.
author: last guy who worked here #
bad_values2: #
value: "2" # delete these lines in many files.
author: last guy who worked here #
bad_values3: #
value: "3" # delete these lines in many files.
author: last guy who worked here #

1. Target the files you need to edit.

Two strong choices here – either aggregate a list of filenames (which will be super easy if you know how to do visual editing in VIM):

1
vim file1.yml file2.yml ...

or throw a regex at it. grep? Yes, please.

Open vim in folder of your choosing.

1
:grep -R bad_values *

Your screen will probably throw up on you for a spell, and then you’ll press enter a couple times and find yourself on the first occurance of bad_values. Completely unobvious to you, you can type :cnext<enter> to move to the next occurance.

2. Make the edit.

Think of this exercise as a script. You don’t want to move over into edit mode and stumble around the text. You wish to tell the machine what you would like it to change.

In this case you know you will be on the bad_values line. You want to delete the present line, and the two lines that follow it. You just happen to solve this 3 line delete situation by pressing 3dd. Generally speaking, VIM operates by receiving commands as a “motion” and an “action.” A motion could be “the next word”, “the next 3 sentences”, or read until you see a “,”. If you can think it up VIM can do it. Actions include: delete, replace, substitute, add one to the number. You’ll need to learn a handful of each of these, and you’ll spend the reset of your career remember-forgetting them. Know a couple well and move on.

Now you’re getting somewhere. You can make a todo list of files or lines, and run a single command on them. You don’t have to write 10’s of lines of code to read in files, modify using your clever technique, and then write. This is usable. But this probably seems a bit basic to you.

3. Multi-step Edits (recording)

Realistically you probably aren’t going to have as much luck as above at some point. You’ll learn VIM well enough to know you can make a couple deterministic motions to get into the right place, but you’ll be in the same situation as before (but looking cooler while doing it.) In these situations you can tell VIM to record your keystrokes and replay them for you.

For instance in the YAML example, maybe I find myself in the file, I wish to update all the bad_values value keys. Let’s get ridiculous. Follow along by pressing the code keys below, do not add or escape any additional keys.

  1. Start a new recording q, name it something, let’s say w, but you have 26 letters to choose here.
  2. Search for the areas to update. / (search) for bad_values. This highlights your targets and gets you most of the way there.
  3. You really want to change the line below your search target, so let’s press the down key (j if you’re a pro.)
  4. Let’s say we want to increment the number in the quotes. <ctrl>+a. If you want to add 10, 10<ctrl>+a. want to update it because it’s a string? ci" will “change” “inside” the ‘“‘ quotes. No kidding.
  5. Stop the recording. q
  6. Repeat this process as many times as you like @w.

Conclusion

I will never tell a developer to use any certain tool, but having at least one scripting editor in your tool belt will take you to the next level.