Reader small image

You're reading from  Mastering Vim

Product typeBook
Published inNov 2018
PublisherPackt
ISBN-139781789341096
Edition1st Edition
Tools
Right arrow
Author (1)
Ruslan Osipov
Ruslan Osipov
author image
Ruslan Osipov

Ruslan Osipov is a software engineer at Google, an avid traveler, and a part-time blogger. He is a self-taught engineer. He started publishing personal Vim notes in 2012, and became increasingly interested in the intricacies of the editor and its applications in optimizing development workflows.
Read more about Ruslan Osipov

Right arrow

Refactoring Code with Regex and Macros

This chapter will focus on features provided by Vim to support refactoring operations.

We will cover the following topics:

  • Using search or replace functionality with :substitute
  • Using regular expressions to make searches and substitutions smarter
  • Using arglist to perform operations on multiple files
  • Examples of refactoring operations, such as renaming methods and reordering arguments
  • Macros, which let you record and replay keystrokes

Technical requirements

Search or replace with regular expressions

Regular expressions (or regexes) are wonderful, and you should know how to use them. Vim, as is custom among regex implementations, has its own flavor of regex. However, once you learn one, you're comfortable with all of them.

First, let's talk about the regular (that is, the normal) search and replace command.

Search and replace

Vim supports search and replace through the :substitute command, most often abbreviated to :s. By default, :s will replace one substring with another in a current line. It has the following format:

:s/<find-this>/<replace-with-this>/<flags>

The flags are optional, and you shouldn't worry about them for now. To try it, open...

Recording and playing macros

Macros are an extremely powerful tool that allow you to record and replay a set of actions.

Let's perform the same operation as before, using macros. We have the following code in farm.py:

...
def act(self, target):
for animal in self.animals:
if animal.get_kind() == 'cat':
print(animal.act(target, 'meows'))
elif animal.get_kind() == 'dog':
print(animal.act(target, 'barks'))
elif animal.get_kind() == 'sheep':
print(animal.act(target, 'baas'))
else:
print(animal.act(target, 'looks'))
...

We'd like to reorder arguments in animal.act calls. Open farm.py, and move your cursor to the top of the file with gg:

Enter macro recording mode using q followed by any register (let's pick a), as follows: qa...

Using plugins to do the job

"Wait," you ask. "There were plugins to do this all along?" Indeed, there are plugins that support refactoring operations—be it modifying parameters, renaming, or method extraction.

However, when working with existing refactoring solutions, I always find they do almost, but not quite, what I need. That's why I continue to write fancy substitute commands for refactoring. I find the cost of incorporating a refactoring plugin into my workflow, only to switch to :substitute commands for some of my refactoring needs, too high.

At the time of writing this book, there's no go-to refactoring plugin. Some are language-specific, and some focus on only certain aspects of refactoring. For instance, plugins like YouCompleteMe provide semantically-aware renaming commands (such as :YcmComplete RefactorRename).

Your best bet is...

Summary

In this chapter, we covered the :substitute command and macros—two powerful tools we can use for refactoring.

We covered the :substitute command and its flags. We looked into arglist, which is a way to execute a command across multiple files.

The :substitute command also supports regular expressions, which make your life a lot easier by allowing you to go beyond literal matches. We covered the basics of regular expressions and Vim magic modes (which are ways of interpreting special characters when parsing regex-enabled strings).

Finally, we looked at macros: a feature that lets you record and later replay a set of keystrokes. Macros can be edited the same way that registers can, and can also be made recursive to play as many times as needed.

In the next chapter, we'll cover customizing Vim for a personalized editing experience.

...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Vim
Published in: Nov 2018Publisher: PacktISBN-13: 9781789341096
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ruslan Osipov

Ruslan Osipov is a software engineer at Google, an avid traveler, and a part-time blogger. He is a self-taught engineer. He started publishing personal Vim notes in 2012, and became increasingly interested in the intricacies of the editor and its applications in optimizing development workflows.
Read more about Ruslan Osipov