80-characters / right margin line in Sublime Text 3

132,787

Solution 1

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

Solution 2

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.

Share:
132,787

Related videos on Youtube

trejder
Author by

trejder

I'm just an average person, that uniquely combines strict, engineering mind with a non-schematic, humanistic and magical way of looking on life and surrounding world. Add a little bit of madness, and you'll get entire picture of me! :] I'm a developer, both desktop and mobile, with 15+ years of experience and 50+ small-to-big projects on account. In the very same time I'm a writer with similar experience and a lot of books, articles, blog posts or own blogs ideas on account.

Updated on June 21, 2021

Comments

  • trejder
    trejder about 3 years

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

  • trejder
    trejder almost 10 years
    Thanks. Removed your bolding, cause it kick my eyeballs! :] Plus, added some extra info, though all the glory goes to you, of course.
  • evolvedmicrobe
    evolvedmicrobe almost 9 years
    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?)
  • ptim
    ptim almost 9 years
    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.
  • Tim Destan
    Tim Destan almost 9 years
    @evolvedmicrobe 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.
  • Bobby Jack
    Bobby Jack over 8 years
    Why on earth does a wrap width of 80 characters wrap after the 79th character? Am I doing something stupid?!
  • trejder
    trejder over 8 years
    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.
  • Koray Tugay
    Koray Tugay over 8 years
    @BobbyJack I realised that too. :) Just use 81 for wrap width. :)
  • engineerDave
    engineerDave about 8 years
    @BobbyJack my guess would be zero is the first element, classic offset problem!
  • Tom
    Tom almost 8 years
    Note that if you're using sublime-project files, you should place these settings inside a "settings": { } block.
  • Chockomonkey
    Chockomonkey almost 7 years
    @MattDMo 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? Cheers
  • MTen
    MTen over 6 years
    @MattDMo 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.
  • elbowlobstercowstand
    elbowlobstercowstand over 5 years
    @evolvedmicrobe 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).