Sunday, May 8, 2016

The Best and Simple Way to python VIM autocompletion


Thanks for this blog, that solve my problem.

I represent the above blog, and try to use more simple way to get the result, autocompletion for python.

download package

first of all download neobundle.vim

mkdir -p ~/.vim/bundle
git clone ~/.vim/bundle/neobundle.vim

download jedi-vim

cd ~/.vim/bundle/
git clone --recursive

execute NeoBundleInstall

execute VIM and type the following command line.


Edit the .VIMRC

go to root path and edit .vimrc

cd ~/
vim .vimrc

copy-paste the following to .vimrc

if 0 | endif
if has('vim_starting')
   if &compatible
      set nocompatible               " Be iMproved
  " Required:
  set runtimepath+=~/.vim/bundle/neobundle.vim/
" Required:
call neobundle#begin(expand('~/.vim/bundle/'))
" Let NeoBundle manage NeoBundle
" Required:
NeoBundleFetch 'Shougo/neobundle.vim'
" My Bundles here:
" Refer to |:NeoBundle-examples|.
" Note: You don't set neobundle setting in .gvimrc!
call neobundle#end()
" Required:
"filetype plugin indent on
" If there are uninstalled bundles found on startup,
" this will conveniently prompt you to install them.

" Jedi for python
NeoBundleLazy "davidhalter/jedi-vim", {
    \ "autoload": { "filetypes": [ "python", "python3", "djangohtml"] }}
if ! empty(neobundle#get("jedi-vim"))
  let g:jedi#auto_initialization = 1
  let g:jedi#auto_vim_configuration = 1
  nnoremap [jedi] <Nop>
  xnoremap [jedi] <Nop>
  nmap <Leader>j [jedi]
  xmap <Leader>j [jedi]
  let g:jedi#completions_command = "<C-x>,<C-o>"
  let g:jedi#goto_assignments_command = "<C-g>"
  let g:jedi#goto_definitions_command = "<C-d>"
  let g:jedi#documentation_command = "<C-k>"
  let g:jedi#rename_command = "[jedi]r"
  let g:jedi#usages_command = "[jedi]n"
  let g:jedi#popup_select_first = 0
  let g:jedi#popup_on_dot = 0

  "autocmd FileType python setlocal completeopt-=preview
  " for w/ neocomplete
    if ! empty(neobundle#get("neocomplete.vim"))
        autocmd FileType python setlocal omnifunc=jedi#completions
        let g:jedi#completions_enabled = 0
        let g:jedi#auto_vim_configuration = 0
        let g:neocomplete#force_omni_input_patterns.python =
                        \ '\%([^. \t]\.\|^\s*@\|^\s*from\s.\+import \|^\s*from \|^\s*import \)\w*'

syntax on;
if has("autocmd")
           autocmd BufRead *.txt set tw=78
              autocmd BufReadPost *
                    \ if line("'\"") > 0 && line ("'\"") <= line("$") |
                    \   exe "normal g'\"" |
                    \ endif
      set nocompatible
      set paste
      set viminfo='1000,<500
      set nowrap
      set nocp incsearch
      set shiftwidth=4
      set cinoptions=:0,p0,t0
      set cinwords=if,else,while,do,for,switch,case
      set hls
      set cindent
      " python auto-complete code
      " "autocmd FileType python setlocal et | setlocal sta | setlocal sw=4"
      " autocmd FileType python set et nu sw=4 ts=4


Now you can use auto completion by using ctl-x ctl-o. where ctl is control button. You will see the following result.

More test

You can try another package that not belong to original python.

pip install simplejson

try ctl-x ctl-o, you can see the funtion of simplejson lib.

No comments:

Post a Comment