Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-indentation for lists #126

Open
telemachus opened this issue Sep 11, 2014 · 44 comments
Open

Auto-indentation for lists #126

telemachus opened this issue Sep 11, 2014 · 44 comments

Comments

@telemachus
Copy link

Hi,

I believe that my problem is related to #78 and I'm pretty sure that this what I'm seeing is a deliberate choice (based on lines 41-43 of indent/mkd.vim), but I want to make sure.

If I'm writing Markdown and I start a list-item, when I hit return, the cursor is indented one level further in than the original list-item.

Example:

Here is some text.

+ Now I start a list. Soon I will hit enter.<CR>
    |<cursor is now here>

I don't understand this choice. As a user, when I'm done with the first list-item (and hit <CR>), I expect to start typing the next list-item. So shouldn't I be at the same indentation level, rather than further in? (If I want the next list-item to start a sub-list or be code, that's a separate choice. I would then <TAB> in further myself.)

tl;dr Is this observed indentation behavior deliberate? If so, why? Thanks.

@cirosantilli
Copy link
Collaborator

Weird, I don't reproduce: hitting enter for me creates the new item. I'm working on adding unit tests to this lib with https://github.com/junegunn/vader.vim: when that is done we'll create tests for this and decide =)

@telemachus
Copy link
Author

Ah, that is interesting. It may be another plugin interaction (though I don't use many) or maybe something to do with my format-options.

On the other hand, if I remove lines 41-43 from indent/mkd.vim, the problem goes away. So I would have sworn it was connected to vim-markdown. Then again, there are lots of variables involved.

Can I ask you what set fo shows for you? Thanks.

@cirosantilli
Copy link
Collaborator

set fo gives me tqr.

@telemachus
Copy link
Author

I start with tcqn1, but changing those options doesn't seem to matter.

I'll try with/without other plugins, but for now the only thing that works is to edit this plugin's indentation file.

@cirosantilli
Copy link
Collaborator

I have just tried it on a minimal config with: https://github.com/cirosantilli/vundle-plugin-tester and still don't reproduce.

To try it out, clone, ./test.sh a.md.

I'm on Vim 7.3.

@telemachus
Copy link
Author

I couldn't get vundle-plugin-tester to work at all, but I've found the problem by loading only this plugin and then playing with my ~/.vimrc. It's not a plugin interaction. Something here depends on syntax highlighting being turned on. I have syntax off at the top of my ~/.vimrc because I prefer no syntax highlighting.

@raine
Copy link

raine commented Oct 3, 2014

I have this issue.

@ryanoasis
Copy link

I have this issue as well

@blueyed
Copy link
Contributor

blueyed commented Nov 17, 2014

@telemachus
Make sure you have r in formatoptions. This is meant to start a new list entry on Enter (https://github.com/plasticboy/vim-markdown/blob/master/syntax/mkd.vim#L131-134).

Another issue here is the indentation of 4, which is introduced in 5cd2223.

This gets applied when automatically wrapping on textwidth.

TEST CASE:

  1. set tw=20
  2. Enter: - foo bar baz

RESULT:

- foo bar
    baz

@beyondwdq (who did the commit)
Was this intentional to chose 4 here?
I think it's better to use the length of the found start of the list item (including any whitespace).

But for what it's worth, there are mechanisms in Vim already to get this behavior:

let &formatlistpat='^\s*\d\+\.\s\+\|^[-*+]\s\+'  (from Vim's ftplugin/markdown.vim)
set formatoptions+=n

@blueyed
Copy link
Contributor

blueyed commented Nov 17, 2014

Anyone having problems with this: post the output of:

verb set flp fo ft inde comments tw

I could imagine that problems are based on using filetype mkd by default, and Vim's default markdown handling not being applied for you then (see #64).

I cannot see a reason for a custom indentexpr, except if you want the odd behavior of always identing by e.g. 4, instead of having the list aligned.

@telemachus
Copy link
Author

@blueyed

  formatlistpat=^\s*\d\+[\]:.)}\t ]\s*
        Last set from /usr/local/Cellar/vim/7.4.488/share/vim/vim74/ftplugin.vim
  formatoptions=tcqn1
  filetype=mkd
        Last set from ~/.vim/bundle/vim-markdown/ftdetect/mkd.vim
  indentexpr=GetMkdIndent()
        Last set from ~/.vim/bundle/vim-markdown/indent/mkd.vim
  comments=s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,fb:-
        Last set from /usr/local/Cellar/vim/7.4.488/share/vim/vim74/ftplugin.vim
  textwidth=79
        Last set from ~/.vimrc

I suspect you're right about the filetype issue, but even if I add r to my formatoptions manually, the problem remains. (For what it's worth, I've gone back to Tim Pope's vim-markdown, but I'm still curious about the problem.)

@blueyed
Copy link
Contributor

blueyed commented Nov 17, 2014

@telemachus
Your problem is that you're missing this from the syntax file:
https://github.com/plasticboy/vim-markdown/blob/master/syntax/mkd.vim#L131-L139

This does not belong into the syntax file, but should be in the filetype plugin.

For what it's worth, I've gone back to Tim Pope's vim-markdown

Makes sense, especially since it gets shipped with Vim.
I'll propose a PR, trying to clean this up a bit.

I'm still interested in feedback from @beyondwdq of course.

@telemachus
Copy link
Author

@blueyed

Your problem is that you're missing this from the syntax file

I'm confused: I just cloned the directory today, so why would I be missing those lines?

@blueyed
Copy link
Contributor

blueyed commented Nov 18, 2014

@telemachus
You have syntax off. You are not missing them in your checkout, but they do not get executed. See #144 - with that everybody is in your boat, but markdown gets sourced and only the funky indenting still causes problems.

@telemachus
Copy link
Author

@blueyed Ah, I'm sorry, but no: since my earlier post, I've given in and gone back to syntax on. I should have mentioned this, but didn't think of it. Apologies. (This markdown plugin wasn't the only thing that was wonky without it.)

So something else must be the issue, though I'm not sure what it is.

@raine
Copy link

raine commented Dec 31, 2014

Output for verb set flp fo ft inde comments tw:

  formatlistpat=^\s*\d\+[\]:.)}\t ]\s*
        Last set from /usr/local/Cellar/macvim/7.4-73_1/MacVim.app/Contents/Resources/vim/runtime/ftplugin.vim
  formatoptions=tcq
        Last set from /usr/local/Cellar/macvim/7.4-73_1/MacVim.app/Contents/Resources/vim/runtime/ftplugin.vim
  filetype=mkd
        Last set from ~/Dropbox/dotfiles/.vim/bundle/vim-markdown/ftdetect/mkd.vim
  indentexpr=GetMkdIndent()
        Last set from ~/Dropbox/dotfiles/.vim/bundle/vim-markdown/indent/mkd.vim
  comments=s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,fb:-
        Last set from /usr/local/Cellar/macvim/7.4-73_1/MacVim.app/Contents/Resources/vim/runtime/ftplugin.vim
  textwidth=77

@mauvm
Copy link

mauvm commented Jan 15, 2015

  formatlistpat=^\s*\d\+[\]:.)}\t ]\s*
  formatoptions=croql
        Last set from /usr/share/vim/vim73/ftplugin/vim.vim
  filetype=vim
        Last set from /usr/share/vim/vim73/filetype.vim
  indentexpr=GetVimIndent()
        Last set from /usr/share/vim/vim73/indent/vim.vim
  comments=sO:" -,mO:"  ,eO:"",:"
        Last set from /usr/share/vim/vim73/ftplugin/vim.vim
  textwidth=78
        Last set from /usr/share/vim/vim73/ftplugin/vim.vim

set fo gives me formatoptions=rtcqln

j5shi pushed a commit to j5shi/vim-markdown that referenced this issue Feb 15, 2015
@MWers
Copy link

MWers commented Mar 6, 2015

👍

  formatlistpat=^\s*\d\+\.\s\+\|^[-*+]\s\+
        Last set from /usr/share/vim/vim73/ftplugin/markdown.vim
  formatoptions=rtcqln
        Last set from /usr/share/vim/vim73/ftplugin/markdown.vim
  filetype=markdown
        Last set from ~/.vimrc
  indentexpr=GetMkdIndent()
        Last set from ~/Dropbox/Application Support/Dotfiles/.vim/bundle/vim-markdown/indent/mkd.vim
  comments=fb:*,fb:-,fb:+,n:>
        Last set from /usr/share/vim/vim73/ftplugin/markdown.vim
  textwidth=0

@sukima
Copy link

sukima commented Apr 24, 2015

This was referenced above but I don't see a reply.

List indent is mentioned in the original spec:

List items may consist of multiple paragraphs. Each subsequent paragraph in a list item must be indented by either 4 spaces or one tab

This allows a markdown parser to bundle multiple paragraphs in one list item. By aligning the text to follow suit you can make the switch to double digit numbers without offsetting the indent as shown:

8. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
9. quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
   Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
10. nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
    officia deserunt mollit anim id est laborum. 

VS

8.  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
9.  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
10. nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
    officia deserunt mollit anim id est laborum. 

@fredsherbet
Copy link

I'm hitting this issue, and found that it's because I remapped <CR> to <esc>o (i.e. whereever I am in the line, return starts a new line, rather than splitting the line)

When I removed the mapping, I get the correct list behaviour.

Does that give any clue to the problem? I'd quite like to keep my remapped return key, too...

@fredsherbet
Copy link

I worked around by using

inoremap <CR> <esc>A<CR>

@tdi
Copy link

tdi commented Sep 29, 2015

formatlistpat=^\s*\d\+\.\s\+\|^[-*+]\s\+
    Last set from /usr/share/vim/vim74/ftplugin/markdown.vim
formatoptions=tcqln
    Last set from /usr/share/vim/vim74/ftplugin/markdown.vim
filetype=mkd.markdown
    Last set from ~/.vim/bundle/vim-markdown/ftdetect/markdown.vim
indentexpr=GetMarkdownIndent()
    Last set from ~/.vim/bundle/vim-markdown/indent/markdown.vim
comments=fb:*,fb:-,fb:+,n:>
    Last set from /usr/share/vim/vim74/ftplugin/markdown.vim
textwidth=100
    Last set from ~/.vimrc

@thenerdlawyer
Copy link

I also am having this issue.

formatlistpat=^\s*\d\+\.\s\+\|^[-*+]\s\+
        Last set from /usr/local/Cellar/macvim/7.4-77/MacVim.app/Contents/Resources/vim/runtime/ftplugin
/markdown.vim
  formatoptions=tcqln
        Last set from /usr/local/Cellar/macvim/7.4-77/MacVim.app/Contents/Resources/vim/runtime/ftplugin
/markdown.vim
  filetype=markdown.pandoc
  indentexpr=GetMarkdownIndent()
        Last set from ~/.vim/bundle/vim-markdown/indent/markdown.vim
  comments=fb:*,fb:-,fb:+,n:>
        Last set from /usr/local/Cellar/macvim/7.4-77/MacVim.app/Contents/Resources/vim/runtime/ftplugin
/markdown.vim
  textwidth=0 

@arthurdent
Copy link

I moved some things around in my vimrc and now things are working correctly.

filetype plugin indent on needs to be above syntax enable or syntax on

Here is the top of my working vimrc:

execute pathogen#infect()
filetype plugin indent on
syntax enable

Removing filetype plugin indent on from my vimrc fixed the problem, but that wasn't a good solution.

Alternatively, I was able to fix it by manually issuing :syntax enable in vim. I thought this was weird because it was already in my vimrc, but eventually I put two and two together.

@sukima
Copy link

sukima commented Oct 7, 2015

@arthurdent I think this might be relevant: VundleVim/Vundle.vim#176

It is Vundle not pathogen but I think I recall them both having the same gotcha.

P.S. 👍 on the GitHub username.

@danawoodman
Copy link

@arthurdent moving filetype plugin indent on did not work for me. Are you sure that was the only change you made? Commenting it out did work however.

FWIW, my :verb set flp fo ft inde comments tw output:

  formatlistpat=^\s*\d\+[\]:.)}\t ]\s*
        Last set from ~/.vim/view/~=+.vimrc=
  formatoptions=jcroql
        Last set from ~/.vim/view/~=+.vimrc=
  filetype=vim
        Last set from /usr/local/Cellar/neovim/HEAD/share/nvim/runtime/filetype.vim
  indentexpr=GetVimIndent()
        Last set from ~/.vim/view/~=+.vimrc=
  comments=sO:" -,mO:"  ,eO:"",:"
        Last set from ~/.vim/view/~=+.vimrc=
  textwidth=500
        Last set from ~/.vim/view/~=+.vimrc=

@borismus
Copy link

Also seeing this. What's the solution?

@arthurdent
Copy link

@danawoodman The only other thing I did is disable folding with let g:vim_markdown_folding_disabled=1, which I also added before syntax enable. I should definitely mention that using o or O to enter insert mode still results in an indent instead of a new item. Only pressing return in insert mode adds an item. I am not sure if this is the intended behavior.

@flight16
Copy link

I'm also getting this. The only solution for me was to remove "filetype plugin indent on" from vimrc. : (

This is really, really annoying. I have stayed away from this plugin for years because of this behavior.

I'm using Vim 7.4.258 on OS X.

@flight16
Copy link

As a work-around to only disable plugin indents with markdown, I added the following to my .vimrc:

autocmd BufNewFile,BufRead *.md filetype plugin indent off

With the number of people posting in this thread, maybe it should go in the README.md so people find it easier?

@telemachus
Copy link
Author

@shirosaki An alternative is to switch to tpope's markdown plugin.

@j5shi
Copy link

j5shi commented May 29, 2016

@blueyed I'm having a similar issue, but in my case both * and + work well, except -, which will get over indented by 2 spaces.

  formatlistpat=^\s*[0-9*-+]\+[\]:.)}\t ]\s*\|^\s*\[[x\- ]\]\s\+\|^\s*\[[x -]\]\s\+
    Last set from c:\Program Files (x86)\Vim\_vimrc
  formatoptions=Bjtqlnr
    Last set from c:\Program Files (x86)\Vim\vimfiles\bundle\vim-markdown\indent\markdown.vim
  filetype=markdown
    Last set from c:\Program Files (x86)\Vim\vimfiles\bundle\vim-markdown\ftdetect\markdown.vim
  indentexpr=GetMarkdownIndent()
    Last set from c:\Program Files (x86)\Vim\vimfiles\bundle\vim-markdown\indent\markdown.vim
  comments=b:>
    Last set from c:\Program Files (x86)\Vim\_vimrc
  textwidth=100
    Last set from c:\Program Files (x86)\Vim\_vimrc

@djhaskin987
Copy link

Still a problem in 2017. I'm on a mac, I just cloned this into my bundle directory. I'm using pathogen.

@for-coursera
Copy link

for-coursera commented Jul 4, 2017

How about adding something like the following to after/ftplugin/markdown.vim?

" helper to detect if you are in a dash-list or not
" because it's a bug: https://github.com/plasticboy/vim-markdown/issues/126
function! s:InAListOrNot()
    let the_line = getline(".")
    return the_line =~ '^ *-' ? 1 : 0
endfunction
" then map i_<CR> and o to i_<CR>_<BS> and o<BS> accordingly if yes, and leave as is if not
inoremap <expr> <CR> <SID>InAListOrNot() ? '<CR><BS>' : '<CR>'
nnoremap <expr> o <SID>InAListOrNot() ? 'o<BS>' : 'o'

@gpanders
Copy link

gpanders commented Apr 22, 2019

I use this plugin indirectly via vim-polyglot and am seeing the same problem. The solution for me was to clear indentexpr:

setlocal indentexpr=

Specifically, the issue in the function GetMarkdownIndent() seems to be here:

    elseif s:IsLiStart(line)
        if s:IsMkdCode(lnum)
            return ind
        else
            " Last line is the first line of a list item, increase indent
            return ind + list_ind
        end
    else
        return ind
    endif

If the current line (when you press the Return key) is the first line of a list item (e.g. starts with a - ), then the new line will have the indent of the current line plus the value of list_ind (which is g:vim_markdown_new_list_item_indent or 4 if not set). Commenting out that elseif block also solves the problem.

For whatever else it's worth, I only see this issue when g:vim_markdown_auto_insert_bullets is 0.

@akarzim
Copy link

akarzim commented May 22, 2020

Same here using vim-polyglot too. My :verb set flp fo ft inde comments tw output was:

  formatlistpat=^\s*\d\+\.\s\+\|^[-*+]\s\+\|^\[^\ze[^\]]\+\]:
        Modifié la dernière fois dans /usr/local/Cellar/neovim/0.4.3/share/nvim/runtime/ftplugin/markdown.vim line 14
  formatoptions=jtcqln
        Modifié la dernière fois dans /usr/local/Cellar/neovim/0.4.3/share/nvim/runtime/ftplugin/markdown.vim line 13
  filetype=markdown
        Modifié la dernière fois dans ~/.local/share/nvim/plugged/vim-polyglot/ftdetect/polyglot.vim line 379
  indentexpr=GetMarkdownIndent()
        Modifié la dernière fois dans ~/.local/share/nvim/plugged/vim-polyglot/indent/markdown.vim line 6
  comments=fb:*,fb:-,fb:+,n:>
        Modifié la dernière fois dans /usr/local/Cellar/neovim/0.4.3/share/nvim/runtime/ftplugin/markdown.vim line 12
  textwidth=80
        Modifié la dernière fois dans ~/git/vim/after/ftplugin/markdown.vim line 2

And my workaround is to set this conf in my after/ftplugin/markdown.vim file:

" r Automatically insert the current comment leader after hitting
"   <Enter> in Insert mode.
" o Automatically insert the current comment leader after hitting 'o' or
"   'O' in Normal mode.
set formatoptions+=ro

" The 'comments' option is a comma-separated list of parts.
" Each part defines a type of comment string.
" A part consists of: {flags}:{string}

" {string} is the literal text that must appear.

" {flags}:
"   n Nested comment.  Nesting with mixed parts is allowed.
"   b Blank (<Space>, <Tab> or <EOL>) required after {string}.
set comments=b:*,b:-,b:+,b:1.,n:>

Note about the removed f flag:

  f     Only the first line has the comment string.  Do not repeat comment on
        the next line, but preserve indentation (e.g., a bullet-list).

bonus point: thanks to b:1. it handles numeric lists well too ;)

@pwc3
Copy link

pwc3 commented Jun 8, 2020

Based on @akarzim's post above, I was able to work around this issue by adding the following to my .vimrc

au filetype markdown set formatoptions+=ro
au filetype markdown set comments=b:*,b:-,b:+,b:1.,n:>

@bendman
Copy link

bendman commented Oct 2, 2020

The proposed solution from @pwc3 fixed my list indentation both for return and o/O.

@jchook
Copy link

jchook commented May 10, 2021

Using this hack for now:

autocmd filetype markdown set indentexpr=

The fix from @pwc3 works for me with the cursor on a list item, but not on the empty line after the list:

- List item 1
- List item 2
- List item 3
█ <normal cursor here, press o>
- List item 1
- List item 2
- List item 3

    | <insert cursor here now>

Output of verb set flp fo ft inde comments tw:

  formatlistpat=^\s*\d\+\.\s\+\|^[-*+]\s\+\|^\[^\ze[^\]]\+\]:
        Last set from /usr/share/nvim/runtime/ftplugin/markdown.vim line 14
  formatoptions=jtqlnro
        Last set from ~/.dotfiles/editor/nvim/init.vim line 616
  filetype=markdown
        Last set from ~/.dotfiles/editor/nvim/init.vim line 599
  indentexpr=GetMarkdownIndent()
        Last set from ~/.dotfiles/editor/nvim/plugged/vim-polyglot/indent/markdown.vim line 8
  comments=b:*,b:-,b:+,b:1.,n:>
        Last set from ~/.dotfiles/editor/nvim/init.vim line 617
  textwidth=0

@devstefancho
Copy link

Using this hack for now:

autocmd filetype markdown set indentexpr=

this works for me.
(when pressing o then cursor start from same level of upper list level)

@alerque
Copy link
Member

alerque commented May 19, 2022

Re-openening to (eventually) review why people think they still have a problem with this after a supposed fix was merged.

@alerque alerque reopened this May 19, 2022
@jodavaho
Copy link

jodavaho commented Feb 14, 2023

I still have this issue:

- List item 1
- List item 2
- List item 3
| <insert cursor here, press enter>
- List item 1
- List item 2
- List item 3

    | <insert cursor here now>

And autocmd BufNewFile,BufRead *.md filetype plugin indent off at bottom of .vimrc solved it.

@pizdjuk
Copy link

pizdjuk commented Jun 5, 2023

A have the same issue. Fixed with syntax on. But the problem, that the line wrapping still does not work properly (not indenting)

@Gullumluvl
Copy link

Hi, same issue.

Re-openening to (eventually) review why people think they still have a problem with this after a supposed fix was merged.

@alerque, what was the supposed fix? The commit 642730f on Nov 16, 2015?

For me the solution was to comment out parts of GetMarkdownIndent as indicated by @gpanders above.

Only adding the line autocmd BufNewFile,BufRead *.md filetype plugin indent off at the end of the .vimrc did not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests