You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Vim is an extremely powerful text editor, especially for developers. Here, I'll share some of the tips I use working with it on Rails projects. This is not pretended to be a guide about how to use vim, but just to show my most common configuration that I find useful.
No Compatible
Vim is, by default, compatible with vi (the text editor which vim is based in). This is good if you work a lot with both applications because you'll want to have the same experience as user, but the cost of this compatibility is you lose all new features vim supports and vi doesn't. So, since I only use vim, I disable the vi compatibility.
setnocompatible
Update: Vim 8 is no compatible by default, so this option is not needed anymore.
Syntax highlight
Another useful feature of all modern (and not so modern) text editors is syntax highlighting. Vim takes the file extension to guess what language is the file of, and highlight the code correctly. However, this feature is not enable by default, you have to enable it manually adding the following line to your .vimrc.
syntaxon
Indent
Indenting code is a basic task as developer. Vim can help us doing this automatically, adding indentation when needing, for example, after a if statement, and reducing it after the block ends. It does not only help us indenting code while we write it, but also we can re-indent blocks of code with '='. For example, hiting == in normal mode, will indent the current line. If you hit 27== the next 27 lines will be automatically indented. Obviously, you can indent all the file: gg=G (be careful if your file is too long).
However, automatic indentation is not enable by default, you have to add this line to your .vimrc.
filetypepluginindenton
We also need to set shiftwith in order to get the indentation working with a given number of spaces. If you don't configure this, vim uses tabs of 8 spaces. If you want to have the same number of spaces when hitting tab key, add tabstop to the config file.
setshiftwidth=2settabstop=2
Also, it is possible to tell vim to convert tabs into spaces automatically, with the expandtab option.
setexpandtab
Linenumbers
Vim can show the linenumbers of the file.
setnumber
This option shows the number of each line on the left. The numbers starts at 1 in the first line, 2 in the second one, etc.
setrelativenumber
This option shows the number of each line on the left too. The difference with the previous one is that the number is relative to the current one. The current line is 0, the lines before are 1, 2, 3, etc, and the lines after are 1, 2, 3, etc. This kind of numbering is specially usefull in vim because you can move faster through the classic number-motion, for example 12j, 3k, etc.
In my experience, this option is very usefull but it slows down vim a while, so try it before you include in your .vimrc
setruler
This option shows the number of the current line and the number of the current column at the bottom of the file.
Once our basic vim configuration is done, let add some plugins that will make our code monkey work a little easier. I won't cover all the features that these plugins offer but just a basic introduction. If you want to know all the stuffs it brings (I highly recommend it), check the projects' documentation.
Pathogen
The first thing I do is installing pathogen, which will be responsible for installing all future plugins I want to have in vim. It is fine if you don't install it, but then you have to install all these other plugins by hand (what is not difficult but a repetitive task I want to avoid).
If you want to access the documentation of the installed plugins inside of vim, you have to run this command inside vim:
:Helptags
The ~/.vim/bundle directory
If the plugin has a git repo, its installation is as simple as cloning it into the ~/.vim/bundle directory.
cd ~/.vim/bundle
This is also configurable, red the pathogen doc for more information. If you have not installed pathogen, read the plugin documentation to see what you need to do to get it installed.
Vim ruby
Vim supports ruby language out of the box, but some caveats can appear when doing specific tasks, especially auto indenting some ruby specific structures, like nested hashes and arrays, or highlighting some special keywords.
vim-ruby plugin offers a better support for ruby language in those cases. Also, it adds some useful autocompletion features and allows you to customize its behavior.
NERDtree plugin adds a navigation tree on the left of the editor. It allows to navigate and open files from there, but also allows to create, move, delete or remove files and directories easily.
You can read the nerdtree doc from vim once the plugin is installed, but if you prefer to read it before you install it, check it online.
vim-rails is a powerful pluglin that allows us to use some shortcuts when using vim on a rails project. For example, instead of typing :edit app/models/user, you can open this file just by typing :Rmodel user. From there, you can change to users_controller just typing :Rcontroller. It also provides some shortcuts to open files on a split window, new tab, etc.
If you want to add coffeescript support to vim, you can use this plugin. It adds color syntax highlighting and indentation support for this language... well, indentation support as far as an indentation based language permits. The plugin is very usefull even (or specially) if you are not working on a rails project, because it adds some commands to compile the coofeescript to javascript.
I find the vim-surround plugin very useful. It allows as to add, remove or change surroundings. For example, changing single quotes for double quotes or square brackets for curly brackets, or adding a html tag, etc. I use this plugin combined with vim-repeat plugin, which allows to repeat the last surrounding action.
One of the most important tool in my daily work is git. I used to work with it on console until I discovered vim-figitive, a wonderful vim plugin that integrates most of git workflow into vim. For example, you can type :Gstatus from vim and a buffer is opened to show what files has been changed, added or deleted since the last commit, and from there, you can select what files will be included in the next commit.
For more information, check the README of the project, especially, see the vimcasts.
Sometimes, in order to improve the code readability, we would want to align it in columns. For example, a set of assignments, a multiple line hash, or a set of example columns in cucumber. Tabular help us with this task. For example, you can select the lines to align (visual mode) and execute tabular :Tabular /=> It will align all the hash rockets on the selected lines. Obviously, you can change the characters you want to align by just changing the characters after the slash /.
Most of times, vim-rails will be enough for finding the file we want to open. :Rmodel, :Rcontroller, :Rview and so on. But vim-rails just work within a rails project, and if we are working in a no-rails project, like an engine, a gem, or another kind of project, we need to find files easily. One usefull plugin is ctrlp.vim. It allow us to find a file in the tree. It list the files that match with the name we write in the prompt, allowing us to select which one we want to open.
In vim, you can use % in normal mode to jump between pairs of parentheses (), {}, <>, []. matchit is a plugin that extends this behavior and allows to match more complex pairs of keywords like def-end, if-else-end, do-end, etc. Also, it works with pairs of HTML tags, which is specially usefull.
matchit is included in vim but it is not enabled by default, you have to enable it manually. The easiest way to do that is adding the following line to the vimrc
runtime macros/matchit.vim
or, the pathogen way, cloning it into the ~/.vim/bundle
To align the Ruby access modifiers (private, public, protected) to the class definition level, add this line to your .vimrc.
letg:ruby_indent_access_modifier_style='outdent'
Highlight Search
Vim can highlight search matches. You can enable that feature with :set hls in command mode, or adding the line to your .vimrc.
sethls
There is a problem with hls. It highlights the search matches and keeps it hihglighted all the time (until you search something else). Obviously, you can disable it with :set nohls, but then, next time you do a search, you have to enable it again. That is not very comfortable. Fortunately, it is possible to hide the current highlights while keeping hls enable.
:noh
Highlight Search
I use pry to debug in ruby, but sometimes I forget to remove the debugging line before commit it. This configuration highligth it and prevent me to commit it accidentaly.
Vim's ecosystem is huge. I just showed the plugin I use most, but there are a lot of them I didn't cover. Here are others I find useful although they are not in my primary list.
Gist
Github allows us to create and share gists (simple pieces of code). With gist-vim we can do that inside vim. Within an open file, in command mode, just type :Gist and it will automatically create a gist with the content of the file, or with a portion of this content if we selected a text previously. The plugin and its dependency.
Markdown is a simple markup language extesively used in the Ruby world. This text, for example, is written using markdown. To add the syntax to vim, add this repo to ~/.vim/bundle
This is only the engine, you have to add your own snippets for your languaje, in this case, ruby. It is really easy to do, but also there are some repos with some useful ones, for example, vim-snippets.
Finally, a minor feature that vim supports but what I can't live without: color schemes. Most of these schemes work only for graphic vim, like gvim, or qvim in my case. If you are in a text console, use this command to support 256 colors.
export TERM='xterm-256color'
Some (many) of the themes will not show properly because they need more than 256 colors palette, but it is possible to get an approximate color schema with this plugin.
This is, probably, the main pack of colors for vim. It includes a lot of color schemes, grouped by light, dark and others. It also adds a menu to select it.
Another useful repo with more color schemes. Be carefull if you install both (color sample pack and this one) because when listed, the color schemas fill up the screen. You will need a Full HD screen for that :)