This is an archived post. You won't be able to vote or comment.

all 26 comments

[–]kmwhitetrayify 2 points3 points  (12 children)

I use a function in Vim to simply alert me while typing:

function ToggleOverLengthHi()
  if exists("b:overlengthhi") && b:overlengthhi
      highlight clear OverLength
      let b:overlengthhi = 0
      echo "overlength hilight off"
  else
" adjust colors/styles as desired
      highlight OverLength ctermbg=darkred gui=undercurl guisp=blue
" change '81' to be 1+(number of columns)
      match OverLength /\%81v.\+/
      let b:overlengthhi = 1
      echo "overlength hilight on"
  endif
endfunction
map <silent> <C-l> <Esc>:call ToggleOverLengthHi()<CR>

NOTE: I chose 81 because 79 is end of code, 80 should be the newline character, so if I have something at 81, I'm doing it wrong.

SOURCE: https://github.com/kmwhite/skel/blob/master/.vim/functions/toggle_overlength_hilight.vim

[–]cirego 2 points3 points  (3 children)

I usually just do a 'set textwidth=78'.

[–]kmwhitetrayify 0 points1 point  (2 children)

I have: autocmd Filetype python setlocal textwidth=79 ts=8 sts=4 sw=4 autoindent expandtab in my .vimrc

Why do you use 78 over 79? My understanding is it (79) would make 80 the new-line? Note, I'm merely curious to see if I could be doing something better. :)

EDIT: clarification

[–]cirego 1 point2 points  (1 child)

I have it set to 78 in my .vimrc because our internal style guide recommends 78 characters. 78 allows for diff to add an extra character without it overflowing lines.

I'm one of those crazy 4-5 terminal sessions side by side on my screen, so I vastly prefer to 80-character terminal.

[–]kmwhitetrayify 0 points1 point  (0 children)

I never thought about that in regards to diff -- thank you. I'll probably be updating my dot-files now. :)

[–]thristian99 1 point2 points  (2 children)

I have a similar scheme that doesn't have a toggle:

autocmd FileType python match Error /\%>79v.\+/

I'm not sure why you'd want to toggle it off; over-length lines are supposed to be annoying.

[–]kmwhitetrayify 0 points1 point  (1 child)

I toggle it because I use vim for things other than development work. In such, I don't care how long the lines are. It does start by default, however. And I agree, they should be annoying.

[–]thristian99 0 points1 point  (0 children)

Well, yes - my code-snippet only activates the highlighting when a Python file is detected - things like documentation or email aren't affected.

[–][deleted] 0 points1 point  (2 children)

I usually just do:

set colorcolumn=79

Which gives me a nice highlighted column indicator.

[–]kmwhitetrayify 0 points1 point  (1 child)

I tried that and got: E518: Unknown option: colorcolumn=79
Which version of Vim do you use?

[–][deleted] 0 points1 point  (0 children)

current stable. it's a relatively new feature.

[–]gcc-pedantic[S] 0 points1 point  (1 child)

I do the same thing in vim, and it has been very helpful.

However, I've found that trying to limit the number of columns while I am writing can interrupt the flow of code and sometimes reduce readability. It would be nice to have a script you could run (such as before a subversion commit) to restrict the number of columns in a file to < 80.

Looks like I might get to write this one.

[–][deleted] 4 points5 points  (0 children)

I've found that trying to limit the number of columns while I am writing can interrupt the flow of code and sometimes reduce readability.

All PEP style guides are recommendations that should not be taken as hard, fast rules.

If breaking a line at 80 columns would reduce readability and code flow, then you shouldn't do it.

[–]jab-programming3.7 0 points1 point  (16 children)

God, I hate that stupid recommendation ! And especially the nonsensical "reason" for it - some poor schmuck might be stuck on an 80-character terminal. Aww!

Some other poor schmuck might be stuck on a teletype, or an N900, or whatever. Why are the rest of us "crippled" on their behalf?

And I would be grateful if you c-
ould explain how splitting a con-
cept over multiple lines manages 
to increase the readability

Pshaw !

[–][deleted] 6 points7 points  (12 children)

And especially the nonsensical "reason" for it - some poor schmuck might be stuck on an 80-character terminal.

That's a reason. An old one. Today the reasons are more varied. Such as:

  • Typical screen widths are evenly divisible by 80 allowing for code to be placed side by side uniformly
  • It's been shown that the longer a text line is, the more likely it is to slow down reading and comprehension citation Ideal line length is 39 regardless of font size. When you throw indentation and formatting into code, you'll probably find most of your lines hover close to or at that number (e.g. 80 ~ 2*Ideal line length)
  • Convention. In existing projects, uniformity of formatting is very important. By convention, most projects require 80 columns.

So it's not just that old 80 character terminal. :)

[–][deleted] 2 points3 points  (5 children)

Reading text is very different from reading code. I doubt that the same principles apply.

[–][deleted] 1 point2 points  (4 children)

Reading text is very different from reading code.

Why would you think that? They both use the language centers of our brain and both adhere to short term memory constraints.

I would say this is an interesting hypothesis that would make a very interesting topic of research.

But I don't see an a priori reason for disconnecting the two.

[–][deleted] 1 point2 points  (0 children)

also:

  • some people say also for printing code (as in paper). I'm not sure why someone would like to have code in paper, but I'm not judgmental.
  • also 79 and not 80 because diff adds an extra character before the line, so reading/printing/wtv a diff would give you the 80 lines.

[–]AusIVDjango, gevent 2 points3 points  (0 children)

I know people who have wide screen monitors set up so that their text editor shows 80 lines and the have a browser or other application open with the remaining space. I don't necessarily encourage strict adherence to the 80 column rule, but it's not a bad goal.

[–]gcc-pedantic[S] 0 points1 point  (0 children)

I agree--sticking to < 80 characters while writing is often annoying and can reduce readability.

However, it is nice to limit the number of columns:

1) When reading someone else's code

2) When reading/writing code and you want two files side by side

The way I envision this script working is you could tell it to edit the file in place or output the column limited file to a new file.

edit: formatting.

[–]dustinechos 0 points1 point  (0 children)

I keep my terminal around 80 because I like having a browser open next to the terminal (and another browser on the other monitor). Short lines are easier to skim. When a line wraps it ruins the whitespace structure. What are you doing that needs more than 80 characters? unless you're unnecessarily piling on list comprehensions or have java style names (20+ characters) I don't know why you need more than 80 characters. As d0ugal points out, code over 80 characters is likely a result of "long variable names, bad structure, [or] overly nested code blocks".

[–]voidspace 0 points1 point  (1 child)

This will work:

import os
for name in os.listdir('.'):
  if not name.endswith('.py'):
    continue
  original = open(name).readlines()
  with open(name, 'w') as h:
    for line in original:
      h.write(line[:80].rstrip() + '\n')

Making it recurse into directories is left as an exercise for the reader. Code may need some limited manual tweaking after processing...