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

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

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

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

»
6 лет назад, скрыть # |
← 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>

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

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +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!

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

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

      Here is my .vimrc

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

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

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

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

  • »
    »
    6 лет назад, скрыть # ^ |
    ← 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.

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

    bruh! can u share your vimrc file ?

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

      Not sure if it really helps, but anyway:

      .vimrc
»
6 лет назад, скрыть # |
← 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(

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

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

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

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

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

simplest form of compiler flag

map<C-b> :!g++ -std=c++17 -O2 % <CR>  
map<C-n> :!./a.out < in <CR>
»
6 лет назад, скрыть # |
← 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.

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

Did you consider using a text editor with vim keybindings?

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