Vim: Command to include or import a file

August 24, 2011

This command will insert #include "x" or #import "x" at the top of your file (after the last include or import statement) without moving your cursor.

e.g.

:Import MyAppDelegate.h
" Adds a #include (or #import) directive
func! Include(name, directive)
  let l:stmt = a:directive . " \"" . a:name . "\""
  normal! mh
  try
    execute "?^#[       ]*\\(import\\|include\\)"
    execute "normal! o" . l:stmt
  catch
    execute "normal! ggi" . l:stmt . "\<CR>"
  endtry
  normal! `h
endfunc
com! -nargs=1 -complete=file Import :call Include(<q-args>,"#import")
com! -nargs=1 -complete=file Include :call Include(<q-args>,"#include")
Tags: objc fixme vim