Skip to content

Instantly share code, notes, and snippets.

@wazery
Created May 21, 2014 18:40
Show Gist options
  • Save wazery/1b5f1580783462e8d03f to your computer and use it in GitHub Desktop.
Save wazery/1b5f1580783462e8d03f to your computer and use it in GitHub Desktop.
Welcome document
Vim for Rails
=====================
----------
## Contents
- Introduction to Vim
- Setting up your Vim
- Vim themes
- Editing .vimrc config
- Vim modes
- Vim commands
- Vim recording
- Killer commands
- Some Productivity Tips
- Source Navigation
- Vim Ruby Plugin
- Installation
- Usage/commands
- Rails.vim Plugin
- Basics
- File Navigation
- Timesaving commands
- Executing/Running migrations/scaffolds
- Ruby Refactoring Plugin
- Vim Cucumber
- Other useful Ruby/Rails plugins
- Vim Rake
- Vim Bundler
- CTags
- Indexing your Rails project tags
- Easy jumping to definitions
- Using regex to jump to definitions
- snipMate
- Adding Ruby snippets
- Ack
- Easy searching in your source code
- Editing .ackrc config
- Quick navigation through results using Vim
- tComment
- Easy commenting your Ruby code
- Sync with Dropbox or Github
## Introduction to Vim
### Brief history of Vi and Vim
### Why to use Vim, is Vim a text editor only?
### Vim for Rubyists
### Installing Vim
#### On Ubuntu
If you’re using a GNOME-based desktop such as Unity, you can use vim-gnome which is a GUI version of Vim. To install, type in a terminal
```
sudo apt-get install vim-gnome
```
If you’re running another desktop environment such as XFCE, LXDE, or KDE, you can use vim-gtk which doesn’t depend on as many extra library files as vim-gnome. To install, type in a terminal
```
sudo apt-get install vim-gtk
```
> **NOTE:**
> As of Ubuntu 6.10 (Edgy Eft), the default Vim install is a cut-down version called "vim-tiny". In order to get the full version complete with the online help documentation, and many of the advanced functions such as code folding; you will need to install "vim":
```
sudo apt-get install vim
```
#### On MacOSX
In MacOSX the alternatives to apt package manager are `Homebrew` and `MacPorts`. Their is a project called MacVim which is a GUI interface for Vim like GVim on Linux, to install it using Homebrew just type in the terminal:
```
brew install macvim --override-system-vim
```
This will create vim, vimdiff, etc. symlinks to mvim in /usr/local/bin/vim, and as long as /usr/local/bin is before /usr/bin in your PATH. MacVim come with built-in support for the Ruby language which will be useful for us when integrating some plugins like Command-T.
#### On Windows
1. Download Vim: Go to Vim Download page and click on “PC: MS-DOS and MS-Windows”. Click on the ``gvim72.exe``, which is a Self-installing executable, or download ``gvim72.exe`` directly.
2. Installing: After downloading click on gVim72.exe, which will install gVim on your Windows as shown below.
![Vim Installation Confirmation][1]
Vim Installation Confirmation
![Vim 7.2 License Agreement][2]
Vim 7.2 License Agreement
![Vim Installation Option. (Make sure to select “Full” from the drop-down list)][3]
Vim Installation Option. (Make sure to select “Full” from the drop-down list)
![Vim Installation location ( directory should end with vim )][4]
Vim Installation location ( directory should end with vim )
![gVim Installation Progress and Completion][5]
gVim Installation Progress and Completion
#### Setting up your Vim
When Vim starts up it loads a file called vimrc, which holds all the configuration settings that Vim needs. On Unix based systems the file is named .vimrc and is located in the .vim directory in your home, while in Windows it is called _vimrc. The home dirctory is specified by ``$HOME`` which is ``~`` in Unix based systems, while on Windows systems you can find ``$HOME`` by typing ``:echo $HOME`` in Vim. You can simply open your vimrc file by typing ``:e $MYVIMRC`` in Vim.
Type ``:help vimrc`` or for short ``:h vimrc`` in your Vim to have some documentation about it. In this tutorial we will edit the vimrc file a lot to add some plugins and some customizations.
#### Recovering from errors
Some errors in your vimrc may prevent Vim from starting successfully. A reliable way to handle that would be to rename your vimrc file, then edit the renamed file, then give it the correct name. Alternatively, you could start Vim with a command like this (or use "GVim/MacVim" if that is how you run Vim):
> vim -N -u NONE -U NONE
The `-N` starts in "not compatible" mode (that is, with extra Vim features). The NONE argument (must be uppercase) skips initializations and does not read any vimrc file (-u), and does not read any gvimrc file (-U).
#### Using Vundle plugin manager
Vim has a plugin, or script system which can be used to add some more functionality to Vim. These plugins, or scripts are implemented as a simple configuration files which are located in subdirectories inside the `$HOME/.vim` directory. If you are using a lot of plugins, managing them would add some hassle, so here it comes the importance of a plugin manager to take care of them effectively and help make this task simpler.
There are some plugin managers for Vim like [Vundle][6] and [Pathogen][7], which make it super easy to add, install, update, search, or remove any plugin or runtime file you want each of which in their own private subdirectories. In this tutorial we will be exploring how to use Vundle.
Vundle is a short for `Vim Bundle`, and here is how to install and use it to manage your Vim plugins.
To install it you need to have [Git][8] first. [Here][9] is a guide on how to install it.
**1-** In your terminal type:
```
$ git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
```
This command will clone the Vundle plugin into the correct location.
**2-** Open up your vimrc file simply by typing `:e $MYVIMRC` in your vim.
**3- Including Vundle in your vimrc**
Put this at the top of your `.vimrc` to use Vundle. Remove plugins you don't need, they are for illustration purposes.
```
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
```
**4- Adding the plugins:**
Now you can add any plugin you want from just by adding the word Plugin and the url for it or its Git repository (it doesn't matter if the repository is on Github or any other server or even on your machine just add the path to it). As an example to add the [vim-fugitive][10] plugin you will add the following line inside `call vundle#begin()` and `call vundle#end()` block.
```
Plugin 'tpope/vim-fugitive.git'
```
> **NOTE:**
> We didn't add the full git URL which is '[email protected]:tpope/vim-fugitive.git', because Vundle will automatically do that for us if the repository is hosted on Github, so you will add the full path only if the repository is hosted somewhere else.
**5- Installing the plugins**
To install the plugins you already included you need to launch vim and run `:PluginInstall` or to install from command line just type:
```
$ vim +PluginInstall +qall
```
#### Beginning Vim user should be able to:
- Move to beginning and end of a file.
- Enter and exit insert mode.
- Exit.
- Append text.
- Save.
- Combine operators (like delete) with motions (like end of word).
- Operate on lines.
- Undo.
- Search forward and backward.
- Match parentheses and brackets.
- Substitute.
- Execute a shell command.
- Copy and paste.
- Tab complete.
- Split horizontally and vertically.
- Jump forward and backward.
- Modify your environment via dotfiles.
> **Source:** Thoughtbot's [Vim checklist][11]
Some Tips
- Copy to system clipboard
- Remap your CapsLock key to Control
### Learning Resources
- http://vim-adventures.com/
- http://www.viemu.com/vi-vim-cheat-sheet.gif
vim replacement icons
Vundle
> **NOTE:**
>
> - StackEdit is accessible offline after the application has been loaded for the first time.
> - Your local documents are not shared between different browsers or computers.
> - Clearing your browser's data may **delete all your local documents!** Make sure your documents are backed up using **Google Drive** or **Dropbox** synchronization (see [<i class="icon-share"></i> Synchronization](#synchronization) section).
>
#### <i class="icon-folder-open"></i> Switch to another document
You can list all your local documents and switch from one to another by clicking the <i class="icon-folder-open"></i> button in the navigation bar.
#### <i class="icon-pencil"></i> Rename a document
You can rename the current document by clicking the document title in the navigation bar.
#### <i class="icon-trash"></i> Delete a document
You can delete the current document by clicking the <i class="icon-trash"></i> button in the navigation bar.
#### <i class="icon-hdd"></i> Save a document
You can save the current document to a file using the <i class="icon-hdd"></i> `Save as...` sub-menu from the <i class="icon-provider-stackedit"></i> menu.
> **Tip:** See [<i class="icon-share"></i> Publish a document](#publish-a-document) section for a description of the different output formats.
----------
Synchronization
---------------
**StackEdit** can be combined with **Google Drive** and **Dropbox** to have your documents centralized in the *Cloud*. The synchronization mechanism will take care of uploading your modifications or downloading the latest version of your documents.
> **NOTE:**
>
> - Full access to **Google Drive** or **Dropbox** is required to be able to import any document in StackEdit.
> - Imported documents are downloaded in your browser and are not transmitted to a server.
> - If you experience problems exporting documents to Google Drive, check and optionally disable browser extensions, such as Disconnect.
#### <i class="icon-download"></i> Import a document
You can import a document from the *Cloud* by going to the <i class="icon-provider-gdrive"></i> `Google Drive` or the <i class="icon-provider-dropbox"></i> `Dropbox` sub-menu and by clicking `Import from...`. Once imported, your document will be automatically synchronized with the **Google Drive** / **Dropbox** file.
#### <i class="icon-upload"></i> Export a document
You can export any document by going to the <i class="icon-provider-gdrive"></i> `Google Drive` or the <i class="icon-provider-dropbox"></i> `Dropbox` sub-menu and by clicking `Export to...`. Even if your document is already synchronized with **Google Drive** or **Dropbox**, you can export it to a another location. **StackEdit** can synchronize one document with multiple locations.
> **Tip:** Using **Google Drive**, you can create collaborative documents to work in real time with other users. Just check the box `Create a real time collaborative document` in the dialog options when exporting to Google Drive.
#### <i class="icon-refresh"></i> Synchronize a document
Once your document is linked to a **Google Drive** or a **Dropbox** file, **StackEdit** will periodically (every 3 minutes) synchronize it by downloading/uploading any modification. Any conflict will be detected, and a local copy of your document will be created as a backup if necessary.
If you just have modified your document and you want to force the synchronization, click the <i class="icon-refresh"></i> button in the navigation bar.
> **NOTE:** The <i class="icon-refresh"></i> button is disabled when you have no document to synchronize.
#### <i class="icon-refresh"></i> Manage document synchronization
Since one document can be synchronized with multiple locations, you can list and manage synchronized locations by clicking <i class="icon-refresh"></i> `Manage synchronization` in the <i class="icon-provider-stackedit"></i> menu. This will open a dialog box allowing you to add or remove synchronization links that are associated to your document.
> **NOTE:** If you delete the file from **Google Drive** or from **Dropbox**, the document will no longer be synchronized with that location.
----------
Publication
-----------
Once you are happy with your document, you can publish it on different websites directly from **StackEdit**. As for now, **StackEdit** can publish on **Blogger**, **Dropbox**, **Gist**, **GitHub**, **Google Drive**, **Tumblr**, **WordPress** and on any SSH server.
#### <i class="icon-share"></i> Publish a document
You can publish your document by going to the <i class="icon-share"></i> `Publish on` sub-menu and by choosing a website. In the dialog box, you can choose the publication format:
- Markdown, to publish the Markdown text on a website that can interpret it (**GitHub** for instance),
- HTML, to publish the document converted into HTML (on a blog for instance),
- Template, to have a full control of the output.
> **NOTE:** The default template is a simple webpage wrapping your document in HTML format. You can customize it in the `Services` tab of the <i class="icon-cog"></i> `Settings` dialog.
#### <i class="icon-share"></i> Update a publication
After publishing, **StackEdit** will keep your document linked to that publish location so that you can update it easily. Once you have modified your document and you want to update your publication, click on the <i class="icon-share"></i> button in the navigation bar.
> **NOTE:** The <i class="icon-share"></i> button is disabled when the document has not been published yet.
#### <i class="icon-share"></i> Manage document publication
Since one document can be published on multiple locations, you can list and manage publish locations by clicking <i class="icon-share"></i> `Manage publication` in the <i class="icon-provider-stackedit"></i> menu. This will open a dialog box allowing you to remove publication links that are associated to your document.
> **NOTE:** In some cases, if the file has been removed from the website or the blog, the document will no longer be published on that location.
----------
Markdown Extra
--------------
**StackEdit** supports **Markdown Extra**, which extends **Markdown** syntax with some nice features.
> **Tip:** You can disable any **Markdown Extra** feature in the `Extensions` tab of the <i class="icon-cog"></i> `Settings` dialog.
### Tables
**Markdown Extra** has a special syntax for tables:
Item | Value
--------- | -----
Computer | 1600 USD
Phone | 12 USD
Pipe | 1 USD
You can specify column alignment with one or two colons:
| Item | Value | Qty |
| :-------- | --------:| :--: |
| Computer | 1600 USD | 5 |
| Phone | 12 USD | 12 |
| Pipe | 1 USD | 234 |
### Definition Lists
**Markdown Extra** has a special syntax for definition lists too:
Term 1
Term 2
: Definition A
: Definition B
Term 3
: Definition C
: Definition D
> part of definition D
### Fenced code blocks
GitHub's fenced code blocks[^gfm] are also supported with **Prettify** syntax highlighting:
```
// Foo
var bar = 0;
```
> **Tip:** To use **Highlight.js** instead of **Prettify**, just configure the `Markdown Extra` extension in the <i class="icon-cog"></i> `Settings` dialog.
### Footnotes
You can create footnotes like this[^footnote].
[^footnote]: Here is the *text* of the **footnote**.
### SmartyPants
SmartyPants converts ASCII punctuation characters into "smart" typographic punctuation HTML entities. For example:
| | ASCII | HTML |
------------------|------------------------------------------|-------------------------------------
| Single backticks | `'Isn't this fun?'` | &#8216;Isn&#8217;t this fun?&#8217; |
| Quotes | `"Isn't this fun?"` | &#8220;Isn&#8217;t this fun?&#8221; |
| Dashes | `-- is an en-dash and --- is an em-dash` | &#8211; is an en-dash and &#8212; is an em-dash |
### Table of contents
You can insert a table of contents using the marker `[TOC]`:
[TOC]
### Comments
Usually, comments in Markdown are just standard HTML comments. <!-- like this -->
**StackEdit** extends HTML comments in order to produce useful, highlighted comments in the preview but not in your exported documents. <!--- This is very useful for collecting feedback in a collaborative document. -->
### MathJax
You can render *LaTeX* mathematical expressions using **MathJax**, as on [math.stackexchange.com][12]:
The *Gamma function* satisfying $\Gamma(n) = (n-1)!\quad\forall
n\in\mathbb N$ is via the Euler integral
$$
\Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,.
$$
> **Tip:** Make sure you include MathJax into your publications to render mathematical expression correctly. Your page/template should include something like:
```
<script type="text/javascript" src="https://stackedit.io/libs/MathJax/MathJax.js?config=TeX-AMS_HTML"></script>
```
> **NOTE:** You can find more information:
>
> - about **Markdown** syntax [here][13],
> - about **Markdown Extra** extension [here][14],
> - about **LaTeX** mathematical expressions [here][15],
> - about **Prettify** syntax highlighting [here][16],
> - about **Highlight.js** syntax highlighting [here][17].
[^stackedit]: [StackEdit](https://stackedit.io/) is a full-featured, open-source Markdown editor based on PageDown, the Markdown library used by Stack Overflow and the other Stack Exchange sites.
[^gfm]: **GitHub Flavored Markdown** (GFM) is supported by StackEdit.
[1]: http://static.thegeekstuff.com/wp-content/uploads/2009/12/gvim-setup-confirmation.PNG
[2]: http://static.thegeekstuff.com/wp-content/uploads/2009/12/gvim-license-agreement.PNG
[3]: http://static.thegeekstuff.com/wp-content/uploads/2009/12/gvim-installation-options.PNG
[4]: http://static.thegeekstuff.com/wp-content/uploads/2009/12/gvim-installation-location.PNG
[5]: http://static.thegeekstuff.com/wp-content/uploads/2009/12/gvim-installation-complete.PNG
[6]: https://github.com/gmarik/Vundle.vim
[7]: http://www.vim.org/scripts/script.php?script_id=2332
[8]: http://git-scm.com/
[9]: http://git-scm.com/book/en/Getting-Started-Installing-Git
[10]: https://github.com/tpope/vim-fugitive
[11]: https://learn.thoughtbot.com/vim
[12]: http://math.stackexchange.com/
[13]: http://daringfireball.net/projects/markdown/syntax "Markdown"
[14]: https://github.com/jmcmanus/pagedown-extra "Pagedown Extra"
[15]: http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference
[16]: https://code.google.com/p/google-code-prettify/
[17]: http://highlightjs.org/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment