Sublime to Vim!

Over the last few weeks I have made the plunge from coding within Sublime Text to coding in terminal Vim. I had attempted to do this a few times before but always gave up because there were a few things in Sublime that I used a lot that I didn’t know how to recreate in Vim.

I felt like it was going to take me forever to do anything and thought that outweighed the benefit. Anyway I encountered a problem on both laptops of Sublime constantly crashing so I took it as a sign that I should make the jump once and for all and invest effort into sticking with Vim.

In order to make the switch to coding in Vim a smoother experience I set up my Vim environment to look and behave as much like Sublime as I could. This might sound pointless, but now I have the familiarity of the layout with all the additional Vim commands which should ultimately save me a load of time in the future. In this post I will explain how I did this so that if you are thinking of switching, you shall learn a few ways to make the transition a bit nicer.

What is Vim?

I probably shouldn’t admit it, but when I very first heard of Vim I thought that it was some sort of alternative to Bash and was used primarily to run commands to run programs. This is NOT the case… Vim is actually a text editor within the terminal. You use it to open files and edit them, like you do with a regular text editor... just that now it is from within the terminal. It also comes with its own set of commands (though most are just one key stroke) which let you jump around the file quickly aka a dollar sign $ will take you to the end of a line, G will take you to the last line in the document etc.

Why Vim?

The reason a lot of programmers prefer Vim is that it is highly configurable, allowing you to map key strokes to custom functionalities. Because editing files in Vim happens within the terminal, you can't use the mouse to click to where you want on the screen. You need to get used to learning the commands to move around as quickly as possible.

This is the initial barrier, because as well as working on writing a program you also have to remember a load of commands in order to enter your code into the right place. The idea is that after a while these commands will become second nature and you will save a lot of time not having to use the mouse. Basically you have to slow down to learn and then you will speed up over time. This can be off putting at first but I am assured that the perseverance is worthwhile.

I can definitely say that after a couple of weeks of using Vim, you do get used to it quite quickly (ok ‘it’ at this stage only means a handful of commands) but it isn’t as difficult as you may expect. Something that is obviously useful is that you don’t need to switch between a separate editor window and the terminal, it is all in one place and you can switch through tabs easily using cmd + Shift + } or {.

Getting started!

The installation instructions can be found on the Vim website. I am using a Mac so it came shipped with Vim.

To open a file in Vim cd into the appropriate directory and run vim name_of_file.rb. This will then open up your file in vim. 

1

2a

At this point you will find that you can’t just start typing out the text (probably code) you want to enter. This is because on entering Vim you are placed into ‘normal’ mode. Here you can do stuff like entering Vim commands to move the cursor around or perform actions such as deleting text (for example dd will delete a whole line).

In order to add a new line of code to your program, you will need to go into ‘Insert mode’. This can be done by pressing i which starts insert mode from where the cursor is currently or a which will start insert mode with the cursor one space forward. In insert mode your keyboard will work as regular keyboard text input usually does, so hitting x is going to type a letter x as opposed to deleting a character like it would if you were in Normal mode. To get out of insert mode back into normal mode you need to press esc or ctrl+c.

To save changes go into normal mode and type :w (including the colon!) and to exit use :q. To do both at once use :wq (w = write, q = quit). An alternative to :q is ZZ.

On my first failed attempt of using Vim I literally just Googled the commands as I needed them. This is a terrible approach and disturbs the flow completely so I would seriously recommend finding a Vim cheat sheet online, printing it out and sticking it somewhere nearby. This is the one I have tacked to my wall.

An additional method, which I haven’t done yet for Vim but did for UNIX command line is to make cue cards of the commands (with the command on the front and what it does on the back) and to drill through them when you have a spare minute.

On my old laptop Vim didn’t automatically colorise the syntax, though it did on my new one. To turn syntax colorisation on each time you open a file do :syntax onand then :syntax off to get rid of it. A better option is to set it up to always be on automatically. To do this you will need to edit your .vimrc file which can be found in the ~ directory (if it's not there then you need to make one). This is where you specify customisations for your environment. Add syntax on to the end of the .vimrc file and save - your syntax will always be colorised on opening Vim :)

It is a good idea to start with creating a new file and trying out the commands from the cheat sheet. Also spend some time going through Vimtutor. Just type Vimtutor into the terminal to get started. Realistically though, individually opening files is annoying as it takes up time, especially if you are not completely confident on where files are within the structure. To conquer this I installed a Vim plug in called NERDTree which opens up your project in a file structure so that it looks fairy similar to Sublime (though I will admit not as pretty but you soon get used to it). NERDTree can be installed by following the installation instructions on Github.

To run NERDTree cd into the root of the directory that you want to display in the editor. Enter the command vim to open Vim and then enter :NERDTree.

3

This then begins to look a lot more similar to Sublime Text with the file structure along the side. To open a file hit enter.

indent

To open a file in a new tab hit t.

5

To switch between tabs use gt.

To get from the NERDTree edit window back to the side navigation use ctrl + ww.

So this is a good start to making the cross over from Sublime but it still isn’t ideal for moving around and opening stuff up. The next tool I would recommend is a plugin called CtrlP. This is used for opening files within a file structure but makes things a lot quicker than scrolling through the side menu in NERDTree.

You can use CtrlP with NERDTree. Open up your project in NERDTree and Enter :CtrlP the/file/path then hit enter and you will see a list of all the files in that directory, type in the name of the file you want and this will find the file for you, press enter to open it. ctrl + c closes CtrlP.

6

7

At this point I was pretty happy that I had a fairly similar set up to what I was used to, at least visually. The thing that stopped me committing to using Vim was that in Sublime I always used ctrl + cmd + f to search for snippets of code within the whole project and cmd + f to look for words within a particular file. I struggled to figure out how to do this on Vim so I sent out a cry for help on Facebook and within about 10 minutes I had the solution thanks to wonderful friends.

If you just want to search for a word in a file (cmd + f stylee) then /  followed by the word you want will put the cursor onto that word.

In order to search for all instances of a word within the codebase (cmd + ctrl + f style) you can use :Ack or :Ag. This means that you can run :Ack thing you want to find and it will show you all the places that word shows up:

ack

8

You can then open up the files in the list by moving the cursor over the file name and hitting enter.

In order to install Ack I decided to use Vundle as a friend recommended using a plugin manager as it makes it easier to customise your environment with loads of features. A couple of gotchas for installing Vundle. When you copy the example on the README into your .vimrc make sure the formatting is right, the lines with the " " in front of them are comments and I think when I first copied and pasted somehow the format meant some stuff was commented out that shouldn't have been. Also it tells you to launch Vim and run :PluginInstall. I had my .vimrc file open in Vim so I kept trying to run :PluginInstall from there and it wouldn’t work. Anyway in the end it turned out I needed to close it and open a new vim session and run the command… so just a heads up!

In order to install Ack I added Plugin mileszs/ack.vim to my list of plugins in my .vimrc file and ran :PluginInstall… so mega simple to install thanks to Vundle.

9

Running :h vundle will give you loads of commands for managing and viewing your plugins :PluginList lists them all.

So now that I could view my file structure text editor style, move around it easily and search for files I felt happy using Vim. There's still lots to learn and I have been told it takes years to really get the hang of using Vim but I am definitely enjoying it. I think the best way forward is just to delete any other text editor from your computer and use Vim as much as possible, even though it will slow you down at first.

Here are a few useful commands that I have been using a lot:

* - shows you where your cursor is on the screen

x - deletes character underneath cursor

u - undo

ctrl + r - redo

h - left

j - down

k - up

l - right

O - Takes you to the start of the current line

$ - Takes you to the end of the current line