1
" Python logger-print and vice-versa

function! SetPrintLogMaps()
    echom "called"
    nnoremap <buffer> <Leader>lp "lyy"lp"l5cawprint^[
    nnoremap <buffer> <Leader>ll "lyy"lp"lcawself.logger.info(^O$)^[
endfunction

autocmd BufWrite,BufRead *.py :call SetPrintLogMaps()

I have this autocmd so that the mappings are set only when a python script is read or written. But the function is not being called when I open any python script.

I've just found that if I am opening the script from command-line like vim test.py it is not working. But if I just open vim and then execute :e test.py the function is being called.

1 Answers1

3

You should use the FileType event:

augroup myPythonStuff
    autocmd!
    autocmd FileType python call SetPrintLogMaps()
augroup END

Better, put this in ~/.vim/after/ftplugin/python.vim:

nnoremap <buffer> <leader>lp "lyy"lp"l5cawprint^[
nnoremap <buffer> <leader>ll "lyy"lp"lcawself.logger.info(^O$)^[

and call it a day.

romainl
  • 186,200
  • 21
  • 280
  • 313