Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So, I've never done any Vim scripting before, but based on <a href="https://stackoverflow.com/questions/3475072/vim-different-textwidth-for-multiline-c-comments">this question about doing something similar in C</a> and <a href="http://vim.wikia.com/wiki/Check_for_comments_independent_of_filetype" rel="nofollow noreferrer">this tip for checking if you're currently in a comment</a>, I've hacked together a solution.</p> <p>By default, this uses the PEP8-suggested widths of 79 characters for normal lines and 72 characters for comments, but you can override them by <code>let</code>ting <code>g:python_normal_text_width</code> or <code>g:python_comment_text_width</code> variables, respectively. (Personally, I wrap normal lines at 78 characters.)</p> <p>Drop this baby in your <em>.vimrc</em> and you should be good to go. I may package this up as a plugin later.</p> <pre><code>function! GetPythonTextWidth() if !exists('g:python_normal_text_width') let normal_text_width = 79 else let normal_text_width = g:python_normal_text_width endif if !exists('g:python_comment_text_width') let comment_text_width = 72 else let comment_text_width = g:python_comment_text_width endif let cur_syntax = synIDattr(synIDtrans(synID(line("."), col("."), 0)), "name") if cur_syntax == "Comment" return comment_text_width elseif cur_syntax == "String" " Check to see if we're in a docstring let lnum = line(".") while lnum &gt;= 1 &amp;&amp; (synIDattr(synIDtrans(synID(lnum, col([lnum, "$"]) - 1, 0)), "name") == "String" || match(getline(lnum), '\v^\s*$') &gt; -1) if match(getline(lnum), "\\('''\\|\"\"\"\\)") &gt; -1 " Assume that any longstring is a docstring return comment_text_width endif let lnum -= 1 endwhile endif return normal_text_width endfunction augroup pep8 au! autocmd CursorMoved,CursorMovedI * :if &amp;ft == 'python' | :exe 'setlocal textwidth='.GetPythonTextWidth() | :endif augroup END </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload