How to Compile and Run C++, Python, and LaTeX Files in Vim
Category: vim
Date: April 2023
Views: 1.24K
Hello everyone,
As a programmer, I have been using Vim as my primary text editor for various programming languages, including C++, Python, and LaTeX. but it gets tiresome to siwtch back and forth between your code in vim and the terminal to compile it. So I started exploring options to compile and run C++ code directly within Vim. And, I found a fantastic solution that I think you would love too.
To start with, I created a function in Vim called RunCPP
which compiles the current file name using g++ and runs the executable file. This has been a real-time saver for me, as I no longer have to switch between Vim and terminal continuously.
function! RunCPP()
let s:current_file = expand("%")
enew|silent execute ".!g++ " . shellescape(s:current_file, 1) . " -o " . shellescape(s:current_file, 1) . ".out && ./" . shellescape(s:current_file, 1) . ".out"
setlocal buftype=nofile bufhidden=wipe noswapfile nowrap
endfunction
To use this function, I created a Vim mapping that calls the function when triggered with a keyboard shortcut, such as the leader key followed by the letter 'c.' This way, I can compile and run C++ code with just a couple of keystrokes.
And the good news is, similar to C++, I have done the same to compile and run Python scripts and generate PDFs from LaTeX files within Vim itself.
function! RunPython()
let s:current_file = expand("%")
enew|silent execute ".!python " . shellescape(s:current_file, 1)
setlocal buftype=nofile bufhidden=wipe noswapfile nowrap
endfunction
Now, moving on to LaTeX, I have two Vim functions: RunTex
and ViewTex
. The "RunTex" function compiles the current LaTeX file using the "xelatex" command, while the "ViewTex" function generates a PDF from the LaTeX file using the "okular" command and opens it in a new buffer within Vim. Both these functions are mapped to specific keyboard shortcuts for easy access.
function! RunTex()
let s:current_file = expand("%")
enew|silent execute ".!xelatex " . shellescape(s:current_file, 1)
setlocal buftype=nofile bufhidden=wipe noswapfile nowrap
normal! G
nnoremap :bd!
endfunction
function! ViewTex()
let s:current_file = expand("%")
let s:pdf_file = substitute(s:current_file, '\.tex$', '.pdf', '')
enew
silent execute "!nohup okular " . shellescape(s:pdf_file, 1) . " </dev/null >/dev/null 2>&1 & "
setlocal buftype=nofile bufhidden=wipe noswapfile nowrap
nnoremap :bd!
endfunction
to use these functions, just create these files in the ~/.vim/after/ftplugin
directory. and put them in the files accordingly
~/.vim/after/ftplugin/python.vim
~/.vim/after/ftplugin/cpp.vim
~/.vim/after/ftplugin/tex.vim
By setting up these mappings, I have been able to streamline my workflow and avoid switching between Vim and the terminal. These functions and mappings save time, making it easier to focus on programming without any distractions. those ftplugin files contain specific settings for each file type so feel free to add your own configurations to them
I hope this tutorial helps you in running C++, Python and LaTeX files within Vim as it did for me. Give it a try and let me know how it goes!
2 Comments, latest
Laurent
9 months agoYes ! I have similar code to compile python, latex, manim, Bash files with always the same command. Vim is very cool when you use it with that
admin Admin Reply
9 months agoLaurent:
Yes ! I have similar code to compile python, latex, manim, Bash files with always the same command. Vim is very cool when you use it with that
Yes vim is great once you confugure it to your liking. This is just an example. My work flow with vim is something else. Code snippets, macros...