LoginSignup
13
12

More than 5 years have passed since last update.

Vimでコードスニペットを挿入する

Last updated at Posted at 2012-03-12

同じコードを繰り返し書かないで済む。重宝する。

" VimプラグインをVundleで管理する方法はぐぐってください!
Bundle 'Shougo/neocomplcache'
Bundle 'Shougo/neocomplcache-snippets-complete'

let g:neocomplcache_snippets_dir = "~/.vim/snippets"
" Define dictionary.
let g:neocomplcache_dictionary_filetype_lists = {
    \ 'default'    : '',
    \ 'perl'       : $HOME . '/.vim/dict/perl.dict',
    \ 'javascript' : $HOME . '/.vim/dict/javascript.dict'
    \ }

" Define Keybinds.
imap <expr><C-k> neocomplcache#sources#snippets_complete#expandable() ? "\<Plug>(neocomplcache_snippets_expand)" : "\<C-n>"
smap <C-k> <Plug>(neocomplcache_snippets_expand)

これで C-k で登録済みのコードスニペットを呼び出せるようになる。

neocomplcache-snippets-complete というVimプラグインを用いている。昔はneocomplcacheに付属していたが今は分離されている模様。

参考までに、以下は僕がPerlを書く際に使っている現在のsnippetsです。JavaScriptでやるならば こちら が参考になるでしょう。

snippet u
    #!/usr/bin/env perl
    use 5.10.0;
    use strict;
    use warnings;

snippet d
    use Data::Dump qw(dump);
    warn dump 

snippet dd
    use Data::Dumper;
    local $Data::Dumper::Indent = 1;
    local $Data::Dumper::Terse  = 1;
    warn Dumper 

snippet dds
    use Data::Dump::Streamer qw(Dumper);
    warn Dumper 

snippet c
    use Carp qw/croak/;

snippet n
    sub new {
        my $class = shift;
        my $args = ref $_[0] ? $_[0] : +{@_};

        ${1:code}

        bless $args, $class;
    }

snippet l
    use Class::Accessor::Lite

snippet ll
    Clas::Accessor::Lite->mk_accessors(${1:accessors});

snippet t
    #!/usr/bin/env perl -w
    use strict;
    use Test::More;

    subtest => sub {
    };

    done_testing;

snippet s
    my ($self) = @_;

snippet ex
    use parent qw(Exporter);
    our @EXPORT_OK = qw();

snippet plack
    use Plack::Request;
    sub {
        my $req = Plack::Request->new($_[0]);
        given ($req->path) {
            when ('/') {
                $req->param('');
                return [200, ["Content-Type" => "text/html"], ['200 OK']];
            }
        }
        return [404, ["Content-Type" => "text/plain"], ['404 Not Found']];
    };

snippet file
    use Path::Class qw(file);
    my @lines = file(__FILE__)->dir->file('/path/to/file')->slurp;

snippet r200
    [ 200, ['Content-Type', 'text/plain'], ['200 OK'] ]

snippet r404
    [ 404, ['Content-Type', 'text/plain'], ['404 Not Found'] ]
13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12