The Case for Vim

Swift, Kotlin, Java, Objective-C, Python…

You are probably familiar with the word Vim (more probable if you run Unix). It is also likely that you are not aware of its primary reason for existence. For that reason, I will try to reveal what is beneath the surface for those who have not seen under the tip.

I Once Hated Rebase, Now It Is A Home To Me

I have first encountered Vim when I ran the command git rebase. And as most people who first interact with Vim, I was stupefied and petrified. As if I suddenly forgot how to use a computer, what a computer is, what my name is, where I was…

For a long while, I tolerated its absurd, seemingly counter-intuitive way of functioning. It was literally impossible to quit from the terminal. Of course, there is a reason for this.

Let There Be Light

Before I dive into its features, I want to mention Vim support. Vim is included in most Unix systems along with Mac’s. You can edit any text-based file with Vim. Just go ahead and type vimtutor (vimtutor starts the interactive tutorial to teach you the basics) in your terminal (or you can open your .java, .swift, .md, .txt etc. files by running vim "FILENAME" command).

Of course in few seconds, your mind will produce the following question: “Am I supposed to write my program in terminal, in 2018?”. No. You can add coloring and auto-completion and more features to your terminal Vim. But you can (and probably should) add Vim plug-in to your IDE. All JetBrains IDE’s (Intellij, Webstorm, PyCharm…) supports Vim natively. There also exist a plug-in for Xcode called XVim (XVim2 for Xcode 9 and above).

Weapon of Choice

Most of our time is devoted to reading, editing, adding and removing code. No matter how cool the device on the left looks, it still has two buttons on it which makes it gazillion times less expressive than a keyboard. So our weapon of choice should be a keyboard, not a mouse. Meaning: We should prefer keyboard over mouse in as many cases as we can. You use all those short-cuts for a reason. Without further ado…


Vim has three modes: Normal, Insert and Visual.

Normal Mode is the mode where you try to type in but you can’t. It is used to run commands and navigate through, not to write code. You can navigate through the text with j, k, l, m keys which map to down, up, left and right respectively. 5j would move to 5 lines below. w jumps a word. 7b jumps back 7 words. These are called motion keys which can be prefixed with a number. d means delete, and c is change. Running dw at the beginning of a word would delete that word. Running 2wc would delete next two words and enter into insert mode.

Insert Mode works exactly the same with any other “regular” editor. You just type in and press esc to go back to Normal Mode.

Visual Mode is typically the least used (but used anyway) mode. I will not go into details.

It is best to go through vimtutor tutorial by trying all the things out yourself. It takes about half an hour to complete and you will get the basics. And even only the basics improve your speed tremendously. I also recommend watching this video, which sold me the idea of using Vim.

Roll the Sleeves

I use Vim only for few months now and I use very little amount of its features. Nevertheless, I certainly do not want to go back to Vimless days of coding. I will share my favorite features so far.

Say you want to change the code below:

if let color = view.button.title.color as? UIColor {

We want to replace view.button.title.color with something. You would hover your mouse to the beginning of view, either select the snippet with mouse or press shift+cmd+arrow multiple times and hit delete. With Vim, I can move to that line by something like 8j (or if my cursor is too far away, I can type in the line number followed by G command such as 57G. Then I would press fv which will navigate to first v character, followed by cW which will clear the next Word (word means next concatenated characters until a space or non-alphnumeric character while Word means whole chunk of next concatenated characters). So running cW at character v results in the following:

if let color = as? UIColor {

Then you type in something and hit esc.

Clearing Functions

To change all the arguments in this function, navigate anywhere between the parentheses (by 5k in the gif below) and hit ci( or ci). Translation: “Change inside of parentheses”.

To delete the implementation of the function, navigate anywhere in the function and hit di{ or di}: “Delete inside of braces”. Now you have an empty function.

Repeating Actions

We often write similar lines of codes repetitively such as initializing class variables in the initializer.

Yank (copy) the similar line by hitting yy, in this case:

self.friendsAndRelatives = friendsAndRelatives

Press p to paste the yanked line to a line below. f. (find .) followed by l navigates to the beginning of the word friendsAndRelatives . Hit ce and type in dateOfBirth. Then navigate to next friendsAndRelatives and hit .. Dot command repeats the last action.

M Marks the Spot

We can mark lines in the current file or in any file with m command. Say we have a 250 lines file and you are going back and forth between few functions. We can mark these lines to any key:

  • ma -> Line 25
  • ms -> Line 128
  • md -> Line 223

Pressing `a will jump to line 25, `s to line 128 and `d to line 223. We can also do this for navigating between different files: Just mark to capital letter instead: mA, mS and so on. This way, you do not need to search for the file everytime.

Find & Replace

I have the struct below and I want to change all var’s to let’s. If I use the traditional find & replace-all for this, all the var’s in the file will be replaced. With Vim, you can express anything you want so I can say replace the var’s between these specific lines: We will run a command now and to run a command you start with hitting to :.

Running :12,18s/var/let/g is what we need.

We just said, change every var between lines 12 and 18 with let.

Chill Vim Moments

Now I want to delete this struct because I feel like it: I hit d10j to delete 10 lines below.

To rename some part of the variable, use ct (change till).

Navigating with 5w to emailTextField and hit ctF to change till F letter. I could replicate the same command by pressing . on passwordTextField and it would also become usernameTextField.

More Than the Stars in Milkyway

There are something like infinite amount of possible expressions in Vim. The more you learn it, the expressive it gets and as a result, the faster you start to write, edit and navigate in your code. You can customize it all you want and/or use other people’s customizations that are available in many sites. You can watch vimcasts from sites like to learn more. I often feel like, “there must be a better way to express this” and If I can’t find it out by myself, I search the web and it is always there.

Learning Wall

People say Vim has a learning wall not a curve because you cannot do anything at first. I don’t fully agree. It seems highly counter-intuitive but not for long. It honestly took me few days to catch up with my normal editing speed (though the first 1 or 2 days were nasty). And after 2 weeks, I only started to get faster and faster. You might think, “well my time is consumed in thinking more than typing”. And you are right, but think about this extreme case: How worse it would be if you were to use on-screen keyboard with a mouse to code? You would do your job much much slower. I am here to tell you that the opposite is also valid.

Considering how much of our time is consumed in looking at computer screens, any percent of improvement should be a valid argument. It is hard to calculate the exact rate, though I sincerely think that it should be no less than 50%.

I wish you all mouse-less days of coding!

Developer Mind

Thoughts, experiences and knowledge sharing from software developers

Alp Avanoğlu

Written by

I follow the white rabbit

Developer Mind

Thoughts, experiences and knowledge sharing from software developers