ChatGPT解决这个技术问题 Extra ChatGPT

80-characters / right margin line in Sublime Text 3

You can have 80-characters / right margin line in Netbeans, Text Mate and probably many, many more other IDEs. Is it possible to have it in Sublime Text 3 as well? Any option, plugin etc.?


M
MattDMo

Yes, it is possible in Sublime Text 2, ST3, and ST4 (which you should really upgrade to if you haven't already). Select View → Ruler → 80 (there are several other options there as well). If you like to actually wrap your text at 80 columns, select View → Word Wrap Column → 80. Make sure that View → Word Wrap is selected.

To make your selections permanent (the default for all opened files or views), open Preferences → Settings and use any of the following rules in the right-side pane:

{
    // set vertical rulers in specified columns.
    // Use "rulers": [80] for just one ruler
    // default value is []
    "rulers": [80, 100, 120],

    // turn on word wrap for source and text
    // default value is "auto", which means off for source and on for text
    "word_wrap": true,

    // set word wrapping at this column
    // default value is 0, meaning wrapping occurs at window width
    "wrap_width": 80
}

These settings can also be used in a .sublime-project file to set defaults on a per-project basis, or in a syntax-specific .sublime-settings file if you only want them to apply to files written in a certain language (Python.sublime-settings vs. JavaScript.sublime-settings, for example). Access these settings files by opening a file with the desired syntax, then selecting Preferences → Settings—Syntax Specific.

As always, if you have multiple entries in your settings file, separate them with commas , except for after the last one. The entire content should be enclosed in curly braces { }. Basically, make sure it's valid JSON.

If you'd like a key combo to automatically set the ruler at 80 for a particular view/file, or you are interested in learning how to set the value without using the mouse, please see my answer here.

Finally, as mentioned in another answer, you really should be using a monospace font in order for your code to line up correctly. Other types of fonts have variable-width letters, which means one 80-character line may not appear to be the same length as another 80-character line with different content, and your indentations will look all messed up. Sublime has monospace fonts set by default, but you can of course choose any one you want. Personally, I really like Liberation Mono. It has glyphs to support many different languages and Unicode characters, looks good at a variety of different sizes, and (most importantly for a programming font) clearly differentiates between 0 and O (digit zero and capital letter oh) and 1 and l (digit one and lowercase letter ell), which not all monospace fonts do, unfortunately. Version 2.0 and later of the font are licensed under the open-source SIL Open Font License 1.1 (here is the FAQ).


Thanks. Removed your bolding, cause it kick my eyeballs! :] Plus, added some extra info, though all the glory goes to you, of course.
Is it possible to hard wrap here? e.g. the equivalent of it automatically doing edit->wrap->wrap paragraph at ruler, but without having to go through this process (or the equivalent keyboard shortcut?)
great, tx! I only want a ruler in my python files; you can add syntax specific settings by opening a .py file, then Preferences>Settings More>Syntax Specific - User.
Why on earth does a wrap width of 80 characters wrap after the 79th character? Am I doing something stupid?!
@BobbyJack I realised that too. :) Just use 81 for wrap width. :)
C
Community

For this to work, your font also needs to be set to monospace. If you think about it, lines can't otherwise line up perfectly perfectly.

This answer is detailed at sublime text forum:
http://www.sublimetext.com/forum/viewtopic.php?f=3&p=42052
This answer has links for choosing an appropriate font for your OS,
and gives an answer to an edge case of fonts not lining up.

Another website that lists great monospaced free fonts for programmers. http://hivelogic.com/articles/top-10-programming-fonts

On stackoverflow, see:

Michael Ruth's answer here: How to make ruler always be shown in Sublime text 2?

MattDMo's answer here: What is the default font of Sublime Text?

I have rulers set at the following: 30 50 (git commit message titles should be limited to 50 characters) 72 (git commit message details should be limited to 72 characters) 80 (Windows Command Console Window maxes out at 80 character width)

Other viewing environments that benefit from shorter lines: github: there is no word wrap when viewing a file online So, I try to keep .js .md and other files at 70-80 characters. Windows Console: 80 characters.


Thanks for your answer. To be honest, I can't imagine coding in ST3 with non-monospace font. That's why this was pretty obvious to me, that monospace font is required.