Yesterday I decided to look into vim indenting for Stata do-files.
Pretty much every programmer likes to indent their code to make it more legible. Many do it by hand, but it is mostly a routine task, and it is easy to have the text editor do it instead.
Vim, however, isn't known for its user-friendliness; its extreme customizability sometimes comes at the cost of clarity. Indenting, I find, is one such example.
Basically, indenting is filetype specific; every major language has its own file in $VIMRUNTIME/indent. Stata doesn't, and I don't think anyone bothered to make one.
There isn't much indenting needed in Stata--the most crucial thing is to indent between curly brackets. However, by default (i.e. when it doesn't find a specific indent file) VIM indents according to the C rules; so we need to customize this a bit, as stata code indented according to C rules will sometimes give strange results.
I find that the following options work well for me:
set shiftwidth=5
set cinoptions=>s,e0,n0,f0,{0,}0,^0,:0,=0,l0,b0,g0,h0,p0,t0,i0,+0,c0,C0,/0,(0,u0,U0,w0,W0,m0,j0,)20,*30,#0
set cinwords=
You can find the help for each of these by just typing :help cinoptions and :help shiftwidth. More general help on indenting is :help C-indenting.
For a bit more on indenting commands, have a look at this Vimcast.
Then to have a block of text indented automatically in your do-file, you can select it and type =. To do it for the whole file, gg=G works like a charm.
EDIT:
If you want a quick workaround, just put this (very simple) indent file in the /indent directory of your runtimepath. It contains the shiftwidth and cinoptions values I gave above.
Wednesday, 14 April 2010
Tuesday, 6 April 2010
Preamble: how I work; and why I blog.
Like many an economist, I use a diversified set of computer tools in my day-to-day research. The ones for which I developed a comfortable proficiency over the years, and which are likely to provide much of this blog's subject matter, are:
- Stata, for statistical analysis. I you're a half decent programmer, Stata is enough for most of your data analysis needs.
- LaTeX, for typesetting (because it looks so darn sciencey)
- Vim, the best text editor ever. (emacs fans are not welcome here.)
- Dropbox. I discovered it in September '09, and it made my work life so, so much easier.
- Spotify, for musical background.
Obviously, I am not claiming that those are the best in their category. Everyone should just choose whichever software they are most comfortable with and stick to it. I am sure that if I had decided to learn emacs rather than vim, I'd be pretty much as happy with it. I chose vim pretty much on a whim, and am sure I will never go back on that decision, simply because of the learning curve of advanced text editors--I do not wish to go through that again!
Anyway I am only saying that those tools work for me, and hence most of the content of this blog will be tips on how to get the most out of them.
Why am I writing this blog? Simply as a way to pay back to the Internet community the help it gave me whenever needed. If I have a problem with LaTeX, most of the time I just have to do a bit of a google search to find someone who had (and solved) the same problem in the past. But sometimes I stumble on problems for which no help is easily available. Those are instances where I can, and arguably should, write clearly my problem and its solution. I hope that at least a few fellow users will benefit from it.
- Stata, for statistical analysis. I you're a half decent programmer, Stata is enough for most of your data analysis needs.
- LaTeX, for typesetting (because it looks so darn sciencey)
- Vim, the best text editor ever. (emacs fans are not welcome here.)
- Dropbox. I discovered it in September '09, and it made my work life so, so much easier.
- Spotify, for musical background.
Obviously, I am not claiming that those are the best in their category. Everyone should just choose whichever software they are most comfortable with and stick to it. I am sure that if I had decided to learn emacs rather than vim, I'd be pretty much as happy with it. I chose vim pretty much on a whim, and am sure I will never go back on that decision, simply because of the learning curve of advanced text editors--I do not wish to go through that again!
Anyway I am only saying that those tools work for me, and hence most of the content of this blog will be tips on how to get the most out of them.
Why am I writing this blog? Simply as a way to pay back to the Internet community the help it gave me whenever needed. If I have a problem with LaTeX, most of the time I just have to do a bit of a google search to find someone who had (and solved) the same problem in the past. But sometimes I stumble on problems for which no help is easily available. Those are instances where I can, and arguably should, write clearly my problem and its solution. I hope that at least a few fellow users will benefit from it.
Subscribe to:
Posts (Atom)