Mostrando entradas con la etiqueta vim. Mostrar todas las entradas
Mostrando entradas con la etiqueta vim. Mostrar todas las entradas

viernes, 1 de abril de 2011

My Vim configuration. Part 4 - Vim as an IDE

Today I'm going to finish my Vim configuration explanation (Have a look at the previous articles). The final part of my configuration is related to the "IDE behaviour" in Vim.

Encoding
First of all, I have set my encoding to utf-8 with:
set encoding=utf-8

Indentation
I prefer spaces to tabs, so this is how I configure it in Vim.
filetype indent on
set tabstop=2
set softtabstop=2
set smarttab
set expandtab
set autoindent

Line numbers
If you want to see the line numbers in Vim you only need to do:
set number

Backspace
Default backspace behaves in a strange way :P I've changed it so it behaves in a more "normal" way:
set backspace=indent,eol,start

Search
One of the most important features in Vim is searching. I've configured mine so it is case insensitive (except if what I search has an uppercase character, then it is case sensitive)
set ignorecase
set smartcase

I also want Vim to highlight my search terms:
set incsearch
set hlsearch

And I have a shortcut to remove the highlighting:
nnoremap <leader><space> :noh<cr>

There are some more little things that can help you in making a more "personal" Vim, but I'm going to stop here :) Feel free to have a look to my vimrc file or, you know, google it :P

Thanks for reading :)

viernes, 11 de marzo de 2011

My Vim configuration. Part 3 - Vim shortcuts

Continuing with my explanation of my Vim configuration (you can have a look at my view settings and my plugin settings), today is the day of the shortcuts :)

Everyone of us love the shortcuts our favorite IDE has. Vim is no less and you can define whatever shortcut you need :)

How to define a shortcut in Vim
It is very easy to define a shortcut in Vim, you only need to map the key combination you one to use with the actual command you want to execute.
To map a combination you can use map or noremap (and all the variants for each mode, like imap, inoremap, nmap, nnoremap, etc...). The difference between map and noremap is that the second one is not recursive. You have a very good explanation here.
You can see an example of a shortcut definition:

This shortcut closes Vim without saving the file and can be executed by pressing the keys 'c', 'l', 'o', 's' and 'e'

Leader
The previous example is more or less a "hard code" shortcut :) A better way to map commands is using the leader variable, so you start your shortcut by pressing your leader combination.
To define your leader combination you only need to do something like this:

In this case, my leader combination is just the ',' key :)

It is important to define your leader combination because a lot of the existing plugins for Vim use it for their own shortcuts (mappings).

My shortcuts
Ok, now that we have defined the leader, let's have a look to my actual shortcuts.
The first one I want to show you is how to change between the actual buffer and the alternative buffer (If you don't know anything about Vim buffers, take a look at this video, it is great!)

What I'm doing here is mapping ',' and '6' to ':b#' (:b# is what you have to type to switch buffers when in normal mode. means that, after the command, I want to press 'return', executing the command). I'm using nnoremap for this shortcut because I want to use it only in normal mode (the first n of nnoremap) and because if some of my plugins map any ':', 'b' or '#' key to any command I don't want to execute that shortcut (not recursive), I just want to execute ':b#'.

The next shortcuts are related to some plugins I use:

I just map the NERD tree to ',d' and a little bit of help every time I want to use ack (only in normal mode) :P

Then, I have two shortcut for removing the extra spaces (at the end of the line) and to change tabs with 2 spaces :)

I also have a shortcut that changes the ruby hashes (with a symbol as key) from 1.8 syntax to 1.9 syntax:

And last, but not least, I map the tab as a match bracket finder:

I probably change all of this shortcuts in the future, when I get more experience with Vim, but I hope it shows you how powerful (and customizable) Vim is :)

PS: I know that the gist are not visible on google reader. Sorry about that! I want to move my blog and fix that :)

domingo, 6 de marzo de 2011

My Vim configuration. Part 2 - Vim plugins

I was supposed to talk about shortcuts in Vim, but I've changed my mind :P I'm going to talk about how to install plugins for Vim :)

Vim plugins
Vim has a lot of plugins, a lot :D So, how do I choose the plugins I need? Well, fist of all, you have to know what you need. This is very important, you shouldn't use a plugin if you're not sure about why you need it!

Once you've decided which plugins do you want you only need to install them!

How to install a vim plugin
Vim plugins are just scripts and to install them you only need to copy the script file in the right directory. Ok, this looks very similar to the way we add color schemes to our Vim, doesn't it? Yes, the only thing we need to change is the directory. When we install a plugin, we have to add the script file to the 'plugin' (or 'ftplugin' if it is a file-type-dependent script) directory in our .vim directory. Sooo easy :D

Yes, so easy. But, do I have to do that every time I get a new plugin or every time I update an old one? Cannot this process be automated? Yes, it can! There are a lot of tools to manage your plugin dependencies. I am using Vundle right now.

Vundle
Vundle is a plugin management tool for vim. It is very easy to set up and it is very easy to use. You only need to follow their instructions to "install" it. Once you have everything in its right place, you have to add those two lines to your .vimrc file:

This makes bundle available while you're using Vim :D

Vim plugins
Ok, so, how do I install plugins with Vundle? You only need to add the plugins you want to your .vimrc file (with the Vundler format). I have those:

As you can see, you can ask for the plugin by its "official" name or you can linked it to its github repository.

Once you've defined the plugins you want you only need to type:

on Vim to install or update your plugins :D

Ok, that's all for today. Soon on your screens "How to create shortcuts with Vim" (Although maybe I'll talk a little bit about why I choose those plugins)

domingo, 27 de febrero de 2011

My Vim configuration. Part 1 - View settings

Lately, I've been trying to understand better how Vim works, so I've started looking at its configuration files. I forked Chris's configuration (that was the easy part :D ) and I removed everything I'm not using at the moment. You can see my actual configuration files here.

It was not easy for me to understand how everything works, that's why I'm going to write about it :D Let's start from the beginning, the vimrc file.

.vimrc file
You can find an explanation about the .vimrc file here. Basically, this file is loaded just before entering Vim and it contains our personal Vim configuration settings.
I'll try to explain every line of my vimrc file during the week but, today, I'm going to show you how to configure the "view" part (colors, syntax highlighting, status line, etc).

My Vim view settings


Now, we have a nice Vim window much more friendly than the initial one :D The only problem is that the color scheme is just the default one (which is not very good).

Adding color schemes to Vim
To add a color scheme to vim we first need to get one :D There are a lot of them. I'm actually using tir_black, but I have to try some more :D

Ok, you've selected one color scheme, how does Vim know about it? First of all, you need to create a .vim/colors directory in your home directory. Then, you have to copy the color scheme vim file to that directory.
That's all! Now, you can go to Vim and type


The only problem with this is that it is not a permanent change. Every time you close Vim, it will lose your color scheme preference.
What can we do about it? Easy, we just need to open our .vimrc file and add the following line:


Now, Vim knows our preferences :D

Ok, that's all for today. Soon on your screens "How to create shortcuts with Vim"

jueves, 24 de febrero de 2011

Vim macros

I'm a total noob with vim, so I'm going to start a very basic series of blog posts writing down everything I learn :) Those are going to be very short posts explaining a vim feature. So, let's get started.

Today, I've been working with Tooky the whole day and he has taught me how macros work in Vim.
I can only imagine how powerful they are :D We've used one to rename a method (including everyplace it was called)

How to record a macro
First of all, we need to be on command mode. Then, we have to press 'q' and the key to store the macro ('qa' will store the macro in the key 'a'). We'll see the word 'recording' on the last line.

Everything we do then will be recorded (And I mean everything and on any mode).

To stop recording the macro, all we need to do is press 'q' on command mode.

How to execute a macro
All we have to do is to press 'q' and the key where we stored the macro (on command mode). To execute a macro stored in the key 'a' we have to press '@a'.

Recursive macros
We can make a recursive macro by recording a '@<key that stores the macro>'

Hope you find it useful :)