Coders knew Basic Tutorial in Vim for the last twenty years for its unmatched efficiency in editing source code. Vim was not too popular in the Rails community until lately when Tim Pope published his plug-in: Rails.vim. This plug-in and Vims legendary extendibility made it the editor of choice of many programmers who used some proprietary applications in the past. The plug-in offers shortcuts for easy navigation between files, makes it possible to run scripts, generate controllers, views, do some refactoring, and do auto completion , all of these from Vims cosy interface. Some people say that it is the least user friendly text editor ever. While these accusations might be partly true, because of the rather steep learning curve, many programmers just liked the way Vim helped them do their work quickly and efficiently.
Enough said lets see how to create a very simple blog in Rails from Vim.
Basic Tutorial in Vim
1. Create a project – Basic Tutorial in Vim
- First and foremost we need to create a new Rails project.
:Rails new myblog
This commands also opens the README file from the new project.
- After this use NerdTree to see the contents of the myblog folder.
- Now , you must change the working directory to myblog.
Type cd in the NerdTree window , or
This is especially important step if you run Vim from a desktop launcher.
- Lets see if everything worked.
:Rpreview. This will boot Webrick, then opens the default browser with the Url http://localhost:3000.
- Of course , you don’t have to type everything, a few initial letters and awill do trick. If you have exuberant-ctags installed , it is sensible to run it now . Later this will help you to navigate to the definition of variables and functions.
- Run :Rtags after you added some functions .
It can become cumbersome, so my advice would be to run ctags after git commits. You can also install a git hook that runs the program automatically.
2. Add some resources
- It is time to add an Article resource. The easiest way to do that to generate it.
:Rgenerate scaffold Article title:string author:string content:text
- Now run some rake tasks.
We have a bunch of files neatly populated. If there were some errors, the Quickfix window will be opened, and you can navigate between them by pressing cn and cp.
- Lets run our web application in the browser (You can type
:Rpreviewnow). http://localhost:3000/articles You can see , that everything is fine and dandy , we can edit, update and delete records. The real strength of Rails.vim is the navigation .
3. Navigate code
:Rcontroller articlesAfter :Rc you can press then another will show you all available controllers. There is a bunch of other motion commands , like Rview and Rmodel, so feel free to use them if you need to.
- We successfully opened the controller of the Article resource, now lets talk about the
For a controller the alternate file is the test, and the related is the template. Keep in mind that this behavior can change depending on what part of the file you are editing. Also you can give an argument to the
:Rcommand, this way you can edit files relative to the application root. Also it is possible to open the files in new tab and splits. Experiment with the options and the various file types and in little time you will find these commands really useful. If you want to get back to the previous file press Ctrl-^.
Go to the part where it reads article.title. If your cursor is on the word article, the command gf will send you to the file /app/models/articles.rb. If you press gf on any of the functions, the corresponding controller will be opened. Ctrl-^ will take you back to the previous file . You can also navigate by tags,
takes you to the first tag named new. If you are not happy you can specify the exact tag by issuing
:tselect. If you really love regular expressions , you can specify the tag that you want to visit by using them.
4. The joys of auto-completion
Many users first question is wether an editor supports autocompletion. The answer is yes , in Vim if you press Ctrl-X Ctrl-U a list will open with all possible identifiers. You only need to pick one. You don’t need to set anything in .vimrc , because the developers already included a nice user function for autocompletion. There are a bunch of plug-ins and editing strategies that makes editing Ruby on Rails code easier, and there are many more features of Rails.vim , but I believe , that these are the essential commands, and after reading this article you can start with a bigger project, and you will feel the productivity gain . Also, you don’t need to remember all the commands, but one by one these could be mastered in a short time frame.
Basic Tutorial in Vim Complete
I hope that you found this article useful. Suggestions are welcomed.