Version 27 of Vim

Updated 2004-04-09 22:10:31

Purpose: to discuss the Vim editor.


First, you can find Vim at http://www.vim.org/



MC 16 Jul 2003: This is probably old news to many, but I finally stumbled upon this little gem only this evening:

vim already does syntax highlighting for *.tcl files. For files without an extension, however, adding a comment in your code that says:

 # vim: syntax=tcl

causes vim to do the highlighting properly.

glennj This magic depends on two settings: (default values)

 set modeline modelines=5

vim will then look at the top 5 lines and the bottom 5 lines for these special comments. See

 :help modeline

Modelines are handy for "forcing" people to conform to particular vim settings. For example, if you like a 8 space indent using tabs, but you're editing someone else's code who likes 4 space indent and no tabs, then in order not to mess up that person's code, you'll want a modeline like (demonstrating the alternate modeline syntax)

 # vim: set shiftwidth=4 smarttab expandtab:

Configuring Vim to conform to yhe Tcl Style Guide

indenting

    set autoindent           " keep the previous line's indentation
    set cindent              " indent after line ending in {, and use 'cinwords'
                             " see also ':help c-indent'
    set shiftwidth=4

do not inadvertantly break a line

    set textwidth=0

comments

    set comments=:#
    set formatoptions+=r      " Automatically insert the current comment leader
    set formatoptions+=q      " Allow formatting of comments with 'gq'

prevent the comment character from forcibly being inserted in column 1

    set cpoptions-=<          " allow '<keycode>' forms in mappings, e.g. <CR>
    inoremap # X<BS>#
    set cinkeys-=0#           " # in column 1 does not prevent >> from indenting
    set indentkeys-=0#

Version 6 of vim has a wonderful feature called folding, where blocks of text can be hidden as a single line until you have to enter them. However, I can't seem to get syntax-based automatic folding of procs. The obvious

    set foldmethod=syntax
    syntax region tclFunc start="^proc.*{$" end="^}" transparent fold

does nothing. Folding blocks works fine with

    syntax region tclBlock  start="{" end="}" transparent fold

but I don't really want to fold every block, I'd rather do it on the proc level. Any suggestions?

US The start regex interferes with "syntax keyword" in the tcl syntax file. Change the line

    syn keyword tclStatement        proc global return lindex

into

    syn keyword tclStatement        global return lindex
    syn match   tclStatement        "proc" contained

and add the (slightly extended) region rule

    syntax region tclFunc start="^\z(\s*\)proc.*{$" end="^\z1}$" transparent fold contains=ALL

Now it should (mostly) work. The "z" part of the start and end regex allows for arbitrary indentation of your proc definition.

There is (at least) one pathological file: I reaped the bitmap editor http://wiki.tcl.tk/6298 and tried the above folding rules on it. It works quite nice, but not for the procedure definitions of ClearBMP, NewBMP, NewDlg and BitFunc. I have no idea, why. Maybe someone else knows better.

NEM I just tried this out, and it doesn't seem to work very well at all. It seems to get the start of procs ok, but the end of the proc seems to be somewhat arbitrary. A couple of procs seem to be closed by a } but not the correct one (like the end of an if statement). One proc was actually closed after a button command, which was quite strange as it didn't contain any braces at all... I don't know Vim re syntax but (\s*\) looks suspicious to me (is the end paren escaped? if so, why?), and what does \z1 do? Presumably, it means the same amount of indenting as at the start of the proc?

AMucha vim folding with marker Use foldmethod marker:

First include in .vimrc:

   filetype plugin on

in $VIMRUNTIME/ftplugin/ (in my linux box this is usr/share/vim/current/ftplugin/) edit file tcl.vim to look like:

 " This is file $VIMRUNTIME/ftplugin/tcl.vim
 " Vim FileType plugin for Tcl/Tk-Files adding folding
 "     
 source $VIMRUNTIME/ftplugin/vim.vim
 :setlocal fmr=<-<,>-> fdm=marker cms=#%s
 " this sets the folding marks to <-< and >->
 function! MyFoldText()
    let line = getline(v:foldstart+1)
    let sub = substitute(line, '/\*\|\*/\|{{{\d\=', '', 'g')
    return v:folddashes . sub
 endfunction
 set foldtext=MyFoldText()
 " this shows the line after the foldmark as text
 map <F5> :call append(line(".")-1, "\# <-<" . (foldlevel(line(".")) + 1))
 map <F6> :call append(line("."), "\# >->")
 " maps to help insert foldmarks. Use: go to end of proc on } and press F6 % F5
 " ############################################
 " end of tcl.vim

If you are daring enough to use a proc with german comments you can add in the same file :

 function! Foldproc()
    :normal 1g
    let s:anz=0
    let W = "W"
    while search("proc\\>",W) > 0
        let s:startpos = line(".") - 1
        let s:anz = s:anz + 1
        "auf das zweite wort gehen (Argumente)
      " go to 2nd word (args)
        :normal WW
        "testen, ob das erste Zeichen der Argumente eine ist
      " test if the first char is a brace
        let startofargument =strpart(getline(line(".")), col(".") - 1 , 10)
        if  match(startofargument,"{") == 0
            "eine Argumentenliste
            if searchpair("{","","}",W,'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"') <= 0
                " keine schließende klammer gefunden
                " die nächste proc probieren
                continue
            endif
        else
            "ein einzelnes wort ; no ist a single word
        endif
        " wir stehen jetzt entweder am beginn eines wortes oder auf der
        " schließenden klammer
      " we are at the beginning of a single word argument 
      " or at the closing brace of an argumentlist
        :normal W
        " Jetzt stehen wir auf der öffnenden Klammer
      " this is the opening brace of the proc
        let startofargument =strpart(getline(line(".")), col(".") - 1 , 10)
        if  match(startofargument,"{") == 0
            " if searchpair("{","","}",W,'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"') <= 0
            if searchpair("{","","}",W,'') <= 0
                " das ende der Proc ist nicht gefunden
            " didn't find end of proc
                continue
            endif
        else
            continue
        endif
      "insert foldmarks
        call append(s:startpos , "\# <-<1")
        call append(line("."), "\# >->1")
    endwhile
 endfunction

To call this proc to place foldmarks around all your procs type

 :call Foldproc()

 What: Vim
 Where: http://www.vim.org/ 
        http://www.bodenstab.org/#VIMCONSOLE 
 Description: Vi clone which provides language syntax colorization (including
        Tcl), command line edit and recall, filename completion,
        split windows, mouse control, drag and drop, and other features.
        The vimconsole is a Tclsh shell that interacts with the
         Tcl support one can build into vim.
        Currently at version 6.0 .
 Updated: 09/2001
 Contact: mailto:[email protected] 

dgonyier Can anybody say why VIM has a seperate syntax file for expect and TCL? I would think it would make more sense for the latter to be a superset of the former...


Category Application