Skip to content

Instantly share code, notes, and snippets.

@da-x
Created November 27, 2017 16:00
Show Gist options
  • Save da-x/0f06071ad676037ad236c1b0b46314ea to your computer and use it in GitHub Desktop.
Save da-x/0f06071ad676037ad236c1b0b46314ea to your computer and use it in GitHub Desktop.
Insert #include for a C declaration in Vim
"
" CInsertIncludeForTag
"
" Search for the filename containing the prototype of the function or
" type definition for a C tag, and insert '#include <filename>' in the
" current file where there is the marker `i`.
"
function! CInsertIncludeForTag()
redir => l:matches
echo (":tselect " . expand("<cword>"))
silent execute (":tselect " . expand("<cword>"))
redir END
for l:line in split(l:matches, "\n")
let l:fields = filter(split(l:line, " "), 'v:val != ""')
if len(l:fields) >= 5
if l:fields[2] == 'p' || l:fields[2] == 't'
call append(line("'i"), "#include <" . l:fields[4] . ">")
break
endif
endif
endfor
endfunction
command! CInsertIncludeForTag call CInsertIncludeForTag()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment