One of beautiful thing about vim is that you can learn something even after using 5+ years. Even though usual vim learning curve characterized like below:

Vim Learning Curve

This is the command I’ve been looking for a long time. I accidentally saw it on one of the ThePrimeagen video.

Entrée

Let’s assume that I’m on the line 5 and want to update "_cache_update_cache" on the line 30, to "_cache_update_current".

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

...


...



class UpdateCacheMiddleware(MiddlewareMixin):
    """
    Response-phase cache middleware that updates the cache if the response is
    cacheable.
    Must be used as part of the two-part update/fetch cache middleware.
    UpdateCacheMiddleware must be the first piece of middleware in MIDDLEWARE
    so that it'll get called last during the response phase.
    """

    def __init__(self, get_response):
        super().__init__(get_response)
        self.cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
        self.page_timeout = None
        self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
        self.cache_alias = settings.CACHE_MIDDLEWARE_ALIAS

    @property
    def cache(self):
        return caches[self.cache_alias]

    def _should_update_cache(self, request, response):
        return hasattr(request, "_cache_update_cache") and request._cache_update_cache

...

What I was doing: press 25jf"ci" on Normal mode, which means:

  • 25j : down 25 line
  • f" : find the first occurrence of "
  • ci" : change inside of "

and then write _cache_update_current.

The annoying part is you have to press additional f" every time. For a normal user it probably means nothing, but for a vim user it means a lot, since “Real Vim ninjas count every keystroke”1

Therefore I was using a macro and key binding for this. Until now.

Trick

The trick is just press ci"; you don’t have to press additional f" (for an unknown reason it works which shouldn’t). So our above command is just 25jci" now.

And it works with usual text objects like {, [, *, ' etc.

All done!


  1. https://www.vimgolf.com ↩︎