ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What are the most compatible, portable etc. values for:
- tabstop
- soft tabstop
- shiftwidth
- whether to expand tab(to spaces)
?
For instance, the following are my settings in different projects for Vim:
Code:
# vim: noet ts=4 sts=4 sw=4
# vim: noet ts=8 sts=4 sw=4 ci pi
# vim: et ts=4 sts=4 sw=4
The problem is that it isn't printed correctly when
- using ex. cat command to show code in terminal
- printing on paper
- many other places like pastebin services etc.
- user left the default 8-space wide tab
It should also somehow depend on language. Well, what's about PHP, C/C++ and
JavaScript, for instance?
The lead of our team insists on tabs. But I wonder how should I align? To
install plugins, align manually(!), or not align at all :/ ? Or to accept, say,
Linux kernel coding style with 8-space wide tabs?
There is no 'best' style (and wars have started over this question).
The most portable setting is to use 8 space tabs, because that has been the default for so long in computing, but many people don't find it to be the most readable style. It is rare in kernel code to have deeply nested indentation, so it is a reasonable choice there.
The option of converting to spaces solves the tab problem by getting rid of them! It works fine if everyone on a project is content (or forced to) use the same spacing for tabs, which is why it is a common solution.
My personal preference is to leave the tabs in place (but to use tab purely for indentation semantics, ie, only ever at the start of a line, and with the soft tabstop the same as the tabstop). This means that others can choose their own preferences for how the indentation is viewed. As you have pointed out, there is extra work when formatting for printed or web output, because the decision has been deferred. But is is not such a big deal, just a matter of using expand when you need to produce such output.
Neither of these methods is bad; what is bad is when a team fail to agree and some use tabs and others don't...
There are some languages where it becomes really important to have consistency, like Occam and Python.
Last edited by neonsignal; 12-03-2011 at 07:30 AM.
There's no such thing. It is subjective/matter of preference.
My personal preference is to use tabs (tab character not emulated with spaces) with proportional fonts in GUI text editors that supports "elastic tabs".
However, some opensource projects insist on using spaces for identation.
Quote:
Originally Posted by r.osmanov
The lead of our team insists on tabs.
Then the best idea would be to use style everyone uses. Unless you're working on specific module nobody except you ever touches.
Quote:
Originally Posted by r.osmanov
But I wonder how should I align?
You have to decide that yourself.
You can convert between spaces and tabs using sed or perl using "global replace". Changing from tabs to spaces is easier than changing from spaces to tabs (because somebody always uses odd number of spaces for identation, and after search/replace you end up with mix of tabs and spaces which is not good).
Then the answer to all of your questions is ask your team. It's common for programming teams to have a coding standard that spells all this out.
Quote:
It should also somehow depend on language. Well, what's about PHP, C/C++ and
JavaScript, for instance?
It is very common for vim users to have different settings for each language they use. For the technical details of how to set that up, see my answer here:
Then the answer to all of your questions is ask your team. It's common for programming teams to have a coding standard that spells all this out.
It is very common for vim users to have different settings for each language they use. For the technical details of how to set that up, see my answer here:
I don't ask what should I do with our team. The question is "What is the best way to indent/align?".
I know these vim settings. The question is not concering use of vim, but what should a programmer use for
tabstop, soft tabstop, shift width and whether to expand tab.
My personal preference is to leave the tabs in place (but to use tab purely for indentation semantics, ie, only ever at the start of a line, and with the soft tabstop the same as the tabstop). This means that others can choose their own preferences for how the indentation is viewed. As you have pointed out, there is extra work when formatting for printed or web output, because the decision has been deferred. But is is not such a big deal, just a matter of using expand when you need to produce such output.
Thank you for the reply.
I'd also note, that if I set
# vim: noet ts=4 sts=4 sw=4
(actually my preference), and some other programmer prefers 2- or 8-space wide tab:
# vim: noet ts=2 sts=4 sw=4
# vim: noet ts=8 sts=4 sw=4
or any other than 4, the output of the following will not be aligned:
IThe question is not concering use of vim, but what should a programmer use for
tabstop, soft tabstop, shift width and whether to expand tab.
You have been answered in every follow-up, including mine: there is no single setting that's ideal for every language and every project. And if you don't like that answer, well, I'm sorry, but it's not my problem.
Quote:
- whether to expand tab(to spaces)
Well, this one actually has an answer. Every coding standard in existence recommends spaces instead of tabs.
I personally favour tabs because you can create a lot of inconsistent styles with space-based indenting depending on the editor you're using. With tabs, you can simply configure the tab width and you're set. With spaces, you HAVE to stick to a fixed number of spaces for indentation and remember to do it for all editors you use before you start coding. The confusion over how many spaces to use 4, 6 or 8 or whatever is transparent with tab-based indentation (everybody can simply configure their editors the way they want, without affecting others) while in space-based indentation it has to be enforced strictly in team based projects.
Last edited by vharishankar; 12-04-2011 at 10:19 AM.
No "best", no right answer, only choices and it is sane to comply with whatever conventions are in use. Where there are coding standards the should be followed.
In Ruby, two space indentation is strongly encouraged and is used in all the examples in O'Reilly's "The Ruby Programming Language" and in every style guide I have seen.
I don't Python but understand indentation standards are mandatory or it wont run. The official style guide says to use four spaces but "For really old code that you don't want to mess up, you can continue to use 8-space tabs"
Spaces are generally but not universally preferred over tabs so the code looks as intended regardless of the editor's tabstop setting. FWIW I code bash using tabs for convenience and periodically run :%s/^I/ /g
EDIT: that only makes sense when using an editor that used fixed-pitch characters; the idea of using a proportional font in a code editor feels very strange.
I'd also note, that if I set
# vim: noet ts=4 sts=4 sw=4
(actually my preference), and some other programmer prefers 2- or 8-space wide tab:
# vim: noet ts=2 sts=4 sw=4
# vim: noet ts=8 sts=4 sw=4
or any other than 4, the output of the following will not be aligned:
Code:
$abcasdfsdfsd = 1233;
$sd = 34;
Which is precisely why I said that my preference is to have tabs "only ever at the start of a line". The editor I have traditionally used enforces this; in vim it is a self-imposed constraint. It is quirks like this that make it easier to just standardize on space expansion.
If you are writing completely new code, use whatever works for the language and programmers that will use the code. If adopting code previously written by others, use the style chosen by the original author(s).
I prefer to not use tabs unless required (Makefiles), and would never use a proportional font in a source code editor. Four columns is a reasonable indentation for most languages that I use. In assembler, I would probably use eight-column indentation, or multiples of eight columns. I prefer comment blocks to be left-aligned for efficient block-copy-paste editing.
--- rod.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.