1

I use the following function to compile a LaTeX document

function! TeXCompile()
  let filepath = expand('%:p')
  execute "!cd $(dirname ".shellescape(filepath,1).") && pdflatex -synctex=1 ".shellescape(filepath,1)
endfunction

which I use with the autocmd

autocmd FileType tex nnoremap <buffer> <silent> <C-t> :call TeXCompile()<cr>

How can I change it so that the command itself (:!cd $(dirname ...) is not echoed to the commandline, but its output (the stuff after This is pdfTex) is?

enter image description here

  • If I had to guess, you probably can't... – D. Ben Knoble May 29 at 13:31
  • @noibe Don't forget to mark the answer as accepted (assuming it did address your problem adequately) and to award the bounty you have open. Thanks! :-) – filbranden Jun 5 at 20:18
  • 1
    @filbranden I know, I was waiting for the bounty to almost expire before accepting it. – noibe Jun 6 at 8:44
5
+50

As Ben said in the comments prompting the command used before its result is a built-in behavior that you can not change without modifying Vim's source code.

However a possible workaround is to use :h system() to avoid prompting anything and then showing the result of the command by yourself.

I did a test with the following mapping:

nnoremap <silent> <F1> :let res = system('cd $HOME && ls') \| echo res<CR>

It will cd to the right directory, put the result of ls in the res variable and then echo the content of the variable showing the result of the command without showing the command itself

This is ugly but this is probably your best best.


Also a few unrelated but still relevant notes:

  • Never use an autocommand without an augroup:h :augroup
  • Don't create your filetype specific mapping with an autocommand you are bloating your config for nothing. Instead use a ftplugin :h ftplugin
  • Instead of having a mapping to invoke pdftext you probaby want to have a read at :h 'makeprg', :h :compiler and :h compiler-tex
| improve this answer | |
  • I have only briefly looked into makeprg, compiler and compiler-tex, but from what I saw they need a makefile, correct? Even if it doesn't need one, what's the advantage of that method over cd-ing to the directory and calling pdflatex? – noibe Jun 1 at 14:34
  • 1
    Actually I think only compiler-text requires a makefile. compiler is a way to set makeprg and makeprg can be basically any command you want. I mentioned these because this is the built-in way to do what you are trying to do, but cding and using pdflatex directly from a mapping has nothing wrong if it fits your needs. IMO the main advantage is that it let's you define a generic interface to "build stuff" and then you can define how to "build" each of your "stuff". – statox Jun 1 at 15:47
  • But yeah these last points were just to let you know these options exist. I still believe you would benefit from using a ftplugin instead an autocommand tho because it allows you to remove bloat from your config while leveraging the built-in behavior offered by Vim – statox Jun 1 at 15:48
  • 1
    @filbranden We don't have much context here, but I think that if OP wants to remove a single line to not see the command they use, I would guess that opening a new buffer with a shell in it and which would show the command they use is not going to make them happy :) – statox Jun 2 at 7:37
  • 1
    @noibe Vimtex's author is really responsive and hangs around here... Can you expand on limitations or annoyances of vimtex's compiler implementation? Also consider filing an issue on vimtex's GitHub page. – filbranden Jun 2 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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