Improve your settings of your Vim editor

Posted by Gilson Filho on seg 05 março 2012


I have on github repository with my settings for editor vim. And these days look witch the .vimrc was very noisy, full of customizations that need not be there. With that, I started from scratch customization, refactoring and putting my changes into place.

And in this post I want to teach some changes I made in the configuration files, which improved the readability and put the settings editor and plugins in the right places.

1 - Set your customizations terminal mode and graphics mode in place

The vim support two configuration files: . vimrc and . gvimrc . For those who do not know, .vimrc is where the program reads your settings and will be used in  terminal mode as in graphics mode, and .gvimrc have the same role, but in the graphical environment. The problem is that unless you explicitly put certain settings is only for the graphical environment and enter only the .vimrc, for example, situations that can happen if you use the editor in terminal mode, mistakes happen or even "break" the preview the edited file. Thus, to avoid this problem and also remove what should not be, in certain configurations in_ .gvimrc._ An example may be the colorscheme to be used. We use a terminal mode type, and other graphics:

``` vim .vimrc set background=dark colorscheme theme_for_mode_terminal

``` vim .gvimrc
set background=light
colorscheme other_theme

But then you ask me: Gilson, but you can set in .vimrc a condition that should only run when in graphics mode. Thus:

``` vim .vimrc if has("gui_running") colorscheme theme_for_gui set fontgui=Ubuntu\ Mono\ 12

And I say: Yes, you can do this. But he **really** **needs** to be in that file? We have another configuration file for it. In short, what we gain by that?

  * File more readable;

  * Avoid potential problems in using the editor in terminal mode and / or graphic;

## 2 - Define properties for each type of file in _ftdetect_

In the documentation editor, he recommends creating files in the folder _ftdetect_ to define properties that will be used in certain types of files. If you use vim to edit files with the extension *.py, *.mkd (Markdown) and *.html, for example, you can create three files in the folder with their name on the file type. So is this: _python.vim,_ and _markdown.vim html.vim._ I'll make an example:

``` vim ftdetect/python.vim
set ruler

setlocal tabstop=4
setlocal softtabstop=4
setlocal shiftwidth=4
setlocal textwidth=80
setlocal smarttab
setlocal expandtab
setlocal smartindent
setlocal colorcolumn=80

``` vim ftdetect/markdown.vim autocmd BufNewFile,BufRead .markdown,.md,.mdown,.mkd,.mkdn set ft=markdown autocmd BufNewFile,BufRead .markdown,.md,.mdown,.mkd,.mkdn set tabstop=8

``` vim ftdetect/html.vim

autocmd BufNewFile,BufRead *.html *.xhtml colorscheme other_theme
autocmd BufNewFile,BufRead *.html *.xhtml set tabstop=2

As we have seen in other settings of other people, put these definitions there is meaningless, and can put in a file and in the correct folder. Much better is not it? ;)

3 - Insert and update your plugins in a more intelligent

The traditional way (so far always been that way) to get new plugins and scripts in your vim, enter the folder is ftplugin (as most are). Only the @tpope made ​​a plugin that took the catch to install and update plugins in a simpler way, using the power of version control is the pathogen. He is nothing more than a plugin that makes vim "sees" all your extensions in a folder only. And because I said the version control?

In git, for example, have the functionality of submodules which is basically a "symbolic link" which is a repository into another repository. With this, you can upgrade and install your plugins in one place without problems.

4 - Define shortcuts and customization of your plugins folder autoload

I did this refactoring, one of the main things that bothered me is the shortcuts of commands and initial settings of plugins that I created within the .vimrc. Most of their mess was because of this. So I thought and thought, and found a simple solution that could help me (smarter solutions are welcome :) .) What did I do? I created custom shortcuts placed in a file called shortcuts.vim, and put the properties of plugins each with the name of their respective extension autoload folder. Example: shortcuts.vim, surround.vim, etc.. Look what stretch of the file links below:

``` vim autoload/shortcuts.vim

" ============= Mappings =========== {

" ============= Tab navigation ============= {

    noremap <C-S-N> :tabnew <CR>
    inoremap <C-S-N> :tabnew <CR><Insert>

    noremap <S-Right> :tabn<CR>
    inoremap <S-Right> <esc>:tabn<CR><Insert>

    noremap <S-Left> :tabprev<CR>
    inoremap <S-Left> <ESC>tabprev<CR><Insert>

" }

" ============= Shortcut Save (default in others editors) ============= {
    noremap <C-S> :w<CR>
    inoremap <C-S> <ESC>:w<CR><Insert>
    noremap <C-X> <img src="" alt=":x" class="wp-smiley"> <CR>
    noremap <C-C> :y <CR>
" }

" ============= Switch background modes ============= {
    noremap <leader>l :set background=light<CR>
    noremap <leader>d :set background=dark<CR>
" }

" ============= Shortcuts NERDTree ============= {
    noremap <C-B> :NERDTreeToggle<CR>
" }

" ============= Shortcuts Buffers ============= {
    map <A-Left> :bnext <CR>
    map <A-Right> :bprevious <CR>
" }

" ============= Autoclose parentheses, brackets and braces.  ============= {
    imap { {}<left>
    imap ( ()<left>
    imap [ []<left>
" }
After creating this file, put in a _command .vimrc_ to load this file:

``` vim .vimrc

let $VIMHOME = $HOME."/.vim"

" (...)

" Importing shortcuts and other configurations
so $VIMHOME/autoload/shortcuts.vim

With this, every file you create in the autoload folder, just set the above command to load the same (of course have to change to the file name you created).

This reshaping witch I did is not a good practices, etc. I did it because I thought the best way to organize my settings. If you liked, do well and share this information. It was curious to see my settings, just access here .

Bye! :)

Comments !