2

I am preparing for the Egyptian Olympiad in Informatics and I prefer vim to be my code editor, but my problem is my vimrc, I think it is a bit long to type at the beginning of an onsite competition, but really is it? Here it is.

set nocp

syntax on
colo torte

set rnu nu nowrap
set hi=1000
set cb=unnamed

set si
set ts=4
set sts=4
set sw=4
set sr

set is hls
nnoremap <CR> :noh<CR><CR>

inoremap {<CR> {<CR>}<C-o>O
map <F7> :w<cr>:!g++ -Wall -Wextra -Werror % -o %<<CR>
map <F8> :w<cr>:!g++ -Wall -Wextra -Werror % -o %< && %<<CR>
map <F9> :w<cr>:!g++ % -o %< && %<<CR>

I really don't mind forgetting to write any one of the above lines (except the last four), it won't be a very big deal, but when I look at each line, they all look well worth the time needed to write them.

Should I memorize my vimrc as it is, or are there any changes you think I should do?

7

The first line really should be runtime defaults.vim. This is a standard file and it must be present in all Vim installations. Among other things, it saves from typing

syntax on
filetype plugin indent on
set hi=200
set is

Also set cb=unnamed is evil (just prepend "+ before normal mode command when needed) and set hls IMO is more harmful (every time doing :noh) than useful. set si is no-op for C/C++ sources if you have ftplugin enabled.

Build with :make command, use set aw to save on make automatically.

So something like this looks shorter and still functional to me:

runtime defaults.vim
colo torte
set aw nu rnu nowrap
set et sr sts=-1 sw=4
nn <f7> :make CXXFLAGS="-Wall -Wextra" %:r<CR>
10

set nocp is not actually needed if your vimrc is located at ~/.vimrc or ~/.vim/vimrc.

Many of your commands can be abbreviated even further

sy on
colo torte

se rnu nu nowrap hi=1000 cb=unnamed si ts=4 sts=4 sw=4 sr is hls

nn <CR> :noh<CR><CR>
ino {<CR> {<CR>}<C-o>O
3

There are a few interresting things already in the default C and C++ filetype plugins -> filetype plugin indent on. This should enable smart indenting. No need anymore to use tabs, which could be left to the universal default of 8 -- I never use tab but always let Vim handle automatically the indent.

Regarding compilation, :make %<<cr> is enough -- Just set $CXXFLAGS once. Beside, this would fill the quickfix windows. In C++, this could really make the difference when dealing with ambiguities or failure to find the right overload.

let $CXXFLAGS = '-std=c++20 -Wall -Wextra -Werror'
nnoremap <silent> <f7>   :<c-u>w<cr>:make %<<cr>
nnoremap <silent> <c-f5> :<c-u>vert term ./%<<cr>
nnoremap <silent> <f11>  :<c-u>cprev<cr>
nnoremap <silent> <f12>  :<c-u>cnext<cr>
  • 1
    Great recommendations! I think you mean filetype plugin on. And for automatic indentation I think you also need filetype plugin indent on. Good call on using :make, that's really the way to go. – filbranden Nov 30 '20 at 23:52
  • 1
    Indeed. Thanks @filbranden, I've written it from memory. I'll fix my answer. – Luc Hermitte Dec 1 '20 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.