Here’s a handy collection of the “Vim tip of the day” posts that I’ve made so far.
I am a huge Vim fan. I’ve used it for years and have gotten to the point where I’m pretty fast with entering/editing text using it. The beauty of using Vim commands is that you can rapidly modify text without taking your hands off of the keyboard. When I first started using Xcode it crushed me when I realized there wasn’t a built-in option to use Vim commands. I am so much faster when I’m able to use them and I’ve felt like I’ve had one hand tied behind my back until now. I know there were third-party solutions but having it being an official part of Xcode is what I really wanted. This Indie Life Unfolding comic from Limited DFS and TopoloGiraffe, the creators of Bluebird app, sums up exactly how I felt when when I learned Vim commands were being added to Xcode 13.
These posts that I’ve been making actually started out as a joke based off of some friendly (?) banter between Dave Jacobsen and myself. By the way, if you’ve never seen Dave’s YouTube videos you should really check them out. His “My Top Charts iOS App” video is a favorite of mine.
Vim tip of the day! If you spot something obviously wrong you can change it quickly with Vim commands. Here I'm hitting "b" to jump backwards over words. You can enter "cw" to change a word, but I did "2cw" to change two words. @emin_ui @davejacobseniOS pic.twitter.com/CZ13uyGAfA— Chris Wu (@MuseumShuffle) November 4, 2021
It got some good feedback so I kept doing it. I’m running out of source material so it seemed like a good time to make a collection of my posts. I’ll add to this blog entry as I post more tips.
Of course you can use these commands in other applications that support Vim commands. This was just my experience trying them out in Xcode to see what was/wasn’t available. Also, there are multiple ways to do the same thing with Vim commands. These might not be these best way. They are just an option available to try.
Jump past words and change to the end of the line
Vim tip of the day! Here I'm using "w" to jump past words. In commands such as 'grep' a dollar sign means the end of the line. Last time I used 'cw' to 'change word' but this time I used 'c$' to change from the cursor to the end of the line. pic.twitter.com/WTPf4FnMFB— Chris Wu (@MuseumShuffle) November 5, 2021
Vim tip of the day! The tilde (~) will reverse the case of a letter (upper to lower and vice versa). If you hold it down it will keep doing it while ignoring characters that aren't letters. pic.twitter.com/LaIA5d66ZY— Chris Wu (@MuseumShuffle) November 6, 2021
Different behavior when deleting a character
I filed feedback about this not working as expected. Much appreciated that it was corrected.
Vim tip of the day! If you hit 'x' it will delete the character your cursor is currently on. An interesting feature is that if the cursor is at the end of the line it's going to start moving backwards. This actually didn't work in the early Xcode 13 betas but does now. Nice! pic.twitter.com/4OHBLEgLZ6— Chris Wu (@MuseumShuffle) November 13, 2021
Jumping to the top/bottom of the file
Vim tip of the day! "G" jumps to the last line of the file but "1G" also jumps to the first! And use can use "dG" to delete from the current line to the last line of the file. pic.twitter.com/VuMAezamwO— Chris Wu (@MuseumShuffle) November 14, 2021
Deleting a word or multiple words
Vim tip of the day! I've mentioned 'cw' and 'dw' is similar except it deletes words. By itself it will delete one word, but you can also combine it with a count. 4dw deletes four words. pic.twitter.com/zkXvxl5dbi— Chris Wu (@MuseumShuffle) November 17, 2021
Yanking a word or multiple words
It turns out that if you try to yank more than one word Xcode gets into a strange state where you can’t use standard Vim commands to move the cursor and you can’t hit “i” to enter insert mode. You can only hit esc to get out and the words aren’t yanked. I filed feedback FB9768086 about this one.
Unusual Vim tip of the day! Just as with dw (delete word) and cw (change) you can use yw for "yank" (copy) word. Just as with 3dw and 3cw you can enter 3yw to yank three words. BUT I can't show you that. This is the result of yw, which yanks one word. I'm hitting 'p' to paste. pic.twitter.com/R5kc40Ah3m— Chris Wu (@MuseumShuffle) November 18, 2021
Yank from in a word
Vim tip of the day! I usually use 'yw' yank a word with the cursor at the start of the word. But you can "yank in word" with 'yiw' no matter where the cursor is on the word. Then you can paste that yanked word however you want. pic.twitter.com/5w00740qlo— Chris Wu (@MuseumShuffle) November 29, 2021
Swap two characters
Vim tip of the day! If you spot a typo that requires two characters to be swapped you can do that very rapidly. Just hit 'x' to delete the character and 'p' to paste it back behind the cursor. pic.twitter.com/YXiTsIPBbk— Chris Wu (@MuseumShuffle) December 4, 2021
Insert at beginning or end of line
Vim tip of the day! I'm guilty of only using 'i' for inserting before the cursor and 'a' for appending after the cursor but you can also use 'I' for inserting at the beginning of the line and 'A' for appending to the end of the line. I used all four commands in this example. pic.twitter.com/hkSxRJDN4T— Chris Wu (@MuseumShuffle) December 7, 2021
Jump past punctuation in words
Vim tip of the day! While 'w' moves forward to the start of a word and 'b' does the same moving backwards both will stop the cursor on punctuation in the middle of a word. You see this in the beginning of the video. If you use 'W' and 'B' instead the punctuation is ignored! pic.twitter.com/PY3v7Sh88K— Chris Wu (@MuseumShuffle) December 8, 2021
Attach the line below to the current line
Vim tip of the day! If you hit 'J' it will attach the line below the cursor with the current line with a space attached. I did not know that you can also use 'gJ' to attach the line without a space! The first example is 'J' and the second is 'gJ'. pic.twitter.com/OcKbOda2WF— Chris Wu (@MuseumShuffle) December 16, 2021
Easily replace the current line
Vim tip of the day! When I want to completely replace a line I erase it with 'dd' and insert on the line above the cursor with 'O' and start typing (first example). It turns out you can just use 'cc' to do that (second example)! pic.twitter.com/KlCiaCOMOd— Chris Wu (@MuseumShuffle) December 18, 2021
Jump to matching character
Vim tip of the day! The percent sign will move the cursor to the matching character. Here I'm using it on parentheses and curly braces. This can also be useful for error checking when you're missing a parenthesis. pic.twitter.com/etbqQwJXhR— Chris Wu (@MuseumShuffle) December 19, 2021
‘Change Word’ lets you paste text
When you change a word you don’t have to type the replacement. You can just paste text!
Vim tip of the day! When I showed 'change word' in an earlier example I typed the new word. But you're also free to paste. Here I hit 'b' to go to the beginning of the word, 'cw' to change the word, and cmd-v to paste. What I'm pasting came from the SF Symbols app. 1/2 🧵 pic.twitter.com/H056eVfmki— Chris Wu (@MuseumShuffle) December 28, 2021
Did you know that if you click on a symbol in the SF Symbols app there is a “Copy Name” option in the “Edit” menu? That will save you from having to type “externaldrive.fill.badge.person.crop”.
Different text can be pasted
Text put in a Vim register is different than text you copy/cut with standard Mac commands.
Vim TOTD! When you delete something with a Vim command (x in this case) it's put into a register where you can paste it back (P). But that register is completely different from where text you copied with cmd-c is held. I can still paste that with cmd-v after deleting with 'x'. pic.twitter.com/xg9ExGrIgJ— Chris Wu (@MuseumShuffle) February 1, 2022
Searching and moving on the same line
You can search for a character and move the cursor to it or NEXT to it.
Vim tip of the day! I didn't know this but variations of f and t let you search and move the cursor on the line you're on. Here I'm using f? to move to the ?, t: to move BEFORE the :, T: to move before the cursor moving backwards, F? to move to the ? moving backwards. pic.twitter.com/GMPDYPw1wS— Chris Wu (@MuseumShuffle) June 1, 2022
Vim commands work with multi-cursor editing
Vim tip of the day: Vim commands work with multi-cursor editing!! Here I'm adding some text, hitting esc, jumping to the end of the line with $, and then continuing to edit from the end of the line. pic.twitter.com/sLYABbuqUC— Chris Wu (@MuseumShuffle) August 1, 2022
The Vim commands added to Xcode so far have been a great start. What would I like to see added next?
- The "Dot" command - This is easily my most used Vim command and it’s painful not having it available. The “dot” command lets you repeat the last Vim command you executed. Deleted five lines? Well you can delete five more lines with just one key press. Changed two words? You can make the same change to two other words with just one key press. This is incredibly powerful.
- Marks - I’m used to “marking” a starting point in my code and then marking an end point. After doing that it’s easy to copy or delete whatever comes between those two marks. It’s also so useful to jump back to a section of code you’ve marked.
- Ex commands - I’m also used to transforming an entire file quickly with Ex commands. An example of how I do this is mentioned in a previous blog post that I made. Yes, you could easily accomplish the same thing with Xcode’s search/replace functionality but I’m really proficient at these commands and can enter them quickly without having to take my hands off of the keyboard.
Big thanks to the Xcode team at Apple for adding Vim commands to Xcode.
Shout out to Vim Cheat Sheet for refreshing my memory on some commands and teaching me some new commands!
If this post was helpful to you I’d love to hear about it! Find me on Twitter.