Old Guy New Trick

An old guys journey to learn how to code.

Learnings from the Front


Author: John on June 23, 2015

I've been watching some shows lately that focus around World War I and II, thus the catchy title in todays' post.  I am now in week three of the new job as a full time Ruby developer, and I've already learned a lot more than I was expecting.  Today I would like to share some little tidbits of knowledge I have accumulated.

Prior to accepting this new job I had been slowly learning how to use vim for more than a quick edit of a server config file.  But it wasn't until I was spending my entire day in vim, and pairing with others, that I realized how powerful vim is, and how little I know about it.  I was a Sublime Text user, and the switch to vim was a leap for me. I had a certain workflow down in Sublime and I needed to learn how to do the same types of things, and more, in vim.

One of my favorite aspects of Sublime was that it remembered all of the files I had open previously, so that when I restarted Sublime, all of my files would be re-opened in their respective tabs.  For about two weeks, I really missed that feature and finally decided, surely vim can do this.  Off to the net for a solution.  I was excited to find that not only could I save all of my open files and tabs, but I could do it with a twist.  Let me explain.

There are times where I am working on say two or three features/issues/bugs through out the span of a few days.  I will bounce between branches off our main app (master) and while working in each of those branches I need different files open.  Vim has a cool command, mksession, that will allow me handle this scenario.  For example, I am working on feature add_user_attribute in a branch with a similar name.  I have six or so files open to work on this item.  However, I need to go work on another task but I don't want to have to remember all of these files that I had open.  Prior to this new tip, what I was doing was executing the command :tabs and taking a screen capture of my open tabs and files.  But with this new knowledge, I now can do the following:


:mksession ~/add_user_attribute.vim

That will save my entire vim session - all open files and their respective tabs.  You can modify the file and path to your liking.  In my example above I am saving the file add_user_attribute.vim to my home directory.  With my session saved, I can now go work on some other tasks.  When I come back to this branch, and want to resume my work, I simply type the following:


vim -S ~/add_user_attribute.vim

Pretty neat, huh?  Sure beats taking a screen shot and re-opening the desired files each time.

To assist me in my learning of vim, I have a desktop/space dedicated to Vim research.  I'm not sure if other operating systems have that feature, but I work on a Mac so I can swipe or Cntrl+right_arrow/left_arrow to work in another desktop/space.  In my vim research desktop I have Chrome fired up with tabs full of answers to all the common things I'm trying to do with vim.  Those browser tabs stay there until my muscle memory has kicked in and I no longer need them.

One more quick tip, as I just learned this today.  When I run my tests and see the failures, there is usually a line number associated with the failure.  In the past, I would switch/open the test file, type gg to go to the top of the file and then type nnj, where nn equals the reported line number.  So if my failing test was on line 124, I would do the following:


gg
124j

It wasn't until I found myself doing this way too often that I decided there had to be a better way.  This is vim, of course there is!  To accomplish the same task, more efficiently, one can use:


:124<cr>

And that is all I have for today.  I've learned a lot more neat new stuff with vim, but I'll save that knowledge for a rainy day.

Learn Something New Every Day

Last Edited by: John on November 10, 2015