Thursday, November 3, 2011

Re: increasing/reducing the number of leading white spaces dynamically

On 11/03/11 10:36, Jose Caballero wrote:
> for our current python project, we are trying to follow the PEP
> recommendations, and therefore keeping all indentations to 4 white spaces.
> However, when I am writing code, I feel more comfortable with 8 white
> spaces.
> I am pretty sure there must be a trick to put on .vimrc (or similar) in
> such a way the indents are expanded dynamically when I open a file, but
> they are reduced to 4 again when I leave ( ':q' or ':wq' )
> In this way the file on disk keeps having 4, but I see 8 when editing code.
> The only problem is that I don't know the keywords to search on google
> myself... If someone can provide for the keywords I can google it myself.

My guess is that you want a pair of autocmds

:help :autocmd

that trigger on BufWritePre and BufWritePost, something like
(untested)

autocmd BufWritePre *.py %s/^\(
\{8}\)\+/\=substitute(submatch(0), repeat(' ', 8), repeat(' ',
4), 'g')

autocmd BufWritePost *.py %s/^\(
\{4}\)\+/\=substitute(submatch(0), repeat(' ', 4), repeat(' ',
8), 'g')

The first one triggers before the file is written, searches for
multiples of 8 spaces at the beginning of the line and replaces
them with the corresponding number of 4-space instances. The
second one triggers after you've written the file and internally
restores the 8-space leading. The 2nd one might be simply
replaced with

autocmd BufWritePre *.py u

which might undo the conversion to 4-space indentation a lot more
cleanly. Test first :)

For more info, read up at

:help BufWritePre
:h BufWritePost
:h sub-replace-special
:help :u

-tim

PS: it's interesting seeing it from your side where you want
*more* leading whitespace. I tend to do my throw-away python
with 2-space indentation, or with hard-tab characters that I then
change 'ts'/'sw' to display in accordance with my mood.


--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment