Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Sunday, May 24, 2009

My Excerpt From C/C++ Support Vim Script

Today I read the document of c.vim. Features that I'll use later are outlined below.

Inserting a template/snippet

Key mappings to insert a template/snippet bellow.

Legend:  (i) insert mode, (n) normal mode, (v) visual mode

\c*       code -> comment /* */               (n,v)
\c/       code -> comment //                  (n,v)
\cc       code -> comment //                  (n,v)
\co       comment -> code                     (n,v)
\p<       #include <>                         (n,i)
\p"       #include ""                         (n,i)

I wonder why The first four key don't have operator-pending mode? We can never commentout 3 lines without using visual mode.

There are a lot of key mappings to insert a template of a snippet. I'm doubtful about the effectiveness of those snippets. The most surprising one is below:

\i0       for( x=0; x<n; x+=1 )               (n,v,i)

Of course we can easily fix those templates. see ~/.vim/c-support/codesnippets/. For example, \p< doesn't insert #include <> but #include\t<>, and even worse the snippet will inserted on the next line, therefore I have to fix the feature or to decide not to use it.

Syntax based folding

In the document, it is written that

Syntax based folding can be enabled by adding the following lines to the file '~/.vim/syntax/c.vim':

 syn region cBlock start="{" end="}" transparent fold
 set foldmethod=syntax
 " initially all folds open:
 set foldlevel=999

But I found that it does not work well. It has a lot of bug. Following setting works well:

" in ~/.vim/syntax/c.vim
set foldmethod=syntax

But I prefer that only functions are folded, so finally I deleted the file. I have to find how to do it.

Compile and Run

\rc       save and compile                    (n)
\rr       run                                 (n)
\ra       set comand line arguments           (n)
\rm       run make                            (n)
\rg       cmd. line arg. for make             (n)
\rp       run splint                          (n)

I don't understand of there is a special key mapping \rc or \rm instead of using preexistent :make feature. I checked :compiler but it was not set.

I always set <space>m as :make, so I wrote below on my vimrc.

augroup MyCompiler
  autocmd Filetype c nmap <buffer> <Space>m \rc
  autocmd Filetype cpp nmap <buffer> <Space>m \rc
augroup END

\rr is OK. I can use both \rr feature which outputs temporary and quickrun.vim which outputs permanentry.

No comments:

Post a Comment