I am writing a little python script to learn VIM basics (I'm a beginner with VIM).
I've configured VIM to work with omnicompletion, and it does.
For example, if I write str. then press ctr+x, ctr+o it suggests me all of the string methods.
However in my code I have something like this:
for line in inFile.readlines():
something = line.rpartition(" ")[0]
I'd like VIM to autocomplete the rpartition method name after typing line.rpart. I don't expect it to know line object type, but I'd like VIM to propose a context-unaware completion list based on python libraries acquaintance.
For example if with eclipse i try to complete
anObject.rpart
it suggests me the rpartition method even if it has nothing to do with anObject!
Is it possible to get this to work with VIM?
Thanks.
My .vimrc file:
set showcmd
set textwidth=80
set expandtab
set smarttab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set number
set autoindent
filetype indent on
filetype plugin on
autocmd BufRead,BufNewFile *.py syntax on
autocmd BufRead,BufNewFile *.py set ai
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,with,try,except,finally,def,class
set modeline
syntax on
" Closes the Omni-Completion tip window when a selection is
" made
autocmd CursorMovedI * if pumvisible() == 0|pclose|endif
autocmd InsertLeave * if pumvisible() == 0|pclose|endif
Seriously, use Jedi!
It's really much better than all the other autocompletions around.
May the force be with you!
You definitely should take a look at PySmell which can be installed for Vim easily. It generates a completion menu based on a static analysis for a given project. It also can generate completion suggestions based on tags it produces for external libraries like the Python Standard Library or Django Lib.
I was happy with Vims Python omnicimpletion, but since I switched to PySmell, I never looked back.
Related
If I open a python file with vim, and set it to have a ruler and auto-line break with this command:
:set textwidth=109 colorcolumn=110
It works like a charm! However ... if I edit ~/.vimrc and add this line at the bottom
autocmd FileType py set textwidth=109 colorcolumn=110
exit the python file, and open it again, nothing happens. Seems like something is overriding my setting, but can't figure out what is doing that (because I'm fairly new to vim). My basic vimrc file is this: https://github.com/amix/vimrc/blob/master/vimrcs/basic.vim
Can someone point me in the right direction on what am I doing wrong?
Note: the same thing is happening for Javascript files when I try to "replace" Tabs with 2 spaces in indentation:
autocmd FileType js setlocal sw=2 sts=2 et
The issue is that the correct filetypes are python and javascript (or similar, like javascript.jsx if you have additional syntax files), not py and js. You can check the filetype used for a file by :set ft?.
Also, you may prefer setlocal (to affect only the current buffer) instead of set.
I am looking for such python editors which suggest inputs(mentioned in file/database) while using custom modules and functions during writing programs.
Is there similar type of editor already exists that I can build something upon?
Can I develop such editor? If yes, how?
Try PyCharm, probably this software will cover all your needs
I would suggest, that you use the PyDev plugin for eclipse. PyDev has a lot of stuff, that increases the efficiency. You find it under: http://www.pydev.org/
Best Regards
1574ad6
You can use vim editor by including this line in your ~/.vimrc file.
:h ins-completion
Now you can use the below keyboard shortcut for auto complete feature for your custom functions.
Completion can be done for:
Whole lines |i_CTRL-X_CTRL-L|
keywords in the current file |i_CTRL-X_CTRL-N|
keywords in 'dictionary' |i_CTRL-X_CTRL-K|
keywords in 'thesaurus', thesaurus-style |i_CTRL-X_CTRL-T|
keywords in the current and included files |i_CTRL-X_CTRL-I|
tags |i_CTRL-X_CTRL-]|
file names |i_CTRL-X_CTRL-F|
definitions or macros |i_CTRL-X_CTRL-D|
Vim command-line |i_CTRL-X_CTRL-V|
if you want to learn the vim basics use this link :Vim Basics
You can use this .vimrc file for python development :
set title
autocmd FileType python set expandtab ts=4 sw=4
"autocmd FileType python set sw=4
"autocmd FileType python set sts=4
"autocmd FileType python set ts=4
" enable syntax highlighting
"autocmd FileType python syntax enable
" show line numbers
" set numbe
" set tabs to have 4 spaces
" autocmd FileType python set ts=4
" indent when moving to the next line while writing code
"set autoindent
" expand tabs into spaces
"autocmd FileType python set expandtab
" when using the >> or << commands, shift lines by 4 spaces
"autocmd FileType python set shiftwidth=4
" show a visual line under the cursor's current line
" set cursorline
" show the matching part of the pair for [] {} and ()
set showmatch
" enable all Python syntax highlighting features
autocmd FileType python let python_highlight_all = 1
" to change the default colour of the string to white.
highlight String guifg=1 guibg=11
:h ins-completion
When I use the = command to indent an entire Python file or a section it won't properly indent it. Here's my vimrc:
set nocompatible
syntax on
set ruler
set tabstop=2
set softtabstop=2
set shiftwidth=2
set expandtab
set smarttab
set hlsearch
set incsearch
set ignorecase
set autoindent
" turn on line numbers:
set number
" Toggle line numbers and fold column for easy copying:
nnoremap <F2> :set nonumber!<CR>:set foldcolumn=0<CR>
nnoremap <F4> :set nospell!<CR>
nnoremap <F3> :set invpaste paste?<Enter>
imap <F3> <C-O><F3>
set pastetoggle=<F3>
filetype on
filetype plugin indent on
filetype plugin on
" Execute file being edited with <Shift> + e:
map <buffer> <S-e> :w<CR>:!/usr/bin/env python % <CR>
let g:solarized_termcolors=256
set background=dark
colorscheme solarized
"set spell spelllang=en_us
set backspace=indent,eol,start
autocmd FileType python set complete+=k~/.vim/syntax/python.vim isk+=.,(
autocmd VimEnter * NERDTree
Also when I loop through my python files using w or b for instance, or when deleting it won't delete properly. For instance it will not stop on the . or ( when deleting a work before them and will even delete these.
You should get rid of filetype on and filetype plugin on: filetype plugin indent on is the only line you need.
edit
The problem with . and ( is almost certainly caused by iskeyword. I vaguely remember someone having the same problem because he/she found out in some misinformed blog post that he/she needed dictionary-based completion. Because the entries in his/her dictionary file where in the form .method(, he/she needed the . to be considered a keyword character.
Try this command when editing a Python file:
:verbose set iskeyword?
It should return a comma separated list of values that includes . and ( and the place where it is set. It's most likely to be a third party python ftplugin because the default ftplugin doesn't touch iskeyword.
This line is what is causing the ./( problem:
autocmd FileType python set complete+=k~/.vim/syntax/python.vim isk+=.,(
You absolutely don't need that completion mechanism because Vim's default omnicompletion is powerful enough.
Because of how that completion mechanism is implemented and how your dictionary file may be written, . and ( must be considered by Vim as keyword characters.
Your custom/ syntax file may not even be formed like that so this setting may not even work.
isk is the short form of iskeyword, the option that defines what is a keyword character.
You seem to have copied settings from someone else without understanding what they did. The simple fact that the answer to your question was in your own ~/.vimrc should suffice to show you how wrong this idea is.
you can paste normally usign the :set paste, I have a very handy macro for that:
set pastetoggle=<F10>
You can set it to be any other key, just add it to your .vimrc file
I've recently switched entirely to Vim for all my Python/Django development. It took me a lot of time to customize it to the point it is today, and God knows how hard it was for me to find help regarding the best vim plugins out there suited for Python/Django development.
I decided to ask this question so people like me could benefit directly from your experience:
You've built the perfect Python/Djangoish Vim editor? Describe it for us (plugins, scripts, customized .vimrc, colorschemes ..etc).
Thanks
My Configuration
Ok, this is my own configuration. actually I've chosen to create a simple Vim configuration so I can master the little number of plugins I've chosen to install instead of make a big stack of plugins that I'll never master nor use. This is the list of the plugins I use the most:
NERDTree for file management,
SnipMate which is an implementation of TextMate's snippets feature,
Code Completion is handled with Omnicompletion which come by default in Vim,
Pydoc to integrate the Python Documentation into Vim,
TagList for source code browsing, very useful in large files.
Pyflakes script to highlight Python code on the fly with warnings
Also I've created a python.vim file in $HOME/.vim/ftplugin/ containing this script so I can run python code from Vim just by running Shift+e:
" Execute file being edited with <Shift> + e:
map <buffer> <S-e> :w<CR>:!/usr/bin/env python % <CR>
Also I've collected some useful .vimrc customizations:
set nocompatible " use vim defaults
set number " show line numbers
colorscheme desert
set tags=tags;$HOME/.vim/tags/ "recursively searches directory for 'tags' file
set expandtab " tabs are converted to spac
set tabstop=4 " numbers of spaces of tab character
set shiftwidth=4 " numbers of spaces to (auto)indent
set showcmd " display incomplete commands
set hlsearch " highlight searches
set incsearch " do incremental searching
set ruler " show the cursor position all the time
set numberwidth=4 " line numbering takes up 5 spaces
set ignorecase " ignore case when searching
set nowrap " stop lines from wrapping
filetype plugin indent on " turn on the indent plugins
syntax on " syntax highlighing
" TagList Plugin Configuration
let Tlist_Ctags_Cmd='/usr/bin/ctags' " point taglist to ctags
let Tlist_GainFocus_On_ToggleOpen = 1 " Focus on the taglist when its toggled
let Tlist_Close_On_Select = 1 " Close when something's selected
let Tlist_Use_Right_Window = 1 " Project uses the left window
let Tlist_File_Fold_Auto_Close = 1 " Close folds for inactive files
" Omnicompletion functions
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
au FileType py set expandtab
au FileType py set foldmethod=indent
map <F2> :previous<CR> " map F2 to open previous buffer
map <F3> :next<CR> " map F3 to open next buffer
map <F4> :NERDTreeToggle<CR> " map F4 to open NERDTree
map <F5> :TlistToggle<CR> " map F5 to toggle the Tag Listing
map <silent><C-Left> <C-T> " taglist - map Ctrl-LeftArrow to jump to the method/property under your cursor
map <silent><C-Right> <C-]> " taglist - map Ctrl-RhitArrow to jump back to your source code
map <silent><A-Right> :tabnext<CR> " map Alt-RightArrow to jump to the next tab
map <silent><A-Left> :tabprevious<CR> " map Alt-LeftArrow to jump to the previous tab
I don't really have much Django specific mods, although I have given the jinja2 syntax a higher priority than the django template syntax.
For Python specifically:
For Python syntax checking I use PyFlakes with highlight SpellBad ctermbg=darkred
Sometimes (rarely) I feeld the need for autocompletion and in that case I use Eclim
For the rest, the default stuff. Tab size 4, soft tabs, etc...
Vim Settings:
256 Color scheme desert256
if ((&term == 'screen') || (&term == 'screen-bce') || (&term == 'xterm'))
set t_Co=256
set t_Sb=^[[4%dm
set t_Sf=^[[3%dm
colo desert256
endif
Lots of tabs (tabe, tabn)
Lots of splits (both vertical and horizontal)
I am not going to post my whole .vimrc file here, but I have a similiar setup as you. This is less Python/Django specific though, except for some custom snippets for snipMate and python-mode. Here the vim plugins I am using:
Pathogen: better organized vim plugin structure in .vim dir
comments.vim: faster language specific commenting with ctrl-c and ctrl-x
NERDTree
NERDTree tabs
syntastic: Syntax checking plugin (for me mainly used for non-python code)
surround.vim and autoclose.vim: Easier handling of brackets, opening and closing tags etc.
matchit: extends the % command to also match and circle through for example html tags. For circling through Python code statements (eg. if-elif-else) you can download python_match.vim and put it into your ftplugin/python/ dir. I put it into ~/.vim/bundle/matchit/ftplugin/python/
python-mode: Great plugin for Python editing. Has automated pyflakes/pep8 checking (or pylint if you want) on file save. I deactivated the auto complete via let g:pymode_rope = 0 in my .vimrc file though, since it lagged for me on each file save. Also the syntax highlighting is extended for python code.
snipMate (custom snippets for python follow below)
tagBar: I can't live without an outline for huge code files.
Some custom python snippets I use quite frequently:
snippet #utf
# -*- coding: utf-8 -*-
snippet ds
"""
${1: }
"""
# just the first (or last) three quites for the docstring
snippet dss
"""${1: }
# For file headers
snippet dsfile
"""
:File: ${1:`Filename('$1.py', 'foo.py')`}
:Author: ${2:`g:snips_author`}
:Description: ${3}
"""
snippet pdb
import pdb
pdb.set_trace()
I have Vim 7 (enhanced) on CentOS 5, and it comes with all the usual Vim plugins/scripts ready to go.
$ find /usr/share/vim/vim70/ -name \*python\*
/usr/share/vim/vim70/syntax/python.vim
/usr/share/vim/vim70/ftplugin/python.vim
/usr/share/vim/vim70/indent/python.vim
/usr/share/vim/vim70/autoload/pythoncomplete.vim
I would think that when opening a file ending in .py (vim file.py) it would automatically load these plugins, but I am not sure that is the case. What I want is:
Press TAB and receive four spaces. Auto indent next line for suites, conditionals, etc.
I have this working by explicitly setting tabstop, shiftwidth, etc. in my .vimrc file. Isn't this what the above Python files are for? Why do I have to set these things in my .vimrc? How do I get these features from the Vim plugins instead?
Current .vimrc:
syntax on
set hls
set expandtab
set textwidth=0
set tabstop=4
set softtabstop=4
set shiftwidth=4
set autoindent
set backspace=indent,eol,start
set incsearch
set ignorecase
set ruler
set wildmenu
set smarttab
filetype indent on
filetype on
filetype plugin on
My understanding is that the python.vim file is just a syntax-highlighting file possibly, because Python files can be indented multiple ways. PEP8 prescribes four spaces, but legacy files could be different including using tabs.
Some of our legacy Python files actually use two spaces per indent. So I leave Python indenting to Vim and configure it per file and per filetype. The following line in .vimrc gives me Python-specific settings which differ from say my xml, xhtml, and html (two spaces).
au FileType python setl shiftwidth=4 tabstop=4
You can also set specific settings by file with a modeline which is handy if you do have legacy files.
# vi: set tabstop=2 expandtab textwidth=70 filetype=python:
Setting tabstop, shiftwidth, etc... in your vimrc is correct. These set your global settings, as well as serve as parameters to the filetype-specific indentation support.
The language indentation plugins use these settings, but typically also set an indent expression (:he inde) appropriate for the language. Thus the Python indenter should be automatically indenting after a block opening statement (def, class, for...), and dedenting after a closing one (return, pass, continue...) and doing so according to the ts,sw,... you have set.
If you're still unsure if the plugin is loading for a buffer, simply do :filetype to show the detection, plugin, and indent settings, and :set ft? to see the detected type.