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

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

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

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

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

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)

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

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>

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

    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).

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

My .vimrc configuration.

F5 — Compile

F9 — Run

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

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

      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?

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

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

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

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.

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

    Can you share those scripts?

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

      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>.

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

    hey can you please share how to read multiple inputs like this using ultisnips?

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

      https://pastebin.com/taThf3vr

      Besides reading multiple inputs, it additionally supports autocompletion for common types like int, long long, or even pair<long long, long long>. E.g. you can press read<C-J>pair<l<C-J>a, b<C-J> and it expands to pair<long long, long long> a, b; cin >> a.first >> a.second >> b.first >> b.second;. It will actually show you the possible completions in real time, once there is only one completion, it you can press <C-J> to accept it.

      If you don't need the completion or the support for pairs, you can probably get rid of most of the code, and just use a transformation that converts , into >>. Something like the following (note: I haven't tested it): https://pastebin.com/1P1Ayy6j

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

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

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.

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

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

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

      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!!

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

    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.

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

Vim like VSCode Got inspired during lockdown and I learnt vim during this time. Here is a link to my neovim config files and you can see the finished config is something really close to VSCode. I spent 2 days learning and making this for NeoVim. If anyone is interested check it out on the given link. https://github.com/AnmolTomer/vim