Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

Tuesday, February 10, 2009

User Input From Vim to Python script

hi, ever wondering how to prompt a user for some input from python script within vim, but first make sure you have imported the vim module

def python_input(message = 'input'):
vim.command("call inputsave()")
vim.command("let user_input = input('" + message + " : ')")
vim.command("call inputrestore()")
return vim.eval('user_input')

now everytime you wanna prompt user, in your python script do something like

input = python_input('input something')

it will prompt the user to input something with 'input something' as the caption of the prompt, in the command-line

for more detail

:help input()

hope, this would help.

Monday, January 05, 2009

Easy Remote PHP Debugging with VIM and Firefox

Happy new year !

I've write a post bout how you can setup debugging environment for PHP using xdebug, so now assuming you have xdebug setup and loaded, I'm going to write bout how you actually debug your PHP code.

Basically there are 2 type of debugging, local or remote debugging, the difference is that whether or not the debug engine separated with the debug client. If you debug a desktop Java code using IDE like Eclipse, most likely you are doing local debugging, where as the debug engine comunicate directly to the eclipse interface (there is no need of debug client). XDebug is a remote debug engine, which mean to actually debug some code we need a debug client to send command to and receive debugging information from the debug engine. From XDebug page we can get alot information bout how this can work, and some option of debug client that you can use (this page should be your first reference).

Now lets get to the chase, I'll write about how to have a remote debugging session with Vim (with plugin) and firefox to debug your PHP code easily.
  1. After you have successfully load xDebug module into php, enable remote debugging in your php configuration (php.ini) by adding
    xdebug.remote_enable = "1"
  2. download the debugger plugin (or watch my blog for an update)
  3. extract the file (debugger.vim and debugger.py) to your home vim plugin directory
    $ unzip debugger.zip
    $ cp plugin/debugger.* ~/.vim/plugin/
  4. fire up Vim!, make sure your build equiped with signs and python support or the plugin won't work!, I'm using ubuntu and a Vim-full package for this, no hush, no mush..
  5. if vim loaded without any error it means the plugin already loaded correctly, if not check your vim build, make sure the +signs and +python is there when you type in vim command
    :version
  6. open firefox, and install this addon
  7. open the PHP application you wish to debug using firefox, make sure the source code is accessible. for example if the source is in (assume you are using xampp)
    /opt/lampp/htdocs/testdebug/index.php
    then you can type in firefox url
    http://localhost/testdebug/index.php
  8. click the xdebug helper icon (the one you've just add) on firefox status bar (default on lower right corner), now the icon should flash green
  9. switch to your vim window and press F5 (the message window would say waiting for connection...)
  10. switch to your firefox window and reload by pressing F5,
  11. now switch to your vim window (again) and there you have it, a debugging interface of your application in vim.
let me explain a few of those steps a little bit (step 1 to 7 should be clear enough), step 8 means you have start a debugging session for the next http request, it is the same as if you append the url with XDEBUG_SESSION_START=1 so the url would be
http://localhost/testdebug/index.php?XDEBUG_SESSION_START=1
ofcourse clickin on xdebug helper icon is alot more convenience than appending the url each time we start a debugging session.

now for step 9 we prepare our beloved Vim to wait a connection from the debug engine at this point vim will kind of hang (waiting mode), the script default timeout is 5 second, if within that there are no debugging session requested, vim will return to normal mode. This means than we have to quckly run step 10, switch to firefox and reload, but again make sure the xdebug helper icon is already flash green (activated/clicked). After you have reload the page if you notice the loading status of the page is keep on spinning, the page wont finish loading until the debugging session stopped or vim is closed or request timeout

Now if we switch back to Vim, our debugging interface should up and running there are source, watch, help, stack and trace window. From this interface you can trace the code using F2 (step into), F3 (step over), F4 (step out). To end a session you can press F6. One of most common task is to put a breakpoint in our code, to do that open the file you wish to debug, position your cursor on a line you wish to stop and type :Bp in vim command window, now everytime the debugging session start, just press F5 to run until execution point reach the breakpoint. You can see other debugging command from the help window.

one thing about this vim plugin, there aren't that much debugging command available, either from the plugin or xdebug itself, from what I can tell it is more than enough to do my daily-debugging-basis. There are some lacks though, but I'll address this issue later on, considering this post is already bloated enough to make people sick from reading it :p

happy vimming (and debugging) !

keyword: ubuntu, vim, firefox, xdebug, php

Saturday, November 01, 2008

Vim Easy Folding Tip

for me the most easiest way to use folding in vim is to use
:set foldmethod=indent
intuitive and simple.

when you use foldmethod=indent the folding will be based on indent (why of course :p), meaning the fold start and fold end is marked by line indentation (can be a tab, spaces, etc..)
and these are the command I use the most.
  • zm : reduce fold level
  • zM : fold level to zero, aka fold'em all !
  • zr : increase fold level
  • zR : fold level to the max, aka remove all fold
  • za : toogle fold open/close on current cursor
  • zA : same as above, but recursively
  • zX : undo previous fold command
what is a fold level ?
the higher fold level means less folded text, the lower fold level means more folded text. Consider this snippet of code
when i used indent as foldmethod, indentation level is a fold level. The higher indentation level would have more foldlevel available on that window, when i type zm, foldlevel are reduce to 1, the line start from
$account->publish($... 
is folded, when I type zm again foldlevel are reduce to 0(this is minimum value), then the line
$docs = new... 
is folded, and so on. And viceversa, when I type zr foldlevel are increased, and the line in coresponding foldlevel is unfolded or opened

what if I dont use indentation in my files ?
u can use manual (the default foldmethod) or marker
:set foldmethod=manual
:set foldmethod=marker
note: that when you tried to open a fold recursively (using zA on a closed fold), it work like the way you expected, but when you tried to close a fold recursively it doesnt(well at least I didn't get it at first). It because zA on open fold, close a fold starting from the cursor as the bottom! not the top!, so how to close fold from the cursor to the bottom/deepest foldlevel, my current workaround is to visually select those region an use zC

find out more bout vim's folding feature..
:help folding
happy vimming !!

Tuesday, October 21, 2008

VIM

Since my migration to linux I've set myself on a quest to find the ultimate text editor (haiiah, lebay :p). well anyway I have my own requirement for such a tool
  • lightweight and fast
  • syntax highlighting and formatting
  • tab-editing
  • auto-complete
  • flexible and configurable (support custom script)
when I'm still using windows, I didnt really pay attention on those criterias, coz most of the time I'm stuck with an IDE(not just a text editor). Komodo, eclipse to name a few. and just lately I realized that most of the tool those IDE provided is less usefull for me(well mostly because some of em isn't free :D), I just need the editor, and yes syntax highlight and formatting is a tempting feature for sure. So I started to googling, to find a good editor which presumably free, been trying this and that, and there it is VIM !.

VIM (Vi IMproved) is a console based / command line editor a charityware wrote by Bram Moolenaar, and available as default in most of linux distro (the silly part is that the whole time the one I'm looking for is right under my own nose yet I'm searching or downloading from various places). With a default plugin gVim (gnome version of vim) load up less than a second in my 1420 inspiron box, whoossh.... It support syntax highlight and formatting for most of programming/scripting language, and my favorite is the omni completion feature, its an auto-complete feature with a dozen of option and preferences to suit my need, it can use a dictionary file or just simply scan a whole directory to find the completion words.

Highly configurable, vim support custom script called vimscript (beside it's support on phyton, perl, and other script), which make it a lot more usefull for many editing purpose, theres a lot of ready made script from it's main site, some of the script I've been using are
  • NerdCommenter, this plugin helps to do any function of commenting and uncommenting, and it support comment for huge number of programming language and script,
  • snippets_emu, this is a wonderfull one, just type few letter and shift+tab(my mapping keys) and boom!the whole snippet of code is there! save a lot of time of typing, you could define your own snippet especially for something you'd type frequently
  • NerdTree, its like an explorer built in the editor, you can browse file and directory from it
  • TagList, you can tag a file of code or a whole directory ( in *nix u can use ctags), and jump between various tag in a file, this is a lot faster than using pageup-pagedown, or scrolling your mouse up and down to go to different section in your file
  • VimTip, for any of you that just start learning vim this plugin is a must!, every day it pops a tip bout what you can do with vim, contributed from various user all over the world.
  • and a lot of any other I cant mention here, I cant thx enough to every of you that develop these vimscript and plugins !
One unique thing about it, is that VIM is a modal-based editor, which means it responds input differently based on its mode(state), for example vim has (from several other modes) input-mode and command-mode, when in input-mode vim behave like any other editor, everything I type is considered as text input, but when in command-mode the typing goes as command, I love it so much coz it allows me to use most of vim's editing function without have to move my finger off my keyboard home keys !!, it makes editing process a lot more efficient!

Gosh, theres a lot of feature and advantage I just wont have enough time and space to tell em all, and yes, every day I always learn something new bout this editor, not so much different with most of you, I'm still a newbie vimmer.

Well, I have admit there are some drawback. The first maybe because of the learning curve, it takes some amount of brain power to at least start editing efficiently, and for me, it kind of hogging my short term memory the first time I learned it(but trust me, its a worthy investment)

until the day I post this, vim development is still going active (thx Bram!!), the last stable version was Vim 7.2, unfortunately it seems ubuntu wont put these updates in the main repo for at least another release cycle, well we can always download the source and compile it for ourself cant we :p

happy vimming !!