textmate >> vim for python - teething troubles: especially indenting - python

I'm (attempting) to move from textmate to vim [macvim to be exact] as my primary editor. I have already installed snipmate - wondering if there are other plugins you would suggest I install?
In particular I seem to be having a lot of trouble with indenting (<< seems to really do some very strange/unpredictable things), and I can't seem to find a solution for this - are there plugins I need for this to work properly?
Thanks!

For source code,
:h =
In a nutshell, in normal mode inside a block you wish to work with:
=a{ to re-indent a block. =a} and =aB work as well.
=2a{ to re-indent this block and its outer block.
If you happen to stand on a brace then =% will re-indent up to the matching brace.
>a{ to increase the indent of this block.
<a{ to decrease the indent of this block.
. repeats the last command, so <a{. decreases the indent of this block twice.
Make sure you have filetype set so Vim recognizes the filetype. Indenting is a function of the file type, after all.
For text,
:h gq
gq{ will format this paragraph.
gq( will format this sentence.
gqgq will format this line.
gggqG will format the entire document.

Set the filetype setting in your vimrc file
filetype plugin indent on
That should enable filetype plugins and automatic indentation

I am not sure what you need exactly as I have not used textmate. But I do use these Plugins for VIM. They have helped me a lot.

Related

Why is the word file get highlighted in .py files in the Vim text editor?

The word "file" in python is not a keyword, but my Vim highlights it as if it were. Can someone suggest a fix and explain this strange behaviour? It keeps messing with me. It bothers me every time I look at it.
Extra info
I think I use regular Vim—I haven't tinkered with my .vimrc much, certainly not with the syntax highlighting.
Many builtins were removed in the Python 2 to 3 transition. file is one of them. Lots of syntax highlighters still highlight them. Now that Python 2 is no longer supported, maintainers may be willing to remove them from the lists, so you can try opening an issue if it bothers you.

Python Tidy Code

I edit all my Python files on a remote Centos server using nano. This seems to work well. Occasionally I need to make a small change that changes the indention over the entire file. Is there an easy way to convert all the one space indents to 4 space etc? I have looked at PythonTidy.py but it seems to change too many things.
Not sure if you would consider switching editors. You can customize VIM so it handles auto indentation and easily set tabs to spaces vice versa using the vimrc file. Vim also has a convenient fix indentation command for your issue.
In you're favorite text editor, use search and replace. Find all the instances of
"\n [^-\s]"
and replace them with
"\n "
As long as it takes the newline and regex as expected, it'll indent all the newlines followed by a space followed by a not whitespace (meaning, it won't change 4 spaces into 7 spaces).
I found this:
How to fix python indentation
Installed python-tools then used reindent.py. Worked absolutely perfect.
Thanks.

got a vim mapping to show python code errors. Now I need to show it only if there are errors

have these in my vimrc:
set makeprg=python\ %
map <F5> :w<CR> :silent make <bar> copen<CR>
imap <F5> <Esc>:w<CR> :silent make <bar> copen<CR>i
it works, but I would like it better if the quickfix window doesnt open if there are no errors. I found some info (and a quite similar question here in SO about a klang compiler) but i dont understand this stuff very well, can anyone help? ty.
edit: the quickfix window is empty if there are no errors, so i guess it is a matter of checking its contents, but this is very far from my limited vim-fu
It might be far easier to use a dedicated plugin like vim-flake8. It can be configured to check Python syntax on each buffer save, so you do not need to misuse the 'make' program setting:
autocmd BufWritePost *.py call Flake8()
Errors open in the quick fix window, and the quick fix window does not open if there are no errors, as you request.
EDIT: ... but if you insist on using only vimrc-based tricks (which is more portable, you are right), then you have to remove the copen part of your commands, because that is what is forcing to open the quick fix window on every run:
map <F5> :w<CR> :silent make <bar><CR>
imap <F5> <Esc>:w<CR> :silent make <bar><CR>i
You are executing your python code not linting it. A quick google shows pylint and Python - check syntax and run script. There is also synstastic.vim if you want a heavier handed approach. I have created a simple python compiler plugin if you wish to use it.
To answer your question you can use :cwindow instead of :copen to only open the quickfix list windown when errors are present.
Optionally: A common approach is to use an :autocmd to execute :cwindow/:copen after :make. e.g autocmd QuickFixCmdPost * cwindow
Some thoughts on your mappings:
It is usually best to supply a mapping so map would become nmap
General rule of thumb use *noremap unless using <Plug> mappings
There is a space in the mapping that will move the cursor
Needlesss <bar>
The "Vim Way" typically avoids executing commands from insert mode
Cleaned up version of you mapping:
nnoremap <f5> :w<cr>:silent make<cr>:cwindow<cr>
For more help see:
:h :make
:h 'makeprg'
:h :compiler
:h :cwindow
:h :compiler
:h write-compiler-plugin

Enforce "spaces" or "tabs" only in python files?

In Python, is there a mean to enforce the use of spaces or tabs indentation with a per file basis ?
Well, perhaps "enforce" is too strong, more like a "recommendation".
I keep receiving patch files with mixed indentation and this is annoying... (to say the least) Python itself can tell when there is a problem, but I am searching something to do that at the editor level, like it exists for the charset.
Edit : Ok, my question wasn't clear, I am asking this because I keep receiving corrections and patches in any mix of tab/space you can imagine. I am using Mercurial as a DVCS, perhaps something exists at this level ?
Tim Peters has written a nifty script called reindent.py which converts .py files to use 4-space indents and no tabs. It is available here, but check your distribution first -- it may have come bundled in an Examples or Tools directory. (On the latest LTS Ubuntu, it is provided by the python2.7-examples package.)
If you can set up a Mercurial hook you should be able to run all files through reindent.py.
By the way, if you are using unix, then your system may also have the expand (and unexpand) command, which convert all tabs to spaces (and spaces to tabs). However, in this case, I think reindent.py is the right tool.
Look at the tabnanny module: — Detection of ambiguous indentation.
This is something your editor should do for you. Most editors (try Notepad++ for example, it's free) will let you set whether hitting tab enters a tab character or a number of spaces. I'd recommend using two spaces instead of tab in all files (I find 4 is too much). Using spaces instead of tabs is better as it means that you can indent stuff using both the space & tab keys without worrying about messing up your files.
If you have files that have a mix it isn't hard to write your own script to convert tabs to spaces
As explicited in PEP 8, never mix tabs and space. However, a file with both may just run...
As it says there:
The most popular way of indenting Python is with spaces only. The
second-most popular way is with tabs only. Code indented with a mixture
of tabs and spaces should be converted to using spaces exclusively.
When invoking the Python command line interpreter with the -t option, it issues
warnings about code that illegally mixes tabs and spaces. When using -tt
these warnings become errors. These options are highly recommended!
the solution is therefore to use as a default:
python -t my_mixed_code.py
To answer at the editor level, this depends on the editor, please specify!

Python tool to balance parentheses, quotes, and brackets

Does anyone know of an already-written Python script, tool, or editor that will check for unbalanced multi-line tokens? (parentheses, quotes, {}, [], etc.)
I've been writing Python code in IDLE, and every so often I'll get "EOF token in multi-line statement" and start swearing, because it means that somewhere in about 200 lines of code I forgot a closing parenthesis or quote and IDLE can't tell me where. This seems like a fairly straightforward thing, I just don't really have the time or headspace to work it out myself right now.
Much thanks
I use Eclipse with PyDev. It's very good for this sort of thing, and lots more.
emacs will automatically highlight matching pairs of parentheses/brackets/quotes/etc. as you type them, and it will inform you immediately if you mismatch them (e.g. if you type a [ followed by a )). I'm sure vim also does this, but since I don't use vim, I can't say with certainty.
PyDev is the best IDE to develop in Python. Has this feature and a lot of more.
If you use the vim text editor, there is a syntax highlighter for Python that might be of some help. Vim's python indenting rules also line up a new line with an unmatched open parenthesis from the previous line. That's been my visual cue that something is unbalanced.

Categories

Resources