Vim Tips

From Wiki

Jump to: navigation, search

Contents

My .vimrc

Plugins I use

Clever Tab

Autocompletion with a nicer interface

function! CleverTab()
    if strpart( getline('.'), 0, col('.')-1 ) =~ '^\s*$'
        return "\<Tab>"
    else
        return "\<C-N>"
endfunction

inoremap <Tab> <C-R>=CleverTab()<CR>

DiffOrig

I can't remember where I took this snippet from

Convenient command to see the difference between the current buffer and the file it was loaded from, thus the changes you made. Only define it when not defined already.

if !exists(":DiffOrig")
  command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
          \ | wincmd p | diffthis
endif

UrlTransform

Sometimes I have to analyze long URLs:

function UrlTransform()
    exec "r !~/bin/uritransform.pl \"" . getline(".") . "\""
endfunction

map <Leader>u :call UrlTransform()<CR>

Here the source for uritransform.pl:

#!/usr/local/bin/perl

use strict;
use warnings;
use feature 'say';

use URI;
use URI::QueryParam;
use Text::SimpleTable;

my $url_string = $ARGV[0] or die "undefined URL";

my $uri = URI->new( $url_string );
my $table_attrs  = Text::SimpleTable->new( [ 20, 'Attribute' ], [ 50, 'Value' ] );
my $table_params = Text::SimpleTable->new( [ 20, 'Key' ],       [ 50, 'Value/s' ] );

$table_attrs->row( 'Scheme', $uri->scheme() );
$table_attrs->row( 'Host',   $uri->host() );
$table_attrs->row( 'Path',   $uri->path() );

foreach my $key (sort $uri->query_param()) {
    $table_params->row( $key, join q{, }, $uri->query_param( $key ) );
}  
 
print $table_attrs->draw();
print $table_params->draw();

Version control for config files

In order to have all my Vim config files in a single directory, and be able use version control for them, I moved my .vimrc file in .vim/. In $HOME/.vimrc, I only have

runtime .vimrc

Open a web browser from Vim

Tip taken from Open a web-browser with the URL in the current lineedit this page. This version works with Mac OS X

function! HandleURI()
  let s:uri = matchstr(getline("."), '[a-z]*:\/\/[^ >,;:]*')
  echo s:uri
  if s:uri != ""
      exec "!open \"" . s:uri . "\""
  else
      echo "No URI found in line."
  endif
endfunction
map <Leader>b :call HandleURI()<CR>
Personal tools