Windows 10 已于2025年10月14日停止支持。如果您正在使用 Windows 10,参见 这篇文章

Windows 10 reached EOS (end of support) on October 14, 2025. If you are on Windows 10, see this article.

搜索 | 用户支持

防范以用户支持为名的诈骗。我们绝对不会要求您拨打电话或发送短信,及提供任何个人信息。请使用“举报滥用”选项报告涉及违规的行为。

详细了解

How to control rendering of tab character in a TXT file?

  • 7 个回答
  • 3 人有此问题
  • 54 次查看
  • 最后回复者为 OldNerd
  • 已解决

When I browse a TXT file, the tab character is rendered so as to look much like 8 spaces. Is there a way to control the apparent number of spaces in the rendering? Yes, I know that things like

               pre { tab-size: 4 }

in CSS3 would control that number in the rendering of HTML elements, but I want similar control for a TXT file.

When I browse a TXT file, the tab character is rendered so as to look much like 8 spaces. Is there a way to control the apparent number of spaces in the rendering? Yes, I know that things like pre { tab-size: 4 } in CSS3 would control that number in the rendering of HTML elements, but I want similar control for a TXT file.

被采纳的解决方案

Or if you want to use CSS, this bookmarklet will insert the style rule into the page:

javascript:var r="pre{-moz-tab-size:4;}"; var s=document.createElement("style"); s.type="text/css"; s.appendChild(document.createTextNode(r)); document.body.appendChild(s); void 0;

(Note that at least for now, you need the -moz- prefix on tab-size in Firefox.)

定位到答案原位置 👍 1

所有回复 (7)

The only way to achieve this would be to open the text file in the Scratchpad.

Then you can control the tab width via the devtools.editor.tabsize pref on the about:config page.

Thank U, cor-el. Unfortunately, my reason for wanting to control the apparent size of tab characters when browsing TXT is that I want control how they print when I print what I am browsing. I fired up Scratchpad and found that it ignores Ctrl-P and has no [Print] button anywhere.

I grew up in a hard-copy world, and I still sometimes like to read my code on paper. I like Editra for editing, but not for printing. I had tentatively decided to switch to hard tabs for indentation, but then I realized that printing from Notepad would stick me with tab characters that look 8 spaces wide, as would printing from Firefox. Bleh.

Maybe I should work out a hack for temporarily populating a pre element in HTML with whatever code I want to print.

It is likely that there are other editors that allow to set the tab width.

You can set in Firefox to use which program to use for viewing the page source:

I case of problems you can test the path with this code in the Browser Console (Firefox/Tools > Web Developer; Ctrl+Shift+J)

viewSourceAppPath = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch)
.getComplexValue("view_source.editor.path",Components.interfaces.nsIFile);
viewSourceAppPath.path;

If the tabs are not used for positioning but just indenting, you could run a replace of each tab character with 4 spaces using a bookmarklet:

javascript:void(document.body.innerHTML = document.body.innerHTML.replace(/\t/g, "    "));

选择的解决方案

Or if you want to use CSS, this bookmarklet will insert the style rule into the page:

javascript:var r="pre{-moz-tab-size:4;}"; var s=document.createElement("style"); s.type="text/css"; s.appendChild(document.createTextNode(r)); document.body.appendChild(s); void 0;

(Note that at least for now, you need the -moz- prefix on tab-size in Firefox.)

Many thanks to jscher2000 for the CSS bookmarklet, which solves my specific problem and can be generalized to do many other things. Thanks also to cor-el and jscher2000 for other helpful comments. I may well want to change the editor used to view page source some time in the future.

Let me clear up a possible misunderstanding. Editra, my current favorite editor, does allow setting the tab width and choosing between hard and soft tabs. Altho very customizable in most respects, Editra will not let me set margins or headers/footers when printing. So I have been printing from Notepad and have gotten used to not having line numbers in printouts. By printing code as TXT browsed by Firefox and applying the CSS bookmarklet, I will get even more control over headers/footers, as well as the ability to print hard tabs of width other than 8.