Wednesday, April 1, 2015

Playing with vim configuration for C++

What I got as a result:


The main features:
  • embedded file system explorer
  • syntax highlighting
  • hotkeys for compiling/running C++ code

HOWTO:

1. Install vim

$ sudo yum install vim

2. Install pathogen.vim for easy installation of other plugins

$ mkdir -p ~/.vim/autoload ~/.vim/bundle && curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
$ vim ~/.vimrc

1
2
3
4
" pathogen.vim configuration
execute pathogen#infect()
syntax on
filetype plugin indent on

3. Install NerdTree for exploring file system

$ cd ~/.vim/bundle
$ git clone https://github.com/scrooloose/nerdtree.git
$ vim ~/.vimrc


1
2
3
4
5
" NerdTree configuration
"hotkey to open/close nerdtree
map <C-n> :NERDTreeToggle<CR>
"close nerdtree if there are no more opened file
autocmd bufenter * if (winnr("quot;) == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif

4. Add hotkeys for compiling/running C++ code

$ vim ~/.vimrc

1
2
3
4
5
6
7
8
" Add a hotkey to save and compile c++ code
autocmd filetype cpp nnoremap <F4> :w <bar> exec '!g++ '.shellescape('%').' -o '.shellescape('%:r')<CR>

" Add a hotkey to run c++ code
nnoremap <F5> :exec '!./'.shellescape('%:r')<CR>

" Add a hotkey to save, compile and run c++ code
autocmd filetype cpp nnoremap <F6> :w <bar> exec '!g++ '.shellescape('%').' -o '.shellescape('%:r').' && ./'.shellescape('%:r')<CR>


My .vimrc content:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
" pathogen.vim configuration
execute pathogen#infect()
syntax on
filetype plugin indent on

" NerdTree configuration
"hotkey to open/close nerdtree
map <C-n> :NERDTreeToggle<CR>
"close nerdtree if there are no more opened file
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif

"Not vi compatible
set nocompatible

" Add a hotkey to save and compile C++ code
autocmd filetype cpp nnoremap <F4> :w <bar> exec '!g++ '.shellescape('%').' -o '.shellescape('%:r')<CR>
" Add a hotkey to run C++ code
nnoremap <F5> :exec '!./'.shellescape('%:r')<CR>
" Add a hotkey to save, compile and run C++ code
autocmd filetype cpp nnoremap <F6> :w <bar> exec '!g++ '.shellescape('%').' -o '.shellescape('%:r').' && ./'.shellescape('%:r')<CR>

" formatting
set showmatch "Highlight matching braces
set tabstop=4
set softtabstop=4
set shiftwidth=4
set noexpandtab
set autoindent "Copy indent from the row above
set si "Smart indent
"set nu "Number lines
set hls "Highlight search
set lbr "Line break

" Toggle line numbers
:nmap <F12> :set invnumber<CR>

In plans:

No comments:

Post a Comment