### Qualified's blog

By Qualified, history, 5 months ago,

What is your compiler flags when using Vim for C++? Where do you put them?

• +26

 » 5 months ago, # | ← Rev. 2 →   0 In .vimrc file. Here is mine....autocmd vimEnter *.cpp map :w :!clear ; g++ --std=c++17 %; if [ -f a.out ]; then time ./a.out; rm a.out; fi
•  » » 5 months ago, # ^ | ← Rev. 2 →   0 I put it in my .vimrc but when I press F8, it doesn't run. Where does the output show? How to input numbers? Do you have multiple input and output files?
•  » » » 5 months ago, # ^ |   0 It works fine for me. Where did you put .vimrc file ? are you sure it is on your home directory. you can just make it like, vim ~/.vimrc or vim /home/USERNAME/.vimrc
•  » » » » 5 months ago, # ^ |   0 Ya, I put it in my home directory.
•  » » 6 weeks ago, # ^ |   0 is it for ubuntu ?
 » 5 months ago, # |   0 I don't use Vim but if you are compiling from the command line you can make an alias so that you can type "bd" instead of "g++ -Wall ...". On Linux you can modify the .bash_aliases. On Windows you can modify $PROFILE by adding a function to it like: function cl($arg) { g++ -std=c++17 -Wshadow -Wall -Wextra -g -D_GLIBCXX_DEBUG $arg } and then you can simply type "cl xxx.cpp" to compile. •  » » 5 months ago, # ^ | ← Rev. 2 → 0 I want to compile and run within Vim but thanks! •  » » » 5 months ago, # ^ | 0 BTW, how do you modifly$PROFILE? Just a question, was wondering...
•  » » » 3 months ago, # ^ |   0 can you please help me .. i want to compile by such that it opens a new terminal screen so that i can compair my code and its output
•  » » » » 3 months ago, # ^ |   0 Did you set up .vimrc?
•  » » » » » 3 months ago, # ^ |   0 yes but its compiling and running my code in same terminal i.e by first closing my vim and then running my code and then after that again opening my vim.. i want that the output must be shown by opening new terminal window ..like in codingblocks ide... is it possible?? help:)
•  » » » » » » 3 months ago, # ^ | ← Rev. 2 →   0 You can use :term CMD to open a terminal in a new window and execute a command in that window. E.g. try :term ls. Replace that with your command you want to run, e.g. the command to compile or run your program. Obviously you can also make a keybinding for that command.
 » 5 months ago, # |   +3 Here's my Compilation and Execution part of Vim Configso ~/.vim/plugins.vim filetype on filetype indent on syntax enable autocmd BufNewFile *.cpp execute "0r ~/.vim/template/".input("Template name: ").".cpp" map :!g++ -g % -o %:r && ./%:r map :!g++ -g % -o %:r map :w map :!gdb ./%:r  F9 for Compile and Run F5 for Compile ( never use this :p ) F2 for saving fileI save and run inside Vim. Also, when creating a new file it asks for a template that I should load , which is saved in .vim/template and it fetches .cpp types which you can easily changeAnd just in Case : My Vim Configurationso ~/.vim/plugins.vim filetype on filetype indent on syntax enable autocmd BufNewFile *.cpp execute "0r ~/.vim/template/".input("Template name: ").".cpp" map :!g++ -g % -o %:r && ./%:r map :!g++ -g % -o %:r map :w map :!gdb ./%:r map gg v G map "+y map :.,$s/min//g " map :s/^/\/\// " map :s/^\/\/// nmap NERDCommenterToggle vmap NERDCommenterTogglegv set whichwrap+=<,>,[,],l,h set number set tabstop=4 set shiftwidth=4 if (empty($TMUX)) if (has("nvim")) let $NVIM_TUI_ENABLE_TRUE_COLOR=1 endif if (has("termguicolors")) set termguicolors endif endif set autoindent set mouse=a set background=light " for the dark version " set background=light " for the light version colorscheme molokai Hope it helps! •  » » 5 months ago, # ^ | 0 When I press F9, it says that '.' is not recognized as an internal or external command, operable program or batch file. shell returned 1. How to solve this problem? •  » » » 5 months ago, # ^ | ← Rev. 2 → 0 Here is my .vimrc .vimrcset nocompatible " be iMproved, required filetype off " required set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() Plugin 'VundleVim/Vundle.vim' Plugin 'joshdick/onedark.vim' call vundle#end() " required filetype plugin indent on " required behave mswin syntax on colorscheme onedark set backspace=indent,eol,start " more powerful backspacing au GUIEnter * simalt ~x set hls set is set cb=unnamed set guifont=* set gfn=Fixedsys:h10 set ts=4 set sw=4 set si set autoindent set smartindent set nocompatible set number set relativenumber inore jj function GuiTabLabel() let label = '' let bufnrlist = tabpagebuflist(v:lnum) for bufnr in bufnrlist if getbufvar(bufnr, "&modified") let label = '+' break endif endfor let wincount = tabpagewinnr(v:lnum, '$') if wincount > 1 let label .= wincount endif if label != '' let label .= ' ' endif return label . "%t" endfunction set guitablabel=%!GuiTabLabel() inoremap { {} inoremap { {}O inoremap {{ { inoremap {} {} ""inoremap " "" ""inoremap ' '' ""inoremap [ [] "inoremap ( () filetype on filetype indent on syntax enable autocmd BufNewFile *.cpp execute "0r ~/.vim/template/".input("Template name: ").".cpp" map :!g++ -g % -o %:r && ./%:r map :!g++ -g % -o %:r map :w map :!gdb ./%:r set diffexpr=MyDiff() function MyDiff() let opt = '-a --binary ' if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif let arg1 = v:fname_in if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif let arg1 = substitute(arg1, '!', '\!', 'g') let arg2 = v:fname_new if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif let arg2 = substitute(arg2, '!', '\!', 'g') let arg3 = v:fname_out if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif let arg3 = substitute(arg3, '!', '\!', 'g') if $VIMRUNTIME =~ ' ' if &sh =~ '\ ' . arg3 if exists('l:shxq_sav') let &shellxquote=l:shxq_sav endif endfunction  •  » » » » 5 months ago, # ^ | ← Rev. 2 → 0 By any chance, are you on Windows?Edit : Sorry Never came across such error , Did you try compiling first , then running by terminal command ./file_name •  » » » » » 5 months ago, # ^ | 0 How to compile first and how to run it in terminal? •  » » » » » » 5 months ago, # ^ | 0 As mentioned above, By pressing F2 to save your code, F5 you can compile your code,( if there's no error, there will be a file created in your current directory with name of your current cpp file name ) Exit vim and run ./filename in the directory file is saved •  » » » » » » » 5 months ago, # ^ | +1 Thanks! •  » » » » » » » » 3 weeks ago, # ^ | 0 hi bro , did you could to run files , i faced same problem as you •  » » 5 months ago, # ^ | 0 thank you! But is this the same for VIM in windows too?! Where the .vimrc file is named as _vimrc. •  » » » 5 months ago, # ^ | 0 I would (in normal mode) type ":version" and then a list of files will appear. Towards the bottom, it will say "user vimrc file: " then that file name. If you don't see that, first create one. •  » » 2 months ago, # ^ | 0 How to set up a C++ template for a GVIM on Windows for competitive programming? Please I have tried so hard, but I haven't found any solution. •  » » » 2 months ago, # ^ | 0 What do you mean by template? Is it like sublime in which you type in the "template", the template appears in the file? •  » » » » 2 months ago, # ^ | 0 Yes. •  » » » » » 7 weeks ago, # ^ | 0 •  » » » » » » 7 weeks ago, # ^ | -8 which font do u use for vim? •  » » » » » » » 7 weeks ago, # ^ | 0 What is up rayhansec. •  » » » » » » » » 7 weeks ago, # ^ | -8 sorry for getting blocked! by the way, i just needed an answer! so i commented, plz do reply! •  » » » » » » » » » 7 weeks ago, # ^ | 0 I use Consolas 15 •  » » » » » » » » » 7 weeks ago, # ^ | -8 and plz unblock me !? i mean, the id rayhansec , as we're friends now! •  » » 2 months ago, # ^ | 0 How to set up a C++ template for a GVIM on Windows for competitive programming? Please I have tried so hard, but I haven't found any solution. •  » » 6 weeks ago, # ^ | 0 I only use F9 with your configure. However, when I press F9, it only show command g++ -g % -o %:r && ./%:r and never stop. How can I fix it •  » » » 6 weeks ago, # ^ | ← Rev. 3 → 0 use this autocmd filetype cpp nnoremap :w !g++ -std=c++14 -O2 -Wall % -o %:r && %:r.exe just pess F10 to compile & run at the same time and you'll not get any error.  » 5 months ago, # | 0 I would like to have a vim plugin to include code snippets. On some keypress a listing of the files of a configured directory should be displayed. I can choose one. If choosen, that file should be copied into my current edited file, maybe at cursor position.Any suggestions? Or how to write such one? •  » » 5 months ago, # ^ | 0 Or does somebody know how to use/extend NERDTree for that purpose? File listing and choosing is perfect, but I don't know how to read a file on keypress. Instead I have to open it, copy content, close it, then paste content.Surly there is a better way. •  » » » 5 months ago, # ^ | 0 I don't exactly know how to do this in Vim, but whenever I need to copy a whole file I just do xsel -b < FILE. •  » » 4 months ago, # ^ | ← Rev. 2 → +8 Hey. I've done exactly the thing you are describing. With a key binding I can choose between all my prewritten algorithms, choose one, and it will be copied into the file at the current position.Take a look here: https://github.com/jakobkogler/Algorithm-DataStructures (The Readme also contains a GIF with a short demonstration)The path to the directory is hard-coded to my algorithms, but you can also change it to something different in the autoload/alg_ds.vim file. •  » » » 4 months ago, # ^ | +8 This is cool, with preview! Thanks a lot. •  » » 3 months ago, # ^ | ← Rev. 2 → 0 You might wanna try UltiSnips. It works just like snippets in VS Code and Sublime. You can even add a tab trigger with a few lines of vim script. •  » » 6 weeks ago, # ^ | 0 bruh! can u share your vimrc file ? •  » » » 6 weeks ago, # ^ | 0 Not sure if it really helps, but anyway: .vimrccolo desert syntax on set softtabstop=4 shiftwidth=4 expandtab " block cursor and colors as of " https://superuser.com/questions/634326/how-can-i-get-a-block-cursor-in-vim-in-the-cygwin-terminal let &t_ti.="\e[1 q" let &t_SI.="\e[5 q" let &t_EI.="\e[1 q" let &t_te.="\e[0 q" " see https://stackoverflow.com/questions/2355834/how-can-i-autoformat-indent-c-code-in-vim autocmd BufNewFile,BufRead *.cc set formatprg=astyle\ -A14\ --lineend=linux\ --break-one-line-headers\ --max-code-length=160 nnoremap :w \| !cf test nnoremap :w \| !cf submit " Specify a directory for plugins " " - For Neovim: stdpath('data') . '/plugged' " " - Avoid using standard Vim directory names like 'plugin' " see https://github.com/junegunn/vim-plug call plug#begin('~/.vim/plugged') Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' } Plug 'jakobkogler/Algorithm-DataStructures' call plug#end() nmap alg :AlgDS nmap :AlgDS   » 5 months ago, # | ← Rev. 2 → 0 Use terminal and Makefile. MakefileCC=g++-8 CFLAGS=-std=c++17 -Wall -Wextra -Wshadow -Wconversion -g -fsanitize=address -fsanitize=undefined -fuse-ld=gold -D_GLIBCXX_DEBUG -O2 all: a b c d e f g h i j k l m a: a.cpp $(CC)$(CFLAGS) -o $@$^ b: b.cpp $(CC)$(CFLAGS) -o $@$^ c: c.cpp $(CC)$(CFLAGS) -o $@$^ d: d.cpp $(CC)$(CFLAGS) -o $@$^ e: e.cpp $(CC)$(CFLAGS) -o $@$^ f: f.cpp $(CC)$(CFLAGS) -o $@$^ g: g.cpp $(CC)$(CFLAGS) -o $@$^ h: h.cpp $(CC)$(CFLAGS) -o $@$^ i: i.cpp $(CC)$(CFLAGS) -o $@$^ j: j.cpp $(CC)$(CFLAGS) -o $@$^ k: k.cpp $(CC)$(CFLAGS) -o $@$^ l: l.cpp $(CC)$(CFLAGS) -o $@$^ m: m.cpp $(CC)$(CFLAGS) -o $@$^ P.S. I don't know, why one dollar automatically replace to three dollars, and have no idea, how to write exactly one dollar, sorry(  » 5 months ago, # | 0 I am new here. Is there any reason why using vim is better than sublime? There are many good settings and automations that you can do with sublime. •  » » 4 months ago, # ^ | 0 What editor you use is up to your personal preference. An editor might be better for me, but worse for you. Find one that you like, learn how to use it and stick to it.The reason I use Vim, is because it is small, fast, directly available in the terminal, available on every server, modal editing is awesome, huge possibilities of configuration and extension, one editor for everything, ...Also I doubt that Sublime has a feature that you can't have in Vim. Well, maybe one feature: being beginner friendly :-) •  » » » 2 months ago, # ^ | 0 Seems appropriate. But why not just use the custom invocation provided by CodeForces itself? •  » » » » 2 months ago, # ^ | 0 Advantages that you have by developing locally: much faster compile and run times not dependent on internet connection you can work on multiple files at once auto-completion, error warnings, jump to declaration, and many other IDE niceties debugging capabilities code snippets support you don't get called "noob" as often and probably many, many more... •  » » » » » 2 months ago, # ^ | 0 Alright. Seems like the number of reasons is much more than I had expected. Anyways, thanks. •  » » 6 weeks ago, # ^ | ← Rev. 2 → 0 .  » 5 months ago, # | ← Rev. 3 → 0 simplest form of compiler flag map :!g++ -std=c++17 -O2 % map :!./a.out < in  •  » » 5 months ago, # ^ | 0 I use autocmd filetype cpp nnoremap :w !g++ -std=c++17 % -O2 -Wall -Wextra -DLOCAL -o %:r -Wl,--stack,268435456 autocmd filetype cpp nnoremap :!%:r just press for compiling and for executing. :P •  » » » 4 months ago, # ^ | 0 it's working, but how can i run, compile and save from a single command?? •  » » » » 4 months ago, # ^ | 0 All you have to do to compile is press . Then, you can press for executing where you can input your data. For saving a file go to normal mode by pressing then type :w If you want to close and save that file in normal mode, you can press :wq Hope that this helped! •  » » » » » 4 months ago, # ^ | 0 thanks, it really helped!! •  » » » » » 6 weeks ago, # ^ | 0 do u know what it does in Vim =>autocmd filetype cpp nnoremap :s/^$$\s*$$/\1\/\/ :s/^$$\s*$$\/\/\/\//\1$
•  » » » 2 months ago, # ^ |   0 How can i compile & run pressing a single function ?? i have a source but it only compile & runs C++11 .But i want it for C++17 .
•  » » » » 2 months ago, # ^ |   0 If you want to press a single button and have it compile and execute and have C++17 with -O2 and -Wall put "autocmd filetype cpp nnoremap :w !g++ -std=c++17 -O2 -Wall % -o %:r && %:r.exe in your .vimrc.
•  » » » » » 2 months ago, # ^ |   0 But, how can i add this --stack,268435456 in vimrc for compiling & running at the same time !?
•  » » » » » » 2 months ago, # ^ |   0 Then use autocmd filetype cpp nnoremap :w !g++ -std=c++17 -O2 -Wall % -o %:r -Wl,--stack,268435456 && %:r.exe 
•  » » » » » » » 2 months ago, # ^ |   0 But when i try to add bits/stdc++.h , it doesn't work and shows error.
•  » » » » » » » » 2 months ago, # ^ |   0 It works for me though
•  » » » » » » » » » 2 months ago, # ^ |   0 which compiler are u using for C++ ?
•  » » » » » » » » » 2 months ago, # ^ |   0 I am using MinGW version 9.2.0 on Windows.
•  » » » » » 6 weeks ago, # ^ | ← Rev. 3 →   0 bruh! i am also using tmw's vimrc but when i first compile (with bits/stdc++.h header) with F9, and then execute my cpp file with F10, the compilation is done quickly, but the problem is=> if i use a F5 to compile & run (that u provided me in this post this lineautocmd filetype cpp nnoremap :w !g++ -std=c++17 -O2 -Wall % -o %:r -Wl,--stack,268435456 && %:r.exe ), the performance is laggy and why does it takes more than 4 seconds to compile first and then run. Any fix to reduce the time with appropriate compilation flag?
•  » » » » » » 6 weeks ago, # ^ |   0 Precompiled headers.
•  » » » » » » » 6 weeks ago, # ^ |   0 i did that also, but didn't work! u can check on your windows also. thanks!
•  » » » » » » » » 6 weeks ago, # ^ |   0 For the F5 command try using this autocmd filetype cpp nnoremap :w !g++ -std=c++14 % -o %:r -Wl,--stack,268435456 && %:r.exe 
•  » » » » » » » » » 6 weeks ago, # ^ |   0 tried but it now runs in 3.5 seconds!
•  » » » » » » » » » 6 weeks ago, # ^ |   0 any solution ?
•  » » » » » » » 6 weeks ago, # ^ |   0 btw, can u share your current vimrc file with me now!
•  » » 4 months ago, # ^ |   0 when i tried your code, it says the system cannot find the file specified. shell returned 1?? what should i do??
•  » » » 4 months ago, # ^ |   0 I think his config is for linux. You may change ./a.out to ./a.exe. I'm not using windows so not 100% sure.
•  » » » » 6 weeks ago, # ^ |   0 can u share a valid vimrc file for ubuntu ?
•  » » » 4 months ago, # ^ |   0 you should have a "in" file first.
•  » » » » 4 months ago, # ^ |   0 can you please explain what is this in file and how to create it??
•  » » » » » 4 months ago, # ^ |   0 "in" is the name of the input file of your program. My setup is use ctrl-n to run the file with input in the "in" file, you can change it to another file name. Just a file to paste input of your program then to run.
•  » » 4 months ago, # ^ |   0 do i have to add something else than mingw to path??
•  » » » 6 weeks ago, # ^ | ← Rev. 2 →   0 .
 » 4 months ago, # | ← Rev. 2 →   0 In ~/.vimrc , here is mine ...nnoremap :!g++ -std=c++17 -Wshadow -Wall -o %:r.out % -O2 -Wno-unused-result | :update nnoremap :!./%:r.out
 » 2 months ago, # | ← Rev. 3 →   0 This is my Gvim commands to compile and Execute the c++ code :w :!g++ % :!filename.exe 
 » 2 months ago, # | ← Rev. 3 →   0 save :w compile :!g++ % Execute or run :!filename.exeI think it will work because I am compiling and running this command on Gvim or vim your welcome if this work on your pc or computer
 » 2 months ago, # |   0 :w :!g++ % :!filename.exe 
 » 2 months ago, # | ← Rev. 2 →   -23 This my _vimrc " Vim with all enhancements source $VIMRUNTIME/vimrc_example.vim  " Use the internal diff if available. " Otherwise use the special 'diffexpr' for Windows. if &diffopt !~# 'internal' set diffexpr=MyDiff() endif function MyDiff() let opt = '-a --binary ' if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif let arg1 = v:fname_in if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif let arg1 = substitute(arg1, '!', '\!', 'g') let arg2 = v:fname_new if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif let arg2 = substitute(arg2, '!', '\!', 'g') let arg3 = v:fname_out if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif let arg3 = substitute(arg3, '!', '\!', 'g') if$VIMRUNTIME =~ ' ' if &sh =~ '\ ' . arg3 if exists('l:shxq_sav') let &shellxquote=l:shxq_sav endif endfunction  
 » 2 months ago, # | ← Rev. 2 →   0 How to include timeout in the command so that it automatically terminates in case of infinite loop..?autocmd filetype cpp nnoremap :w !timeout 3s g++ -ulimit -Wall -Wno-unused-result -std=c++17 -O2 % -o %:r && ./%:r Above command works fine for running code but timeout does'nt work.
 » 2 months ago, # |   -19 You can use "Dev-C++"!It's easy for everyone
 » 2 months ago, # |   0 Did you consider using a text editor with vim keybindings?
 » 2 months ago, # |   0 This my vim commands to compile and execute save:w compile :!g++ -Wall -o "a" "a.cpp" Execute :!a.exeso it will open exe file and you paste input in it and you will get output in Vim but use GvimThank you
 » 7 weeks ago, # |   0 https://github.com/xuhdev/SingleCompile is a Vim plugin made to compile and run single files and it's really good because it support a lot of languages. iirc it runs synchronously (that means it stops vim while your program is running)In Neovim and Vim >= 8 asynchronous tasks were introduced, and i use this plugin ( https://github.com/skywind3000/asyncrun.vim ) because is simpler to configure. But with it you have to define a compile command for every programming language... To simplify everything i made an external bash script that is called by AsyncRun when I press F9. It automatically detects the language and calls the right compiler.