Posts Tagged: vim


20
Jan 11

Hiding that horrible tabline in Vim

Long time no post in here, sorry.

Since I’ve had the chance to review Kim Schulz’s Hacking Vim 7.2 ebook1 and a friend of mine started using vim as his main development tool, digging into Vim’s configurations and flavours has become somewhat even more pleasant.

In an attempt to improve my performance when editing various files, I’m trying to use tabpages instead of only several windows. It’s been working as some kind of context stack for me, as there can be one tabpage (probably with fewer windows) for modifying files from a project and another tabpage for a different but related one (or a non-related task in the same project). Plus, using more tabpages and less windows can save precious screen space.

But what to do about that odious tabline which, besides hurting my sense of aesthetics, eats a whole line of the screen? Thankfully to the Vim’s extremely flexible nature, it didn’t take long to find an answer. It came in form of disabling the tabline entirely and putting its only usable information into the statusline. Used a bit of scripting to overcome the only corner case I could think of and took the chance to try my rusty skills on python + vim bindings. Pretty much everything someone will need to put both domains together is in this little example (besides the vim module complete API, easily accessible by issuing :h python).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function! MyTabMarker(disable)
python << EOF
import vim
def tabpagecount():
return int(vim.eval("tabpagenr('$')"))
def tabpage():
return int(vim.eval("tabpagenr()"))
def tabmark():
disable = int(vim.eval("a:disable"))
if tabpagecount() > 1 and not disable:
return "\"[tab %d/%d] \"" % (tabpage(), tabpagecount())
return "\"\""
vim.command("return " + tabmark())
EOF
endfunction
set laststatus=2
set showtabline=0
set statusline=#%02n\ %{MyTabMarker(0)}%f\ %m%r%y%=\(%bd,%Bh)\ %c,%l/%L\ %P

There you see how to pass arguments into python methods and how to feed values back from it to the vim environment. Eventually I’ll publish my entire .vimrc with comments so everyone can rant about how theirs way is better ;-)

[1] as I’ve lost my notebook due to a robbery at the technical assistance, my review annotations were lost too. I’ll need to re-review the book to get something published about it then.


9
Apr 09

Vim Scripting Using Python

Trying to solve a colleague’s problem when editing Edje files, I pushed myself into learning how to do scripting inside Vim.

Scripting wasn’t ever my choice for reasonably complex editing tasks since I refused to learn Yet Another Scripting Language just because my favorite editor wanted me to. But, for the sake of all lazy guys like me, Vim started to add python support, and python was a must learn bullet in my language listing.

However, not everything are flowers and the entry point must be configured using common vim script. Well, at least until vim supports python from its core, which – I believe – is not far from possible, since python integration has been voted as top priority for a long time.

To successfully import your python script inside vim context, one can wrap it into a vim function in an external file. Let’s call it extras.vim.

1
2
3
4
5
6
7
8
9
10
11
" Title-ize sentences using python str methods
function! PyMakeTitle() " the ! erases previous definitions
python &lt;&lt; END " here-document (bash-style), read 'till given word
import vim
w = vim.current.window
b = vim.current.buffer
line, col = w.cursor
line -= 1
b[line] = b[line].title() # str.title() method
END
endfunction

After using :source extras.vim command to load it, one can call this function by typing :call PyMakeTitle(). Remember repeating the :source step every time the script gets updated.

The net effect of this function is to turn all initial word letters in the current line into capitals. It proved me to be useful when editing a large LaTeX document where all section titles were small letters only.

If it comes to be a very useful function, you may map it to a key-stroke by using :nmap \t :call PyMakeTitle()&lt;CR&gt; inside your vimrc script.

A more complex example accessing internal vim properties. Ok, it’s a bit useless, but it demonstrates well such features.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
" Auto Documentation (example code)
function! PyCreateDoc()
python &lt;&lt; END
import vim
name = vim.eval("expand(\"&lt;cword&gt;\")") # expand word under cursor
ts = int(vim.eval("&amp;ts")) # tab space property
il = int(vim.eval("indent(\".\")")) # indentation on current line
w = vim.current.window
l, c = w.cursor
docstr = '%s# @brief %s - write description' % (' ' * il, name)
b = vim.current.buffer
b[l-1:l-1] = [ docstr ]
print 'Indent set is %d, cursor at (%d, %d)' % (ts, l, c)
END
endfunction

With the cursor over a function name, type :call PyCreateDoc() and watch it insert a line above containing doxygen-like formatting.

For more examples, there is an excellent material at vim’s online help (see :h python). Furthermore, there are some plugins being made using only python, take a look at Omni Completion for Python (pythoncomplete.vim) for a good reference on the power of using python inside vim.

Happy Easter Vim’ing!