Reminder: in case of any technical issues, you can use the lightweight website m1.codeforces.com, m2.codeforces.com, m3.codeforces.com. ×

dkp1024's blog

By dkp1024, history, 2 years ago, In English,

If you are using vim for some time, maybe it will be helpful for new coders like me to know your .vimrc file?
Peace.

 
 
 
 
  • Vote: I like it
  • +12
  • Vote: I do not like it

»
2 years ago, # |
  Vote: I like it +10 Vote: I do not like it

standard vimrc

from /usr/share/vim/vimrc.

mapping(copy current buffer)

nmap <leader>y ggVG"+y''

useful plugins:

vim-surround (deal with parenthesis)

ultisnips and vim-snippets (code snippets)

»
2 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Open a new cpp file filled with a template of your choice . autocmd BufNewFile *.cpp -r ~/cp/template.cpp
Autocomplete curly brackets inoremap {<CR> {<CR>}<Esc>i<CR><Esc>ki<Tab><Tab>

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    This does the wrong indentation, if you define a new global function. inoremap {<CR> {<CR>}<ESC>O fixes and simplifies the command. (btw filetype indent on should be set for either commands to work).

»
2 years ago, # |
  Vote: I like it +10 Vote: I do not like it

My .vimrc configuration.

F5 — Compile

F9 — Run

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Mine is F5 compile and run :)

    autocmd filetype cpp nnoremap <F5> :w <bar> !g++-4.8 -ulimit -Wall -Wno-unused-result -std=c++11   -O2   % -o %:r && ./%:r <CR>
    syntax on
    set nu
    set clipboard=unnamedplus
    set mouse=a
    set tabstop=2
    set autoindent
    set shiftwidth=2
    set softtabstop=2
    set smarttab
    set expandtab
    set smartcase
    
    set t_Co=256
    set background=dark
    colorscheme monokai 
    
    autocmd filetype java nnoremap <F5> :w <bar> !javac % && java -enableassertions %:r <CR>
    autocmd filetype python nnoremap <F5> :w <bar> !python % <CR>
    autocmd filetype perl nnoremap <F5> :w <bar> !perl % <CR>
    autocmd filetype go nnoremap <F5> :w <bar> !go build % && ./%:r <CR>
    
    
    • »
      »
      »
      11 months ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      That looks really nice. I edited my .vimrc and took some ideas from yours, as well as those from above.

      You can check it out here.

    • »
      »
      »
      3 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      i configured it by i. set tabstop=4 ii. set shiftwidth=4 iii. set expandtab but after submitting code in codeforces it still remains tab size 8. which command works to fix this problem?

      • »
        »
        »
        »
        3 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Maybe you wrote your template in another text editor or before you had configure vim spacing ?

»
2 years ago, # |
Rev. 4   Vote: I like it +13 Vote: I do not like it

Actually there is not really much related to competitive programming in my vimrc. The only mentionable things are my plugins.

YouCompleteMe: intelligent code completion, besides completing variable names, it will also complete functions from the STL and much more. E.g. writing v.pu and hitting tab will complete it to v.push_back if v is a vector, or to v.push if v is a stack. Besides completion, ycm will also check your code for errors and report them to you. So usually you won't get any compile errors, because you seen and corrected them already while writing the code.

ultisnips: lots of short code snippets to make my life easier, my template, some long algorithms, ... E.g. inserting my codeforces template can be done with writing cf<C-J>. Or reading four integers from stdin can be done with readi<C-J>n, m, k, test<C-J> and ultisnips will expand it to int n, m, k, test; and cin >> n >> m >> k >> test; (Demonstration)

vim-commentary: comment/uncomment lines

Conque-GDB: gdb inside vim. I'm not completely happy with it, and most of the time I just use the regular gdb. But that's probably only because so far I was too lazy to adjust a few settings for it.

For my other setup (which might be interesting if you use linux): I additionally use tmux in combination with a few scripts. I've written a start script, that will download all test inputs/outputs of a specific codeforces problem, opens a special tmux session with vim inside. For this session I added a few commands, e.g. prefix t will open another pane, compiles my program, and runs it with each test input and compares the results with the outputs. So checking if my code works only takes one seconds. Here's a short demonstration.

  • »
    »
    15 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can you share those scripts?

    • »
      »
      »
      15 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Sorry, but I don't think that I have the original scripts anymore.

      Nowadays I'm just using this vim plugin: cfparser.vim. It basically does the exact same things. E.g. you can download the test cases, compile and run the tests, and even submit the solution.

      The only change I made is, that I use this script for compiling and running the tests, as it gives a better overview over the tests and the results. To run this script with the default key combination, I created the file ~/.vim/after/plugin/cftest.sh with the line nnoremap <Leader>cft :terminal cftest.sh %<CR>.

»
15 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Compile/Run C++

Save the document, clear console screen, show compile time, redirect compile errors to a file, show runtime, redirect stdin/stdout/stderr from file

nnoremap gb :w<CR>:!printf "\033c" && printf "================\n  Compiling...\n================\n" && time g++ -g -std=c++17 -Wall -Wextra -Wno-unused-result -D LOCAL -O2 %:r.cpp -o %:r 2>&1 \| tee %:r.cerr && printf "\n================\n   Running...\n================\n" && time ./%:r < %:r.in > %:r.out 2> %:r.err && printf "\n\n\n\n"<CR>

Some manual brace autocomplete

inoremap {[ {<CR>}<Esc>O
inoremap {; {<CR>};<Esc>O
inoremap {{ {<CR>}<CR><Esc>kO

Behave like Windows

CTRL+A selects all, CTRL+C and CTRL+V copy/paste to system clipboard, if +clipboard is enabled in Vim compile flags.

source $VIMRUNTIME/mswin.vim
behave mswin
»
11 months ago, # |
  Vote: I like it -6 Vote: I do not like it

Hi everyone, how to write default templates for C++ on Vim. Please help me I tried to google a lot on this, all I could find is https://github.com/tibabit/vim-templates which I am finding difficult to understand. Can someone guide me on this? Also if possible share some scripts for increasing efficiency while using Vim, and learning Vim fast :p

Thank you.

  • »
    »
    11 months ago, # ^ |
    Rev. 2   Vote: I like it -11 Vote: I do not like it

    Also, I have tried this but I think it's outdated as I am not able to access my template in new files.

    • »
      »
      »
      11 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      The beauty of CF. If I would have posted some meme there would have been lots of upvotes and responses. Here, a valid problem and there comes the downvotes. Keep it up CF!!

  • »
    »
    6 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    If you still have this issue, here is how I do it. As someone mentioned before, you place this in your .vimrc file in your home folder.

    autocmd BufNewFile *.cpp -r /path/to/template.cpp
    

    You can change /path/to/template.cpp to wherever your template is stored on your drive.