Skip to content

Instantly share code, notes, and snippets.

@nicwest
Last active January 2, 2019 13:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nicwest/11081513 to your computer and use it in GitHub Desktop.
Save nicwest/11081513 to your computer and use it in GitHub Desktop.
vim function that copies a dotted python path label thing from your cursor to your clipboard for running tests
function! PythonGetLabel()
if foldlevel('.') != 0
norm! zo
endif
let originalline = getpos('.')
let lnlist = []
let lastlineindent = indent(line('.'))
let objregexp = "^\\s*\\(class\\|def\\)\\s\\+[^:]\\+\\s*:"
if match(getline('.'),objregexp) != -1
let lastlineindent = indent(line('.'))
norm! ^wye
call insert(lnlist, @0, 0)
endif
while line('.') > 1
if indent('.') < lastlineindent
if match(getline('.'),objregexp) != -1
let lastlineindent = indent(line('.'))
norm! ^wye
call insert(lnlist, @0, 0)
endif
endif
norm! k
endwhile
let pathlist = split(expand('%:r'), '/')
echo 'Python label: ' join(pathlist + lnlist, '.')
let @0 = join(pathlist + lnlist, '.')
let @+ = @0
call setpos('.', originalline)
endfunction
function! DjangoTestThis()
call PythonGetLabel()
let @" = './manage.py test ' . @0
if exists("*SendToTmux")
call SendToTmux(@")
endif
endfunction
nnoremap gL :call PythonGetLabel()<CR>
nnoremap <leader>tl :call DjangoTestThis()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment