DEV Community

ϻ
ϻ

Posted on

Learn Vim for the Mindset

Today I was sat in the office finishing up work when I realised I had some configuration files to update. A few years ago I used to open the files in my favourite IDE, PHPStorm, and do something akin to the following:

  1. Open my trusty IDE.
  2. Open the target project for the configuration changes.
  3. Find and open the target file using the file tree.
  4. Highlight the text I need to change using my mouse.
  5. Delete the text.
  6. Copy and Paste in the new text.
  7. Repeat for other required changes across all files.

I hated the task, it honestly bored me to tears to the point I would put it off for as long as possible.

Fast forward to this evening and my workflow is instead:

  1. Fuzzy find my project (which automatically opens a new tmux session).*
  2. Open the target file with telescope.
  3. Search for the content to change with /<query>.
  4. ci" to delete the content and throw myself into insert mode.
  5. Copy and paste in the new content.
  6. Rinse and Repeat for all affected files.

[*] Big thanks to ThePrimeagen and his tmux sessionizer for that.

I strangely now enjoy the process, but there was something scratching at the back of my head - this seems like a lot of work, can I do this better? Now that thought is important and I'll come back to it, but lets look at a dummy file.

#!/usr/bin/env bash
./example \
  -database="example.db-vip.production" \
  -riak="example.riak-vip.production" \
  -elk="example.elk-vip.production" \
  -port=8989 > example-$(date +%s).log &
Enter fullscreen mode Exit fullscreen mode

Let us say we are moving from example.<resource>-vip.production to example.<resource>-vip.us-production. A fairly simple change and an easy enough job for the core linux util: sed.

sed -i 's/production/us-production/' example.sh
Enter fullscreen mode Exit fullscreen mode

Now lets say we want to apply that to all bash files for multiple different projects - which we will creatively name Project A, Project B and Project C. Have you ever tried to use an IDE for a find and replace across many big projects? The last time I did I'm fairly sure I could have got up and gone for walk while waiting for it to finish.

So lets think about this. We want to apply sed to all *.sh files within three different projects. Sounds like a job for find to grab the files we want, and then pass sed into the -exec to apply it to all the found files.

find ~/first/ProjectA ~/second/ProjectB ~/third/ProjectC \
  -type f -name "*.sh" \
  -exec sed -i 's/production/us-production/' {} \;
Enter fullscreen mode Exit fullscreen mode

Tasks like these become simple by combining the core linux utils that are available and that knowledge becomes even more valuable on environments where you do not have access to your IDE (i.e. an SSH session).

Now the title might have led you to thinking this post was going to include more vim than it has. So what gives?

Remember when I said this;

A lot of fun, but there was something scratching at the back of my head, this seems like a lot of work, can I do this better?

I truly believe this mindset I have developed stems from learning vim. I don't stop when a task is done there is always part of me wondering whether there was a better approach and this appetite to grow and learn has pushed my personal growth far beyond where I think I would be if I stuck with my IDE.

Yes, vim can have a harsh learning curve but the power you feel at your fingertips when it clicks is mesmerising. Even more than that, it also forces you into the command line. This will raise many problem domains that you previously solved with your IDE and instead you will commonly reach for the command line. This gives you much more flexibility as the tools you learn can be used in isolation or in conjunction with one another. They could even be turned into personal scripts to automate common workflow tasks. This programmability is only limited by your creativity.

So why would I suggest learning vim? In my opinion - it will help push you outside your comfort zone and that will force you to grow far more than you may first realise.


P.s. There are probably hundreds of better approaches to the above, and everything else that I do - that potential really excites me. So feel free to tell me all the things I am doing wrong.

Top comments (0)