lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi all,

this is OT, but for those that edit Lua files with vim, these tips might
be useful.

NOTE: the instructions below apply to vim on UNIX; it should be
similar on Windows, but don't ask me how because I simply don't know.

I was reading vim's manual and found out about 'folding', which is an
interesting feature. To put it in a few words, you can tell vim to
'fold' a number of lines, collapsing them into a single line. This might
make editing easier for some, and can easily be turned on/off anytime.

There are a couple of different folding options (manual, syntax,
expression, markers, indent). For details about each one of them, please
refer to vim's manual (vim -c :help), it's on |usr_28.txt| section. But,
here's a quick tip to use syntax folding based on standard lua.vim
syntax file.

SYNTAX FOLDING
=====

Lua's vim syntax file isn't "folding-enabled", so some manual tweaking
is necessary. First, copy it to your own dir:

- mkdir -p ~/.vim/syntax
- cp $VIM/syntax/lua.vim ~/.vim/syntax

($VIM usually points to your vim installation dir, like
/usr/share/vim/vim62)

Now vim will automatically load your syntax file instead of its own.
Next, modify it appending the 'fold'  keyword at the regions definitions
you want it to consider "foldable", such as:

" Function declaration      
syn region  luaFunctionBlock    transparent matchgroup=luaFunction
start="\<function\>" end="\<end\>"
contains=ALLBUT,luaTodo,luaSpecial,luaCond,luaCondElseif,luaCondEnd,lua
Repeat fold

That's it =) Now, try editing a .lua file, and type
:set foldmethod=syntax

With the above settings, all functions will be automatically folded
after you type the last command. Notice that folding is recursive,
so expect nested folders. Some quick reference for useful commands:

zR : opens all folders
zM : collapses all folders
zo : opens the folder you're in (1 level)
zO : opens all folders recursively at the cursor line
zc : collapses the folder you're in (1 level)
zC : collapses all folders recursively at the cursor line
zn : deactivates folding
zN : restores folding to previous state
zi : toggles between zn and zN

MANUAL FOLDING
=====

Manual folding is also cool and might be preferred by some. It is the
default folding mode, but it can be activated anytime with
:set foldmethod=manual

As the name implies, you create folders arbitrarily. One way is to
select a number of lines with SHIFT+V and type

zf

This will define a folder for the selected lines, to which all above
folder commands apply.

After you define your folders, you might want to save them for later
use. You can do this with:

:mkview --> saves folders
:loadview --> restore last saved view

You can store up to ten views for a file, and access them directly, with

:mkview N
:loadview N

A warning from vim's manual:

"Note that when you insert or delete lines the views might become
invalid. Also check out the 'viewdir' option, which specifies where the
views are stored.  You might want to delete old views now and then."

You can delete manual folders with
zd : deletes folder on cursor line (1 level)
zD : deletes folders recursively on the cursor line

TIP
===

While folding is active, try setting 'foldcolum' to a value > 0, like
:set foldcolumn=2

Well, that was it. This was a crash-course, browse the documentation
for more info. For those who are not interested on this, sorry for the
noise.

Best,

Andre

-- 
Andre Oliveira da Costa