dotfiles/vim/.vim/after/ftplugin/python.vim

66 lines
2.2 KiB
VimL
Raw Normal View History

2023-05-26 09:06:10 +02:00
" Press F9 to run the script
autocmd FileType python map <buffer> <F9> :w<CR>:exec '!python3' shellescape(@%, 1)<CR>
autocmd FileType python imap <buffer> <F9> <esc>:w<CR>:exec '!python3' shellescape(@%, 1)<CR>
2024-12-19 09:31:41 +01:00
set tabstop=4 shiftwidth=4 autoindent smartindent expandtab
2026-08-03 18:47:03 +02:00
setlocal colorcolumn=120
2022-12-18 14:54:56 +01:00
setlocal path=.,**
setlocal wildignore+=*.pyc
2026-08-05 11:50:53 +02:00
let g:ale_python_flake8_options = '--max-line-length=120'
2022-12-18 14:54:56 +01:00
" Add search in other files inside the path
set include=^\\s*\\(from\\\|import\\)\\s*\\zs\\(\\S\\+\\s\\{-}\\)*\\ze\\($\\\|\ as\\)
2023-07-18 23:56:32 +02:00
" function! PyInclude(fname)
" " Following examples:
" " import conv.metrics
" " /conv.metrics/
" " conv/metrics.py
" " from conv import conversion as conv (2)
" " /conv import conversion/
" " conv/conversion.py conv.py
" let parts = split(a:fname, ' import ') " (1) [conv.metrics] (2) [conv, conversion]
" let l = parts[0] " (1) conv.metrics (2) conv
" if len ( parts ) > 1
" let r = parts[1] " conversion
" let joined = join([l,r], '.') " conv.conversion
" let fp = substitute(joined, '\.', '/', 'g') . '.py'
" let found = glob(fp, 1)
" if len(found)
" return found
" endif
" endif
" return substitute(joined, '\.', '/', 'g') . '.py'
" endfunction
" setlocal includeexpr=PyInclude(v:fname)
2022-12-18 14:54:56 +01:00
setlocal define=^\\s*\\<\\(def\\\|class\\)\\>
2023-05-26 09:06:10 +02:00
2024-12-19 09:31:41 +01:00
let g:ale_fixers = {
\ '*': ['remove_trailing_lines', 'trim_whitespace'],
\ 'python': ['black', 'add_blank_lines_for_python_control_statements'],
\}
function! LoadVirtualEnv()
if filereadable('./.venv/bin/activate')
let g:virtualenv_path = getcwd() . '/.venv/bin/python'
let g:python3_host_prog = g:virtualenv_path
echo "Loaded virtual environment: " . g:virtualenv_path
elseif filereadable('./venv/bin/activate')
let g:virtualenv_path = getcwd() . '/venv/bin/python'
let g:python3_host_prog = g:virtualenv_path
echo "Loaded virtual environment: " . g:virtualenv_path
else
" If no virtualenv is found, fall back to the default Python
" Check if variables exist before unsetting them
if exists('g:virtualenv_path')
unlet g:virtualenv_path
endif
if exists('g:python3_host_prog')
unlet g:python3_host_prog
endif
endif
endfunction
autocmd BufEnter * call LoadVirtualEnv()