I am using vim for competitive programming and I am using this shell script to compile my c++ files.
g++ -static -DLOCAL -lm -s -x c++ -Wall -Wextra -O2 -std=c++17 -o $1 $1.cpp
When I use this script I get all the errors and warnings in the terminal and when I press ENTER everything is gone. Is there a way to modify this script to display errors and warnings in a separate buffer in vim without going to terminal or is there anything that I should add in my .vimrc file ??
I want my vim to show errors like this one below.
There are many ways of doing it. The best way, IMHO, would be to use the compiler plugins. Please read :help compiler. After that please read :help make. Here is an example from my .vimrc:
nnoremap <F6> :wa <bar> :compiler cargo <bar> :make build <CR>
The example is for Rust. In your case it would be like this
nnoremap <F6> :wa <bar> :compiler gcc <bar> :make<CR>
I fixed the problem by doing this
nnoremap <F9> :wa <bar> :compiler gcc <bar> :silent! make %:r <CR> :cw <CR>
But I don't know how to highlight errors of quickfix list.
Related
I used to use vim/gvim at windows, and my old .vimrc part for compilation looked like this:
autocmd filetype cpp nnoremap <F5> :w <bar> !g++ -std=c++11 -Wall % -o %:r && %:r.exe <CR>
On windows when I pressed f5 a shell (windows cmd) would open and execute it.
However now I began using vim/gvim on linux and when I press f5 it runs on a built-in shell. Is there a way to run on a external shell? I just can open an external shell by typing :! gnome-terminal, but how can I automatically open a shell with the compiler command like g++ main.cpp -o main, javac Main.java && java Main (whatever)
I looking for a solution for a while haha
I'm still improving my english, any critiscism is welcome!!
autocmd filetype cpp nnoremap <F4> :w <bar> exec '!g++ '.shellescape('%').' -o '.shellescape('%:r').' && ./'.shellescape('%:r')<CR>
Try it out!
BTW, maybe here will u find useful information :)
So I am using the gvim editor to program in C++, however when I open a .cpp file on the editor then type :!g++ -std=C++17 file.cpp -o file I get this error:
See attached file below:
C:\Windows\system32\cmd.exe /c(g++ -std=c++17 file.cpp - o file)
g++: error: file.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated
Now is this related to vim, or to my command?
Thanks in advance.
It seems that you're unfortunately using gcc-mingw that doesn't come with gnumake properly configured (:let $CXXFLAGS='-std=c++17 -Wall -Werror'+ make %< would have been enough otherwise). Thus you'll have a do a few things manually. And don't misspell the parameters to g++. And forget :!{compilername}, this is want we had to do 25 years ago with vi. Unlike vi, vim permits to integrate compilation.
" to do once
:let &makeprg='g++ -std=c++17 -Wall -Werror % -o %<'
" to execute each time to compile
:make
:copen
" to navigate the errors
:cc " jump to error under cursor
:cnext " jump to next error
:cprev " jump to previous error
:cclose " close the quickfix window
-> :h quickfix
This shouldn't execute g++ from the wrong directory. If this still does, run :cd %:h.
The execution should work with :./%.
And if you wish to have hot keys for that...
nnoremap <silent> <F7> :<c-u>update<cr>:make<cr>
nnoremap <silent> <C-F5> :<c-u>./%<cr>
nnoremap <silent> <F3> :<c-u>cnext<cr>
nnoremap <silent> <S-F3> :<c-u>cprev<cr>
Automatically opening the quickfix windows only if there are errors or executing the result only if there are no errors requires a little more work.
I'm currently trying to write my own setup for autocompiling and previewing LaTeX code using Vim and Zathura. This is the code I have currently have in my tex.vim file:
:map <F6> :w <bar> ! pdflatex -synctex=1 -interaction=nonstopmode %<CR><CR>
:map <F7> :! zathura $(echo % \| sed 's/tex$/pdf/') & disown<CR><CR>
This works fine as long as when I compile the code (from Vim) I am in the directory where my tex files are. However, I would like this to work regardless of my position (since I frequently open files from Vim using the :e command). How do I fix this?
Also, I would like Zathura to open only if the PDF preview is not already open. Does anyone have any suggestions as to how to do such a thing?
You can use :h filename-modifiers instead of your sed hack, so this should do:
nnoremap :<C-u>w <bar> ! pdflatex -synctex=1 -interaction=nonstopmode %:p<CR>
nnoremap :<C-u>! zathura %:p:r.pdf<CR>
I have also changed these mappings to use :nnoremap which you should prefer anyway.
If you want to expand your Vim-fu even further you can check :h compiler and :h compilet-tex which will handle setting proper 'makeprg' for you which will allow you to use :make to build your TeX files.
An alternative would be to simply use vimtex (https://github.com/lervag/vimtex)
Mapping: <localleader>lv
I am learing Vim and I want have set it up as IDE by mapping F5 key to compilation command which is decided by the filetype.
My ~/.vim/ftplugin/c.vim:
map <F5> :w<CR>:!clang % -o %:r.out && ./%:r.out<CR>
My ~/.vim/ftplugin/cpp.vim:
map <F5> :w<CR>:!clang++ -ggdb -pthread -std=c++11 % -o %:r.out && ./%:r.out<CR>
When I open a C++ file (with .cpp extension) and hit F5, the command from c.vim is executed. Why is this happening?
When I remove the file c.vim, then Vim loads cpp.vim and works as expected.
The cpp ftplugin that comes with vim has the following line:
runtime! ftplugin/c.vim ftplugin/c_*.vim ftplugin/c/*.vim
Which means it is going to source the ~/.vim/ftplugin/c.vim.
A way to overcome this is to put your mappings in ~/.vim/after/ftplugin/cpp.vim and ~/.vim/after/ftplugin/c.vim files.
But your problems don't stop there:
You are using :map where you probably want to at least supply a mode. nmap
You also should probably be using noremap so it would become nnoremap
Your mapping is global so it will stomp over your <f5> everytime you open a cpp and switch to a c file. You should make it local to the specific buffer. nnoremap <buffer>
You should probably also be setting makeprg accordingly so you can take advantage of :make and the quickfix list. e.g. setlocal makprg clang\ %\ -o\ %:r.out
For more help see:
:h :map
:h :map-local
:h 'makeprg'
:h :make
:h quickfix
If you move your file into the /after/ftplugin/ folder, your C specific instructions will still be executed on your C++ files. But your C++ instructions will be executed after the C ones.
If you want to avoid this behaviour, add this at the beginning of your ~/.vim/ftplugin/c.vim file
if (&ft != 'c')
finish
endif
I want to override wq/q/w!/w/q! to user defined command along with its functionality.
Example :
If I use :wq to exit, the command should do static code check of that particular c/c++ file and exit.
Please help me in this case.
Thanks in advance.
The built in solution to your problem is called an "autocommand" in Vim.
It is a way to invoke a command at a specific time like opening, saving or closing a buffer.
See :help autocmd for the full list
In your case, you should add to your .vimrc the following command
autocmd BufWritePre *.cpp,*.hpp !cppcheck %
BufWritePre means 'before writing the buffer' (You can also use BufWrite or BufWritePost)
*.cpp,*.hpp means the auto command will only be applied when saving cpp or hpp files. You can add c and h files if you want.
% means 'path of the current buffer'
cppcheck must be in your path
You are not overriding the defaut behaviour of 'w' but you are using 'hooks' to add custom commands.
I wouldn't do that. It obliges us (well, you actually) to save only when the file is really compilable -- which make no sense when we have to abort the current editing because an urging meeting that we've forget about is about to begin; saving the file in that situation is the normal way to proceed.
Moreover, what is the purpose of running cppcheck on a :wq? How can we exploit the result?
Instead, I'd have a mapping that run cppcheck, though :make in order to exploit the vim quickfix feature (:h quickfix)
function s:Check()
try
let save_makeprg=&makeprg
set makeprg=cppcheck
" you may have to specify other files/extensions
:make *.cpp *.hpp
finally
let &makeprg=save_makeprg
endtry
endfunction
nnoremap <buffer> <c-f7> :call <sid>Check()<cr>
<block>
function FunForQuickfix(makeprgIn, makeefmIn)
try
let save_makeprg=&makeprg
let save_makeefm=&efm
let &makeprg=a:makeprgIn
let &efm=a:makeefmIn
:wa
:make
:cw
finally
let &makeprg=save_makeprg
let &efm=save_makeefm
endtry
endfunction
function CompileAndRunTestCodeByScons()
call FunForQuickfix('scons . -j8 -u', '%f:%l:%c:\ %m,%f:%l:\ %m,build/release64/%f:%l:%c:\ %m,build/release64/%f:%l:\ %m,%f\|%l\|,build/release64/%f:%s,%m:%l:Assertion,%sExpression:\ false,scons:\ building\ terminated\ because\ of\ errors,%sError%m')
endfunction
function CppCheck()
call FunForQuickfix("cppcheck *.cpp -j 8 --enable=all", "\[%f:%l\]:\ %m")
endfunction
nmap ma :wa<CR>:call CompileAndRunTestCodeByScons()<CR>
nmap mc :wa<CR>:call CppCheck()<CR>
nmap <F3> :cp<cr>
nmap <F4> :cn<cr>
</block>
add this in ~/.vimrc, then use ma to compile code and mc to run cppcheck
this code is copy from Luc Hermitte, and i chang it
You might want some things even better, use cppcheck and other checkes while you are developing in vim.
Install and use that vim plugin: https://github.com/scrooloose/syntastic