Skip to content

Text rewrapping #1782

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

Closed
ghost opened this issue Aug 23, 2020 · 15 comments
Closed

Text rewrapping #1782

ghost opened this issue Aug 23, 2020 · 15 comments

Comments

@ghost
Copy link

ghost commented Aug 23, 2020

Hi,

I have done quite some searching but didn't really find a solution, so I am posting here. Apologies if what I describe is a solved issue, after all, and I just didn't find it.

When I write LaTeX, I keep my lines limited to 80 characters, and I also like to start each sentence on a separate line. To maintain this structure when something changes, I am making extensive use of automatic rewrapping with gqis. That works fine, but it does not do what I want if the sentence contains an equation or other environment. Specifically, in a situation like

This is a long sentence, exceeding eighty characters and containing an equation environment,                                         
\begin{equation}                                                                     
 x = 1 \,,                                                                           
\end{equation}                                                                       
that should not be rewrapped.

I would like gqis on the first line to produce

This is a long sentence, exceeding eighty characters and containing an equation
environment,                                         
\begin{equation}                                                                     
 x = 1 \,,                                                                           
\end{equation}                                                                       
that should not be rewrapped.

but instead I get

This is a long sentence, exceeding eighty characters and containing an equation                              
environment, \begin{equation} x = 1 \,,                                              
\end{equation} that should not be rewrapped.     

Is there a way I can teach vim to recognize \begin{}...\end{} as sentence boundaries? And if so, could this be made part of vimtex?

Best,
Sebastian

@ghost ghost added the enhancement label Aug 23, 2020
@lervag
Copy link
Owner

lervag commented Aug 23, 2020

See :help vimtex-nf-formatting, I've written a lot about this stuff there. You can use gw with let g:vimtex_format_enabled = 1 to get something similar to what you are asking for (see g:vimtex_format_enabled).

@lervag lervag closed this as completed Aug 23, 2020
@ghost
Copy link
Author

ghost commented Aug 23, 2020

Thanks for the answer! Unfortunately, it does not work for me: I used let g:vimtex_format_enabled = 1, but the formatting still comes out not the way I want, no matter whether I use gw or gqis.

Is there really no way to achieve what I want? I understand you might not want to include this in vimtex, but could you perhaps point me to what exactly I need to modify so that vim will recognize \begin{} and \end{} as sentence boundaries? I would be willing to give it a shot myself if I knew where to start.

@ghost
Copy link
Author

ghost commented Aug 23, 2020

I found vim-textobj-latex. That allows me (among other things) to operate on blocks delimited by \begin{} and \end{}, but it still does not count these as delimiters as sentence boundaries. To me it looks that rather than definining a new text object, I want to redefine, for .tex files, what is counted as a sentence. Not sure now if this issue belongs here though, I will bring it up in vim-textobj-latex (edit: not doing that, after all, since it seems that project has been stale for six years now...).

@lervag
Copy link
Owner

lervag commented Aug 23, 2020

With set textwidth=80, let g:vimtex_format_enabled = 1, and the following latex example:

\documentclass{minimal}
\begin{document}

This is a long sentence, exceeding eighty characters and containing an equation environment below. The equation starts here:
\begin{equation}
  x = 1 \,,
\end{equation}
It should not be wrapper. This long sentence should, though. It is also more than eighty characters.

\end{document}

Now I do gggqG, and the result is this:

\documentclass{minimal}
\begin{document}

This is a long sentence, exceeding eighty characters and containing an equation
environment below. The equation starts here:
\begin{equation}
  x = 1 \,,
\end{equation}
It should not be wrapper. This long sentence should, though. It is also more
than eighty characters.

\end{document}

So, I was wrong about gw, you need to use gq (see :help gq; the point here is that we use a custom formatexpr).

@ghost
Copy link
Author

ghost commented Aug 23, 2020

Interesting. The same settings and sequence for me produce this:

\documentclass{minimal} \begin{document}                                             
                                                                                     
This is a long sentence, exceeding eighty characters and containing an equation      
environment below. The equation starts here: \begin{equation} x = 1 \,,              
\end{equation} It should not be wrapper. This long sentence should, though. It       
 is also more than eighty characters.                                                
                                                                                 
\end{document}

Perhaps there is interference from something else that I have installed...

@ghost
Copy link
Author

ghost commented Aug 23, 2020

I removed all vim-textobj plugins I had installed, but the result is still the same. No idea what else could make it not work -- any suggestions how I might diagnose what is the cause?

@ghost
Copy link
Author

ghost commented Aug 24, 2020

While I would still rather figure out why the solution above does somehow not work for me, I have meanwhile discovered an alternative. Using vim-textobj-sentence, I took inspiration from this unmerged pull request and modified l:re_term as follows:

  let l:re_term =                                                                    
      \ '([!?]|(' .                                                                  
      \ l:re_abbrev_neg_lookback .                                                   
      \ '\.))+[' .                                                                   
      \ l:trailing .                                                                 
      \ ']*' .                                                                       
      \ '\\begin\{' /                                                                
      \ '\\end\{'     

Of course, globally patching the plugin is unsatisfactory, but I think I can use ftplugin to apply this on the fly for TeX files only.

@ghost
Copy link
Author

ghost commented Aug 24, 2020

Okay, it was a little more complicated, yet, but I finally have a working solution. Leaving this here if anyone ends up here looking wor the same thing in wanted.

@lervag
Copy link
Owner

lervag commented Aug 24, 2020

Perhaps there is interference from something else that I have installed...

I don't know why it doesn't work, but let's find out. When you're in the relevant LaTeX example file, what does :echo &formatexpr say?

The simplest way for me to help you is for you to make a minimal example. In particular, a minimal vimrc file. In this case, I propose:

" test.vim
set nocompatible
let &rtp = '~/.vim/bundle/vimtex,' . &rtp
let &rtp .= ',~/.vim/bundle/vimtex/after'
filetype plugin indent on
syntax enable

set textwidth=80

let g:tex_flavor = 'latex'
let g:vimtex_format_enabled = 1
% test.tex
\documentclass{minimal}
\begin{document}

This is a long sentence, exceeding eighty characters and containing an equation environment below. The equation starts here:
\begin{equation}
  x = 1 \,,
\end{equation}
It should not be wrapper. This long sentence should, though. It is also more than eighty characters.

\end{document}

With the above, you can do:

  1. vim -u test.vim test.tex
  2. In normal mode: gggqG

The result for me (with both Vim and neovim) is as mentioned in my other post.

Btw: I'm mostly ignoring your alternatives, but thanks for investigating and suggesting them - they might be interesting to others!

@ghost
Copy link
Author

ghost commented Aug 24, 2020

Perhaps there is interference from something else that I have installed...

I don't know why it doesn't work, but let's find out. When you're in the relevant LaTeX example file, what does :echo &formatexpr say?

In my normal setup (Neovim with a bunch of plugins), it does not say anything.

The simplest way for me to help you is for you to make a minimal example. In particular, a minimal vimrc file. In this case, I propose:

" test.vim
set nocompatible
let &rtp = '~/.vim/bundle/vimtex,' . &rtp
let &rtp .= ',~/.vim/bundle/vimtex/after'
filetype plugin indent on
syntax enable

set textwidth=80

let g:tex_flavor = 'latex'
let g:vimtex_format_enabled = 1
% test.tex
\documentclass{minimal}
\begin{document}

This is a long sentence, exceeding eighty characters and containing an equation environment below. The equation starts here:
\begin{equation}
  x = 1 \,,
\end{equation}
It should not be wrapper. This long sentence should, though. It is also more than eighty characters.

\end{document}

With the above, you can do:

1. `vim -u test.vim test.tex`

2. In normal mode: `gggqG`

With standard vim and your setup above, is says vimtex#format#formatexpr(). In line with that, gggqG also does the right thing!

The result for me (with both Vim and neovim) is as mentioned in my other post.

Btw: I'm mostly ignoring your alternatives, but thanks for investigating and suggesting them - they might be interesting to others!

Yes, I understand. I realize those comments are slightly off topic here, not pertaining directly to vimtex, but I left them because I thought others might look here.

@lervag
Copy link
Owner

lervag commented Aug 24, 2020

In my normal setup (Neovim with a bunch of plugins), it does not say anything.

With standard vim and your setup above, is says vimtex#format#formatexpr(). In line with that, gggqG also does the right thing!

Which means your normal setup is flawed. What is your normal setup?

@ghost
Copy link
Author

ghost commented Aug 24, 2020

Okay, I just found it: In my tests, I always either typed let g:vimtex_format_enabled = 1 on the command line or put it in after/ftplugin/tex.vim. Neither is sufficient, it needs to be earlier in my config infrastructure. My bad (although perhaps subtle), thank you for all your help in tracking this down!

@lervag
Copy link
Owner

lervag commented Aug 24, 2020

Great, happy to hear it!

Note:

  • A lot of plugins, like vimtex, need the configuration to be specified before the plugin is loaded. This might be a little bit subtle, but when you "get it", it is quite natural.
  • Configuration for vimtex (and generally all plugins) should therefore go in your .vimrc file.
  • Configuration in after/plugin/... and after/ftplugin/... can be used to override behaviour of plugins.

@ghost
Copy link
Author

ghost commented Aug 24, 2020

Okay, that does make sense. Well, I am happy, I now have both the local gqis rewrapping I originally wanted plus the broader gggqG reformatting. Thanks again!

@lervag
Copy link
Owner

lervag commented Aug 24, 2020

Happy to help :)

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

No branches or pull requests

1 participant