Hi! Please consider following me on twitter: @hanekomu.

use() a module without disrupting your editing flow

In his blog, Jonathan Rockway writes:

Anyway, tonight while working on App::TemplateServer, I was sitting at the bottom of a file, in the middle of a statment like my $foo = Some::Module-> when I realized I needed to use Some::Module; at the top of the file. That's always painful, because I have to set the mark, move to the top of the file where the use() statements are, add the use() statement, exchange-point-and-mark, and then try to remember what I was doing before my flow was broken. This takes at least 10 seconds and stresses me out, so I decided to automate it.

He wrote a function and mapping for emacs, but since I use vim, I've adapted his good idea for vim:

    fun! UsePackage()
        let default = expand("<cword>")
        call inputsave()
        let module = input("Module (default " . default . "): ")
        call inputrestore()
        if module == ""
            let module = default
        endif
        normal mz
        normal G$
        call search("^use ", "b")
        call append(line("."), "use " . module . ";")
        normal `z
    endfunction

    nmap <C-k><C-u> :call UsePackage()<CR>
    imap <C-k><C-u> <Esc><C-k><C-u>a

So when you decide that you need to use() another module, just press CTRL-k CTRL-u in either insert or command mode, and enter the module and optional imports. The default is the word under the cursor. I don't use the two-argument form of vim's input() method becase that "types" the default on the input line, so if you don't want to use the default you have to backspace it. The version in the code above just displays what the default would be if the user enters an empty string.

The default works because I also have a trigger that, When a perl source file is opened, calls a function that sets some vim variables to be more useful to perl hacking; among those is:

    setlocal iskeyword=48-57,_,A-Z,a-z,:

Note the colon at the end; this makes vim recognize Perl package names as words.

Write a comment | Bookmark and Share

posted at: 12:01 | path: /dev | permalink | 0 comments | 0 trackbacks

dotfiles.org

My dotfiles are at http://dotfiles.org/~hanekomu - they are also part of Dist-Joseki. Thanks to Andy Lester for the tip.

Write a comment | Bookmark and Share

posted at: 12:16 | path: /dev | permalink | 0 comments | 0 trackbacks