Skip to content

Instantly share code, notes, and snippets.

@damianbaar
Created March 20, 2017 08:49
Show Gist options
  • Save damianbaar/93d3b8caa0478c399cdd49795217f7c0 to your computer and use it in GitHub Desktop.
Save damianbaar/93d3b8caa0478c399cdd49795217f7c0 to your computer and use it in GitHub Desktop.
nnoremap <leader>ti: call GetImport('')<CR>
function! GetImport(word)
let result = json_decode(system('importjs word '.expand('<cword>').' '.expand('%')))
let content = result.fileContent
let cursorPos = getpos(".")
let originalLineCount = line("$")
let @a = content
normal! gg
execute "put a"
" Remove lingering line at the top:
" Restore cursor position, attempting to compensate for the resulting
" imports moving the original line up or down
let newLineCount = line("$")
let cursorPos[1] = cursorPos[1] + newLineCount - originalLineCount
call setpos(".", cursorPos)
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment