1

I want to set my markdown files to have textwidth=100 but cannot get it working. The strange thing is that it works sometimes and sometimes not. My autocmd is:

augroup auFileTypes
  autocmd! auFileTypes
  audocmd BufNewFile,BufRead *.md set filetype=markdown | setlocal textwidth=100
augroup end

Some files (such as create_template.md or index.md) just work; some files (such as completed_jobs.md or issues.md) don't work.

I thought at first it might be a "sequencing" problem such as every other one works, but that turned out not to be the case.

I'm on version 8.1-650, via Homebrew.

UPDATE: When I load the file completed_jobs.md, :setlocal says this:

--- Local option values ---
  autoindent          cryptmethod=        errorformat=        grepprg=            linebreak           number              scroll=33           sidescrolloff=-1    syntax=markdown
--autoread            cursorline          expandtab           include=            lispwords=          path=               scrolloff=-1        softtabstop=2       tabstop=2
  backupcopy=         define=             filetype=markdown   keywordprg=         makeprg=            relativenumber      shiftwidth=2      noswapfile            tagcase=
  cinkeys=0{,0},0),:,0#,!^F,o,O,e
  comments=fb:*,fb:-,fb:+,n:>
  commentstring=<!--%s-->
  fileencoding=utf-8
  formatlistpat=^\s*\d\+\.\s\+\|^[-*+]\s\+\|^\[^\ze[^\]]\+\]:
  indentkeys=0{,0},:,0#,!^F,o,O,e
  iskeyword=@,48-57,_,192-255,$
  tags=./tags,tags,~/workspace/automation-ui-core-etap/tags
  undolevels=-123456

and `onboarding.md :setlocal says this:

--- Local option values ---
  autoindent          cryptmethod=      noendofline           filetype=markdown   keywordprg=         makeprg=            relativenumber      shiftwidth=2      noswapfile            tagcase=
--autoread            cursorline          errorformat=        grepprg=            linebreak           number              scroll=33           sidescrolloff=-1    syntax=markdown     textwidth=100
  backupcopy=         define=             expandtab           include=            lispwords=          path=               scrolloff=-1        softtabstop=2       tabstop=2
  cinkeys=0{,0},0),:,0#,!^F,o,O,e
  comments=fb:*,fb:-,fb:+,n:>
  commentstring=<!--%s-->
  fileencoding=utf-8
  formatoptions=tcqln
  formatlistpat=^\s*\d\+\.\s\+\|^[-*+]\s\+\|^\[^\ze[^\]]\+\]:
  indentkeys=0{,0},:,0#,!^F,o,O,e
  iskeyword=@,48-57,_,192-255,58,-,$
  tags=./tags,tags,~/workspace/automation-ui-core-etap/tags
  undolevels=-123456

Only difference I can see (besides the missing textwidth=100) is the noendofline setting. However, even that is not consistent across the various successes and failures.

3
  • 1
    First, you shouldn't need auFileTypes after autocmd! (I'm not sure it has an impact but I had not seen this syntax before). Second, why do you use set filetype in your autocommand? I think the built-in plugin should set the filetype correctly by itself. Finally rather than using an autocommand you could simply create ~/.vim/after/ftplugin/markdown.vim and put setlocal textwidth=100 in it, this way you let the built in mechanisms do the work without having to mess with autocommands.
    – statox
    Feb 19, 2019 at 15:22
  • Actually, as I understand it, vim defaults to Modula-2 for *.md files, except for README.md. Yes, I could do a plugin, but I prefer the autocmd approach so it's all visible in my .vimrc file.
    – JESii
    Feb 19, 2019 at 15:42
  • 2
    According to filetype.vim from the vim repository, all md files are automatically detected as markdown files. Do you have a :filetype command in your .vimrc? Feb 19, 2019 at 17:01

1 Answer 1

0

Files matching *.md are detected as markdown. The extension md was removed from Modula and "given" to Markdown. So you don't need to act on file extensions, but you can use the FileType event.

Change your vimrc to contain:

augroup auFileTypes
  autocmd!
  autocmd FileType markdown setlocal textwidth=100
augroup end

This autocommand is executed when the file type "markdown" is detected.

If this doesn't work as expected, reduce your Vim setup to only contain the four lines above and test. Then slowly build up your Vim setup again, until you find the part that interferes with above autocommands.


As statox mentioned in the comments, there is a defined way to activate filetype specific settings, by creating a file in the directory ~/.vim/after/ftplugin with the file name like the file type (~/.vim/after/ftplugin/markdown.vim).

I have to admit, that if you only want to change one option, this is oversized.

6
  • Tried that, but unfortunately it doesn't solve the problem. I'm still getting some of them with textwidth=100 and some of them not.
    – JESii
    Feb 19, 2019 at 19:14
  • @JESii See update "If this doesn't work ..."
    – Ralf
    Feb 19, 2019 at 19:30
  • yeah; I was hoping it wouldn't come down to that :-| I'll let you know what I find!
    – JESii
    Feb 19, 2019 at 20:09
  • @JESii Sorry :-) Perhaps you could at first disable all autocmds except the above.
    – Ralf
    Feb 19, 2019 at 20:11
  • 1
    I would argue that using a ftplugin plugin for a single option is not necessary oversized because 1) it is more efficient than creating loads of autocommands (since the mechanism is built-in) 2) it scales well (and chances are that you will probably add more settings with the time) 3) it integrates pretty well in a dotfiles versionning system. Also I recommand reading this reddit wiki which is pretty interesting for this topic.
    – statox
    Feb 21, 2019 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.