On 8/23/06, Khubaib <[EMAIL PROTECTED]> wrote:
hello, i am a good user of VIM, but not very advanced. I use macros a lot. a frequent one 
that i use is to comment code in verilog, C, perl etc. now, i go back and forth frequenty 
between various code, and code comment macro (@c) and uncomment macro (@u) have to be 
different for perl and C, e.g. Now, what I do is that everytime I come to C code, i have to 
redefine the macro (pressing qc0i//<esc>jq) and next time i go to perl code, i have 
to do it again (qc0i#<esc>jq) .
i was wondering if i can put these keystrokes in a file (e.g. C_comment.macro and 
perl_comment.macro), and can source the file in VIM whenevr i want to. that way, i can put 
frequently used macros keystrokes in files, and read those commands at run time in VIM. any idea 
how to write that *.macro file (with these keystrokes)? I tried but <esc> does not work. (i 
put following thing in file: qc0i//<esc>jq. now when i source it, it keep inserting 
<esc> etc too, which is bugging me).
summary: what I want to do is that in VIM window, instead of pressing 
keystrokes on keyboard, VIM reads the keystrokes (as exactly if it were from 
keyboard, including insert mode) from a file...

There are many solutions for that. Here is what I use:
function! ToggleComment()
   let x=@/
   if &filetype == 'cpp'
       if getline('.') =~# '^//'
           s+^//++
       else
           s+^+//+
       endif
   elseif &filetype == 'c'
       if getline('.') =~# '^//'
           s+^//++
       elseif getline('.') =~# '^\s*/\*'
           s+^\(\s*\)/\*+\1+
           silent s+\*/++g
       else
           s+^+/*+
           s+$+*/+
       endif
   elseif &filetype == 'vim'
       if getline('.') =~# '^\s*"'
           s/^\(\s*\)"/\1/
       else
           s+^+"+
       endif
   else
       if getline('.') =~# '^#'
           s+^#++
       else
           s+^+#+
       endif
   endif
   norm j
   let @/=x
endfu
map <f2> :call ToggleComment()<cr>

Yakov

Reply via email to