Tag Archives: Vim

Vim : Anywhere With You

Last time I showed you how to manage your Vim plugins with Vundle. Now I want to give you some ideas how to make your configuration portable. Imagine that you are on another machine (some production server for example) and you want your own Vim. Another thing I want to share is my idea for readable Vundle plugin management. So lets start:

  1. The VundleFile. I don’t want to put my plugins in the .vimrc file, I want to keep it clean. So I created a file called VundleFile (or .VundleFile if you want to be hidden) and put it next to my .vimrc configuration. This file looks like that:

    Bundle 'gmarik/vundle'
    Bundle 'tpope/vim-fugitive'
    Bundle 'The-NERD-tree'

    Clean, isn’t it? But the problem is that this file should be sourced somewhere in the .vimrc configuration. So I modified my .vimrc configuration like this:

    au BufRead,BufNewFile VundleFile set filetype=vim
    if filereadable("VundleFile")
        so VundleFile
    endif

    Now if I have a file called “VundleFile” next to the current (.vimrc) file it is sourced and all the Bundles are taken into account. You can do the same thing for a .VundleFile. The command on the first line tells vim to treat the VundleFile as a vimscript source file (when editing it).

  2. Per-project configuration with Vim.
    Put these two lines in your .vimrc file:

    set exrc
    set secure

    Now if you have a .vimrc file in you current folder (for example your project folder) it will be automatically sourced and used. The secure setting will keep you from harm :). You can push this configuration to your git repository and have it anywhere (but you will have to update all the .vimrc files on all machines to have the two lines from this section… Later I will show you how to have fully portable configuration).

  3. Per-project configuration with Vim + per-project plugins.
    The idea is to not only to have a .vimrc configuration in your project folder but to have a .vim folder with all your plugins in it too. Modify the .vimrc configuration of your project to begin with this:

    set nocp
    
    let s:current = expand('%:p:h')
    while s:current != '/'
        if isdirectory(s:current . '/.vim')
            let s:parent = s:current . '/.vim'
            break
        endif
        let s:current = fnamemodify(s:current, ':h')
    endwhile
    
    let s:clean_path = $VIM . '/vimfiles,' . $VIMRUNTIME . ',' . $VIM . '/vimfiles/after'
    let &runtimepath = s:parent . ',' . s:clean_path . ',' . s:parent . '/after'filetype off
    let &runtimepath = &runtimepath . ',' . s:parent . '/bundle/vundle'
    call vundle#rc(s:parent . '/bundle')

    A lot of magic! The first line tells Vim to be Vim and not Vi. It is important; you will use Vundle. The while statement searches for a .vim folder from the current folder up to your root folder and if it finds such folder it saves it in the s:parrent variable (bad name, I know, I’ll fix it some sunny day…). The following two lines clean up the runtime path of vim to contain only the system paths and the found .vim folder. So if you don’t have dedicated .vim folder the one in your home will be used, isn’t this convenient?
    The last three lines setup Vundle to use your project’s .vim folder. So it is important to install Vundle there.
    Now all your plugins will be loaded and installed from this .vim folder. The plugins installed in the home folder won’t affect this configuration. In other words you have absolutely clean vim configuration for the project.

  4. Per-project configuration without depending on the .vimrc located in the home folder.
    This one is easy. Just start vim manually pointing to your project’s .vimrc.

    vim -u .vimrc
  5. Vim as interpreter for Vimscript.
    If you want to try some vimscript commands in something like interpreter. You should start vim like this:

    vim -nNE

    Here is your interpreter. If you want to use the current project configuration for the interpreter:

    vim -nNE -u .vimrc

    If you want to start the interpreter without configuration:

    vim -nNE -u NORC

This is for today, now you are Vim lover. Try this post using the structure in my github – https://github.com/meddle0x53/portable-vim.

Vim : Welcome To The Vundle

Today we are going to talk about Vim. Again…
Vim is your text editor, Vim is your IDE, Vim is everything you need to develop you masterpiece applications. You are skeptical? It is normal. You are used to your comfortable IDE, you are used to click navigation, auto-complete and beautiful menus, why are you going to leave all of that for the dark black window of Vim? Why you use linux/mac instead of windows, why you use chrome/firefox instead of internet explorer, why you are opening you terminal at all… Because you are developer, you want to have control over the things you do and not vice versa…

Vim is not just a text editor, it can be extended by plugins. And yes I know that you think that means putting strange files at strange places and waiting for the magic to happen after the restart.

Vim has a plugin that does the plugin management for you. It is called Vundle. It has alternatives and I’ll talk about them some sunny day, but for now let’s talk about Vundle.

You can go to the its github site – https://github.com/gmarik/vundle. There you’ll find instructions  for installing and using it. But I’m going to explain some of these instructions to you, so:

  1. Go to your $HOME/.vim folder (cd ~/.vim).
  2. Create a folder called bundle, if it doesn’t already exist (mkdir bundle).
  3. Navigate to the bundle directory (cd bundle).
  4. If you don’t have git, install it (sudo apt-get install git / brew install git). Execute the following command:
    git clone https://github.com/gmarik/vundle.git
  5. Vundle is downloaded. Now edit your vimrc file (vim ~/.vimrc).
     set nocp                       " Vim is not Vim if it is Vi compatible
     filetype off                   " required!
    
     set rtp+=~/.vim/bundle/vundle/
     call vundle#rc()  
     Bundle 'gmarik/vundle' 
     filetype plugin indent on     " required!
  6. Now save the file and exit. Lets examine the vimrc script.
    – The first two line make so that Vim can handle working with Vundle; We should be VIm, not Vi and the FileType event will be turned off for now.
    – The third line adds the Vundle plugin to the runtime path.
    – The fourth line turns on Vundle, by calling the ‘rc’ function defined in the plugin.
    – From here on, we can add bundles using the Bundle command.
    – After all the bundles (plugins) are listed we turn on the FyleType event again. But with this particular command we tell vim to load the plugins located in the ftplugin directories related to the file type of the file we open for editing.
  7. You can add bundles for plugins located at github by adding Bundle ‘<user>/<project>’
  8. You can add bundles in normal no-github repositories by adding Bundle ‘<url>’
  9. You can add the bundles from the https://github.com/vim-scripts repository with
    Bundle ‘<plugin_name>’
  10. :BundleInstall – installs all the Bundles specified in your vimrc that are not already installed.
  11. :BundleInstall! – installs all the Bundles specified in your vimrc that are not already installed and updates these which are installed but can be updated.
  12. :BundleList – lists all the bundles that are installed.
  13. :BundleSearch – can be used to list all the known bundles or to search for bundles using a search phrase.

If you decide to use vim as your primary text/source code editor, you should try Vundle. Later in various posts I’ll use different Vim plugins, that you can easily install using Vundle.

Vim : Fight Power With Control

This is a meta-post. It is filled with vim commands to help you use the editor while playing with the code in the other posts and in the life.

Command Mode

  1. Saving in files:
    :w Saves the current file.
    :w! – Overwrites the current file.
    :w or :w! <file_Path> – Saves/Overwrites to/the <file_path>.
    :wq – A classic – save and exit.
    :h writing – Many other options for these commands, like  :wa (write to all buffers) for example.
  2. Executing shell commands:
    :!<command> – Executes the shell <command> (for example :!ruby %, which executes ruby on the current buffer).
    :!! – Repeats the last executed command.
    :sh – Starts the shell, you can type commands, compile files, etc, after typing exit you will return to Vim.
    :h shell – Detailed help the above commands.
  3. Enter in the Insert Mode (for typing text).
    :i – Enter Insert Mode, insert before the cursor.
    :I – Enter Insert Mode, insert in the beginning of the current line.
    :gi – Enter Insert Mode, from the position the last Insert Mode was exited.
    :o/:O – Insert new line bellow/above the line of the cursor and enter Insert Mode in the beginning of it.
    :a – Enter Insert Mode, insert after the cursor.
    :A Enter Insert Mode, insert at the end of the current line.
    :h insert – Help for the above commands.
    :i comes from insert, :a comes from append.

Normal Mode

  1. Saving files:
    ZZ – Save the current file and exit.
  2. Basic navigation
    h == left
    l == right
    j == down
    k == up
    G == End of the buffer/file
    gg == Beginning of the buffer/file
    0 == The beginning of the current line.
    $ == The end of the current line

Grep & Ack : Searching For The Truth

You are used to work with rich IDEs like Eclipse, or you use powerful file manager like Konqueror and searching in the current directory/project files for a given content is an easy task… But imagine that you are on a remote headless machine and you need to do the same thing there… For example you are searching for an IP address inside all the files and sub-directory files of the /etc folder. How are you going to do this?

  1. Use the grep command. Navigate to the directory you want to search in and execute the following command:
    grep -iRn "<expression-you-are-searching-for>" *

    This will search in all the files in the current directory recursively for the expression and will list the line containing the expression along with the name of the file containing it and the line number.
    – The -i option tells grep to ignore the case of the expression (so if you want case sensitive search, exclude it)
    – The -R option tells the command to search all the files and sub-directories in the passed directory recursively.
    – The -n option tells grep to show the line numbers of the lines matched.
    – The last parameter of the command (‘*’ in the example) is glob, so you can search only in C source files by setting it to ‘*.c’, for example.

  2. If you insist you can use find (again with grep)…

    find . -iname "*.html" -print | xargs grep -i "Some"
  3. You can use ack (brew install ack / sudo apt-get install ack).
    – On some systems the program is called ‘ack-grep’ so you can add this to your .bashrc file : alias ack=ack-grep
    – Here is the command:

    ack --nogroup  -i "<expression>"

    – Here the –nogroup options turns off the grouping of the results by files.
    – The -i option is for ignoring the case.

I personally prefer using ack.

Now if you want to integrate for example the ack search with vim and for example to search for a method use in a rails project using your editor you can add these in your ‘.vimrc’ :

set grepprg=ack\ --nogroup\ --column
set grepformat=%f:%l:%c:%m
  • This will replace the default vim ‘grep’ command with our command.
  • You can create similar configuration with grep, try it.
  • In the grepformat:
    – %f is filename
    – %l is line number
    – %c is column number
    – %m is message or matched content in our case
  • Now if you run Vim and execute :grep “<expression>”, the current working directory will be recursively searched for the files containing the expression using ack.
  • After the search you can execute :copen to be able to browse the search result and navigate in the listed files.
  • After you are done searching/browsing, execute :cclose to close the search results.

So this is it for this post…

Vim : Here, There, Everywhere

This is my first meta-post. A meta-post is post that will be referenced by other posts, it will be filled over the time with the information needed by normal posts.
This one represents you the directory/file structure of the vim configuration.


Let’s talk about the ‘rtp’ or the runtime path. The runtime path contains paths separated by comma. Every such path points to directory that can be structured as your $HOME/.vim. I’m going to explain this structure later in this post. For now it is important for you to understand that when vim starts it loads data and configurations from the directories in the runtime path.
By default this path contains:
– Your $HOME/.vim directory.
– System-wide configuration located in place such as ‘/usr/share/vim/vimfiles’.
– 
Configuration located where vim is installed, for example ‘/usr/share/vim/vim73’.
– System-wide after configuration for example ‘/usr/share/vim/vimfiles/after’.
– Your own $HOME/.vim/after directory.
You can modify this path.

Again – the structure of your $HOME/.vim directory is the same for the system-wide vim directories and the ‘after’ directories.

  1. $HOME/.vimrc(_vimrc) – This file contains an Ex script that will be executed every time you start vim. So your personal configurations are likely to be here. Run :help startup for detailed information.
  2. $HOME/.vim/after – From the Vim help : ‘This is for personal preferences to overrule or add to the distributed defaults or system-wide settings (rarely needed).‘ It has the same structure as the $HOME/.vim directory.
  3. $HOME/.vim/autoload – This directory contains vim scripts. These scripts are not loaded before special functions defined in them aren’t called. This means – lazy scripts. Imagine that a file, named ‘meddle.vim’ is located in the autoload directory. Now if in the file we have a function definition like this:
    function meddle#boo()
        echo "Booooo!"
     endfunction

    We are able to call it from anywhere executing ‘:call meddle#boo’. Only if we call it the script will be loaded and the function executed. Run :help autoload for detailed information. Maybe you’ve heard of the ‘pathogen’ plugin? This plugin uses the auto-load functionality.

  4. $HOME/.vim/bundle – This is not a directory used by Vim by default. It is used by plugin managers, such as Vundle and Pathogen to store and load the plugin bundles. The directory is first introduced by Pathogen.
  5. $HOME/.vim/indent – This folder contains files containing indentation rules. For example if you place a ruby.vim file here with rules for ruby indentations, these rules will be applied to all the filetype ruby files.

Enter The Meddle And Vim (for the nth time)

So, if you have some experience in writing code, administrating an Unix-like OS or you are just a normal human being with some strange and unreasonable interests, you’ve heard of Vim. Every now and then you need to edit a configuration file on a remote server or to commit/merge changes on it, so you know some of the Vim’s magical commands: ‘i’ (insert mode) and ‘<esc>:wq’ (normal mode, save and exit)…

So… How about we make Vim your text editor of choice or even your IDE? Psycho CRUSHER!

This is my first post, I’m laying the grounds of the themes of this blog (one of them is Vim, apparently), so I want to tell you about myself…

I’m Nickolay Tzvetinov, a.k.a The Meddle, I’ve been playing with The Code for a long time, I began the battle with C and its derivative C++ (MFC, Visual Studio, GTK, sadness, despair…), then I dove in the Java world (Heavy IDEs, application servers, EJBs, Swing, pain…), I had my moments of enlightenment (I call them The Python Days) but now I’m free!
I code/read/think in Ruby, not only Rails, but I like Sinatra and the pure Rack, I use Javascript libraries like Ember JS, I’m in love with RSpec from the first moment we’ve met :)
And I found out that Vim is the tool/editor/browser/IDE that I need to do all of this.

I’ll show you how to use Vim to code/edit/merge, maybe I’ll show you how to extend Vim, I’ll show you some tricks with ruby and various libraries.

This is yet another tech blog for web developers, but the difference is that everything will be  presented through the canvas of Vim – magical but easy.

Oh, and I’m ethnic Bulgarian, live in Sofia, play guitar and like the sky.