Блог пользователя Qualified

Автор Qualified, история, 4 года назад, По-английски

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

  • Проголосовать: нравится
  • +26
  • Проголосовать: не нравится

»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

In .vimrc file. Here is mine....

autocmd vimEnter *.cpp map <F8> :w <CR> :!clear ; g++ --std=c++17 %; if [ -f a.out ]; then time ./a.out; rm a.out; fi <CR>

  • »
    »
    4 года назад, # ^ |
    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?

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 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

»
4 года назад, # |
  Проголосовать: нравится 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.

  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    I want to compile and run within Vim but thanks!

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      BTW, how do you modifly $PROFILE? Just a question, was wondering...

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 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

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Did you set up .vimrc?

        • »
          »
          »
          »
          »
          4 года назад, # ^ |
            Проголосовать: нравится 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:)

          • »
            »
            »
            »
            »
            »
            4 года назад, # ^ |
            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.

»
4 года назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится
Here's my Compilation and Execution part of Vim Config


F9 for Compile and Run
F5 for Compile ( never use this :p )
F2 for saving file

I 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 change

And just in Case :

My Vim Configuration

Hope it helps!

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 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?

    • »
      »
      »
      4 года назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      Here is my .vimrc

      .vimrc
      • »
        »
        »
        »
        4 года назад, # ^ |
        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

        • »
          »
          »
          »
          »
          4 года назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          How to compile first and how to run it in terminal?

          • »
            »
            »
            »
            »
            »
            4 года назад, # ^ |
              Проголосовать: нравится 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
        • »
          »
          »
          »
          »
          9 месяцев назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          Is there any solution for windows, it's working in linux but showing error in powershell

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    thank you! But is this the same for VIM in windows too?! Where the .vimrc file is named as _vimrc.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 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.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 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.

»
4 года назад, # |
  Проголосовать: нравится 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?

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 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.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 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 года назад, # ^ |
    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 года назад, # ^ |
    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.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    bruh! can u share your vimrc file ?

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Not sure if it really helps, but anyway:

      .vimrc
»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Use terminal and Makefile.

Makefile

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(

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    can u share ur github link for the makefile and can u share how does it work on windows

»
4 года назад, # |
  Проголосовать: нравится 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 года назад, # ^ |
      Проголосовать: нравится 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 :-)

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Seems appropriate. But why not just use the custom invocation provided by CodeForces itself?

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 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...
»
4 года назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

simplest form of compiler flag

map<C-b> :!g++ -std=c++17 -O2 % <CR>  
map<C-n> :!./a.out < in <CR>
  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I use

    autocmd filetype cpp nnoremap <F9> :w <bar> !g++ -std=c++17 % -O2 -Wall -Wextra -DLOCAL -o %:r -Wl,--stack,268435456<CR>
    autocmd filetype cpp nnoremap <F10> :!%:r<CR>
    

    just press for compiling and for executing. :P

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      it's working, but how can i run, compile and save from a single command??

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 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 года назад, # ^ |
        Проголосовать: нравится 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 .

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 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 <F5> :w <bar> !g++ -std=c++17 -O2 -Wall % -o %:r && %:r.exe <CR>
        

        in your .vimrc.

        • »
          »
          »
          »
          »
          4 года назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          But, how can i add this --stack,268435456 in vimrc for compiling & running at the same time !?

        • »
          »
          »
          »
          »
          4 года назад, # ^ |
          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 line

          ), 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?

          • »
            »
            »
            »
            »
            »
            4 года назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            Precompiled headers.

            • »
              »
              »
              »
              »
              »
              »
              4 года назад, # ^ |
                Проголосовать: нравится 0 Проголосовать: не нравится

              i did that also, but didn't work! u can check on your windows also. thanks!

              • »
                »
                »
                »
                »
                »
                »
                »
                4 года назад, # ^ |
                  Проголосовать: нравится 0 Проголосовать: не нравится

                For the F5 command try using this

                autocmd filetype cpp nnoremap :w !g++ -std=c++14 % -o %:r -Wl,--stack,268435456 && %:r.exe
                
  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    when i tried your code, it says the system cannot find the file specified. shell returned 1?? what should i do??

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 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.

»
4 года назад, # |
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.

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Did you consider using a text editor with vim keybindings?

»
4 года назад, # |
  Проголосовать: нравится 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.