80-characters / right margin line in Sublime Text 3
Asked Answered
C

2

311

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.?

Certificate answered 17/9, 2014 at 21:52 Comment(0)
I
606

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 or 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 (called glyphs), 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).

Ilocano answered 17/9, 2014 at 22:0 Comment(11)
Thanks. Removed your bolding, cause it kick my eyeballs! :] Plus, added some extra info, though all the glory goes to you, of course.Certificate
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?)Empirin
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.Nealey
@Empirin I use a plugin (github.com/ehuss/Sublime-Wrap-Plus) to do word wrap with a key binding (defaults to Alt-Q on Linux). Seems to work pretty well for me.Smokedry
Why on earth does a wrap width of 80 characters wrap after the 79th character? Am I doing something stupid?!Dorrie
@BobbyJack I realised that too. :) Just use 81 for wrap width. :)Cullin
@BobbyJack my guess would be zero is the first element, classic offset problem!Whoever
Note that if you're using sublime-project files, you should place these settings inside a "settings": { } block.Isobelisocheim
@Ilocano I've tried setting the rulers, and for some reason it only works on html files. Or maybe in Group1 not Group2 All my .py files are stuck on 80, in Group2, and when I've attempted making a python specific file, that doesn't work either. Any ideas what gives? CheersSorgo
@Ilocano I've been manually trying to keep my documents at 80 characters for so long, I never knew about word_wrap... life-changing. You are the bomb-diggity.Risser
@Empirin OMG I had no idea you could even do this manually Edit > Wrap paragraph at 72 characters!!! You just saved me 60 seconds for every commit message I write! Thank ye!!! (And you probably already see the keyboard shortcut for wrapping at ruler as cmd+option+q).Sheers
S
14

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.

Shudder answered 12/12, 2015 at 1:6 Comment(1)
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.Certificate

© 2022 - 2024 — McMap. All rights reserved.