2

I am writing a lot of latex and would like to conceal comments. There are often several lines of text that I comment but would like to keep/not delete.

Concealing comments explained: A comment is longer than a given line is spread over multiple lines. I would like to always shorten the comment at the end of the line and only show ".." or a symbol. When the cursor moves over the comment, I would like it to unfold again with all the line breaks. Conceal as soon as the cursor leaves the line.

Unconcealed (.. representing line break):

% asdffdsaaasdffdsa
  ..lbfds

Concealed:

% asdffdsaaasffd..

An advanced feature would be the concealing of several consecutive commented lines to collapse and end on ..[#collapsed_lines].

Unconcealed (.. representing line break):

% asdffdsaaasdffdsa
  ..lbfds
% asdfsdsdsss

Concealed:

% asdffdsaaasd..[2]

I am using Vimtex. Is there such a plugin or feature within Vimtex I haven't discovered or understood, allowing me to do so?

0

2 Answers 2

2

Vim natively supports folding lines of text. See :help folds for the whole documentation.

Here's a quick solution I hacked together using the sample code already present in :help fold-expr as a starting point. Put the lines below in a file used for LaTeX, e.g. ~/.vim/after/ftplugin/latex.vim:

set foldexpr=match(getline(v:lnum),'^\s*%')!=-1
set foldmethod=expr

It will make this text:

Some text
% this is a comment
% so is this
Some more text

look like this:

Some text
+-- 2 lines: % this is a comment---------------------------------------
Some more text

I would say that's reasonably close to your requirements.

You will have noticed that this is an answer for original Vim. You may have to translate it into neovim's syntax. I keep on hearing that neovim is much more advanced so this should be a no-brainer. I hope it's at least a pointer in the right direction.

Addendum:

I just read the question again and there seems to be another problem: you are talking about multi-line comments. AFAIK, LaTeX does not support multi-line comments so this is really just one very long line that is (visually) wrapped by Vim. No newline character is inserted. You can turn it off with :set nowrap, which will give you very long lines. See :help 'wrap'.

7
  • Great, works like a charm (you didn't miss multiline - I was talking about multiple lines of consecutive single line comments). I added it like this: vim.cmd [[ augroup filetype_tex autocmd! autocmd FileType tex setlocal foldmethod=expr foldexpr=getline(v:lnum)[0]=='%' augroup END ]] What do I need to add to take comments into account that have been indented by one or multiple spaces or tabs?
    – baxbear
    Commented Apr 19, 2023 at 10:42
  • 2
    I had a feeling this question would come. The original version was a lazy rip-off of Vim's doc with one character changed. I edited my answer with a version that will fold any number of whitespace followed by a percent sign.
    – Friedrich
    Commented Apr 19, 2023 at 11:11
  • 1
    I fiddled around with the documentation for 15 min and failed. Then I thought, maybe you are kind and I save time if I carefully ask about it since you've already got a feel for it :D - thanks a lot!
    – baxbear
    Commented Apr 19, 2023 at 11:30
  • 1
    The problem with SE is that once you post an answer, you commit yourself to assist with follow-up questions. It's a kind of moral obligation. However, my original foldexpr was flawed and it's a good thing I had to improve it.
    – Friedrich
    Commented Apr 19, 2023 at 11:40
  • 1
    Nah, the best practice is to make the OP post another question which you answer immediately to squeeze out another cheap 25 rep :-D Now back to work...
    – Friedrich
    Commented Apr 19, 2023 at 12:11
3

Enable folding of comments with the option g:vimtex_fold_types

Since you are already using VimTeX, have you tried to set

" .vimrc
let g:vimtex_fold_enabled = 1
let g:vimtex_fold_types = {
    \  'comments': {'enabled' : 1},
    \}
-- init.lua
vim.g.vimtex_fold_enabled = true
vim.g.vimtex_fold_types = {
  comments = { enabled = true }
}

Example with set foldtext=foldtext()

Some text
% this is a comment
% with many lines
% with many lines
% with many lines
% with many lines
% with many lines
Some more text

is shown to me as

enter image description here

For more info :h vimtex-fold

8
  • Doesn't really work - was also hoping to be able to rely on it.
    – baxbear
    Commented Apr 19, 2023 at 10:44
  • 1
    What does not really work? Are the folds not created at all or do you not like the foldtext?
    – Hotschke
    Commented Apr 19, 2023 at 11:00
  • Hotschke - it randomly folded parts of my document e.g. it folded from the start of the document (\section was in the first line) to the third item of an itemize 15 lines below it. So, not sure what doesn't work. But the solution of @Friedrich works great, so I've gone with that.
    – baxbear
    Commented Apr 19, 2023 at 11:27
  • 3
    I see. In this case it would be more sensible to raise an issue on the issue tracker of vimtex (github.com/lervag/vimtex/issues) since you already have an example. Afaik, vimtex works best with its own syntax file and not treesitter. However, if the solution by Friedrich works for you, you probably want to go on with what you are actually doing.
    – Hotschke
    Commented Apr 19, 2023 at 11:50
  • 1
    The problem with this option is that it enables a whole lot of other folding options by default, which then needs to be configured to your liking
    – Ur Ya'ar
    Commented May 17, 2023 at 6:15

Your Answer

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

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