views:

34

answers:

1

I have this shortcut in my vimrc:

map cmt :!start TortoiseProc.exe /command:commit /path:"%" /closeonend:3 <CR>

What this does is whenever I press 'cmt', vim will open the commit dialog for the file I'm currently editing with vim.

What I'd like to do is write this command in such a way that if I put a number in front of it, it will open the commit dialog for the n-th level directory.

Example to make things clearer:

Let's say I have this file structure project/logs/access.log. If I'm editing access.log and

  • press 'cmt' - I should get the dialog for committing access.log;
  • press '1cmt' - I should get the dialog for committing the logs directory;
  • press '2cmt' - I should get the dialog for committing the project directory;

... and so on.

Note: I'm using gvim on Windows 7

Hopefully someone can help me with this. Thanks.

+1  A: 

I've implemented the desired functionality in the following function. It takes two arguments: a path to the file to start from, and a number of levels to go up. Then it cuts the specified number of tailing path components, and builds up the command to run (according to your example). Then it runs the command and checks exit code returned by shell after execution. If an error has occurred, error message together with command output are shown.

function! TortoiseCommitDialog(path, count)
    let pat = '[/\\]\@<=\%([^/\\]\+[/\\]\?\)\{' . a:count . '}$'
    let path = substitute(a:path, pat, '', '')
    let cmd = 'TortoiseProc.exe /command:commit ' .
    \   '/path:' . shellescape(path) . ' /closeonend:3'
    let out = system(cmd)
    if v:shell_error
        echoerr 'Failed to run Tortoise commit dialog'
        echo out
    end
endfunction

To use this function during editing, I recommend to define a command (because it can handle arguments unlike mapping) like this:

command! -count Cmt call TortoiseCommitDialog(expand('%:p'), <count>)

Because of -count flag you can run the command with count two ways: :3Cmt and :Cmt 3 (or even without space: :Cmt3).

ib
Wow, didn't expect this, this is brilliant, thanks.
ccebby