Skip to content

Instantly share code, notes, and snippets.

@apux
Last active March 3, 2017 16:16
Show Gist options
  • Save apux/3614200 to your computer and use it in GitHub Desktop.
Save apux/3614200 to your computer and use it in GitHub Desktop.
List of github repositories for vim

Configuring Vim to work with Rails

Basic configuration

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.

set nocompatible

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.

syntax on

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.

filetype plugin indent on

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.

set shiftwidth=2
set tabstop=2

Also, it is possible to tell vim to convert tabs into spaces automatically, with the expandtab option.

set expandtab

Linenumbers

Vim can show the linenumbers of the file.

set number

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.

set relativenumber

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

set ruler

This option shows the number of the current line and the number of the current column at the bottom of the file.

Current ~/.vimrc

So far, our .vimrc file should look like this

set nocompatible
syntax on
filetype plugin indent on
set shiftwidth=2
set tabstop=2
set expandtab
set ruler

Plugins

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).

mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

Once installed, you need to enable it in your ~/.vimrc. Add this line to the top of your .vimrc.

call pathogen#infect()

How does our .vimrc file look now?

call pathogen#infect()
set nocompatible
syntax on
filetype plugin indent on
set shiftwidth=2
set tabstop=2
set expandtab
set ruler

Now, with pathogen installed, we can add more plugins easily.

More information about pathogen in its doc.

Help tags

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.

You can read the doc.

Install it (in ~/.vim/bundle)

git clone https://github.com/vim-ruby/vim-ruby.git

NERDTree

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.

Install it (in ~/.vim/bundle)

git clone https://github.com/scrooloose/nerdtree.git

Vim Rails

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.

The documentation is very complete, read it.

Install it (in ~/.vim/bundle)

git clone https://github.com/tpope/vim-rails.git

Coffeescript

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.

Check the readme.

Install it (~/.vim/bundle)

git clone https://github.com/kchmck/vim-coffee-script.git

Surrounder

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.

Read the doc.

Install it (~/.vim/bundle)

git clone https://github.com/tpope/vim-surround.git
git clone https://github.com/tpope/vim-repeat.git

Git

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.

Install it (~/.vim/bundle)

git clone https://github.com/tpope/vim-fugitive.git

Tabular

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 /.

For more information, check the doc.

Install it (~/.vim/bundle)

git clone https://github.com/godlygeek/tabular.git

Search files

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.

Install it (~/.vim/bundle)

git clone https://github.com/kien/ctrlp.vim.git

Matchit

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

git clone https://github.com/tsaleh/vim-matchit.git

Indentation of Ruby access modifiers

To align the Ruby access modifiers (private, public, protected) to the class definition level, add this line to your .vimrc.

let g: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.

set hls

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.

au BufEnter *.rb syn match error contained "\<binding.pry\>"

More plugins

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.

git clone https://github.com/mattn/webapi-vim.git
git clone https://github.com/mattn/gist-vim.git

Markdown

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

git clone https://github.com/gabrielelana/vim-markdown.git

Snippets

Some snips to autocomplete code and its dependencies

git clone https://github.com/tomtom/tlib_vim.git
git clone https://github.com/MarcWeber/vim-addon-mw-utils.git
git clone https://github.com/garbas/vim-snipmate.git

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.

git clone https://github.com/honza/vim-snippets.git

The snippets for ruby are here. You can see it includes snippets for ruby, rspec, shoulda among others. There are some snippets for haml and erb.

Some customization in our forked repo:

git clone https://github.com/LogicalBricks/vim-snippets.git

Color schemes

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.

http://www.vim.org/scripts/script.php?script_id=2390

The github repo that I found updated is:

git clone https://github.com/godlygeek/csapprox.git

Color Sampler Pack

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.

http://www.vim.org/scripts/script.php?script_id=625

Download it and unpack into ~/.vim/bundle if you have pathogen, or in ~/.vim/ directly.

If you prefer, there is a mirror in github

git clone https://github.com/vim-scripts/Colour-Sampler-Pack.git

Vim colors

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 :)

Install it (in ~/.vim/bundle)

git clone https://github.com/dandorman/vim-colors.git

This pack includes the codeschool theme, one of my favorites.

Solarized

Solarized is available not just for vim but for many other text editors. You have heard about it for sure. It is very popular and I really like it.

git clone https://github.com/altercation/vim-colors-solarized.git

More themes

More themes I use:

git clone https://github.com/vim-scripts/Guardian.git
git clone https://github.com/cdmedia/itg_flat_vim.git
git clone https://github.com/morhetz/gruvbox.git
git clone https://github.com/farseer90718/flattr.vim.git
git clone https://github.com/blerins/flattown.git
git clone https://github.com/duythinht/vim-coffee.git
git clone https://github.com/gosukiwi/vim-atom-dark.git
git clone https://github.com/chriskempson/vim-tomorrow-theme.git
git clone https://github.com/w0ng/vim-hybrid.git

Bespin

Another pleasant color scheme that I use a lot but I haven't found in a github repo

git clone https://gist.github.com/817896.git

Unpack and copy the bespin.vim file into ~/.vim/colors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment