Wednesday, October 1, 2008

Vim TFS Integration

This is the second post in my series on using Vim to do C# development. You can read the introduction into why someone might want to do that here.

Where I work, we use Microsoft's Team Foundation Server Source Control. It's not very good at merging, but other than that it's a good tool.

Unlike some other source control tools, in TFS a "get" downloads the latest version of a file, a "checkout" lets you edit a file. Basically all a checkout does is make the file not readonly, but you have to do it or else TFS wont let you check the file in when you're done.

Because of this, if you're using Vim to do your work, when you first open a file and try to go into edit mode Vim will warn you that the file is readonly. This reminds you that you need to check it out. It would be very annoying if you had to switch over to the command line (or VS!) to check it out just so you could start editing. So don't do that! Just add this to your vimrc:
" setup TFS integration
function! Tfcheckout()
exe '!tf checkout "' expand('%:p') '"'
endfunction
command! Tfcheckout :call Tfcheckout()

function! Tfcheckin()
exe '!tf checkin "' expand('%:p') '"'
endfunction
command! Tfcheckin :call Tfcheckin()

The "expand" part will expand to the current file with it's full path included (Note: you have to use the execute command instead of running the ! command directly or else you'll have problems with parenthesis not being properly escaped in your file path).

Now you can simply type :Tfcheckout, and the file will be checked out. When you're all done type :Tfcheckin and the TFS checkin dialog will open, allowing you to enter a comment, checkin any other files, associate the checkin with a TFS work item, etc.

5 comments:

  1. I have added the following to your code. It requires Team Foundation Power Tools for the history command, and checking comments are truncated, but it still is handy:

    function! TFhistory()
    if bufnr("TFhistory") >0
    exe "sb TFhistory"
    else
    exe "split TFhistory"
    endif
    setlocal noswapfile
    set buftype=nofile
    setlocal modifiable
    silent! exe 'r!tfpt history "#"'
    setlocal nomodified
    normal 1G
    wincmd J
    endfunction
    command! TFhistory :call TFhistory()

    ReplyDelete
  2. Hello, as i can't acces tf by the cmd used the following vimrc:

    colorscheme slate

    function! TFcheckout()
    exe '!""C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\TF.exe" checkout "'expand('%:p')'""'
    endfunction
    command! TFcheckout :call TFcheckout()

    function! TFcheckin()
    exe '!""C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\TF.exe" checkout "'expand('%:p')'""'
    endfunction
    command! TFcheckin :call TFcheckin()

    function! TFhistory()
    if bufnr("TFhistory") >0
    exe "sb TFhistory"
    else
    exe "split TFhistory"
    endif
    setlocal noswapfile
    set buftype=nofile
    setlocal modifiable
    silent! exe 'r!""C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\TF.exe" history "#""'
    setlocal nomodified
    normal 1G
    wincmd J
    endfunction
    command! TFhistory :call TFhistory()

    ReplyDelete
  3. There's a full vim script to do this now:

    http://www.vim.org/scripts/script.php?script_id=3808

    ReplyDelete
  4. When you open a file in visual studio it checks out the file when you modify it.
    To do something along those lines I added this to my _vimrc:

    au BufReadPre * if &readonly | call Tfcheckout() | endif

    ReplyDelete
  5. This worked for me:
    nnoremap to :execute "!tf checkout " . expand('%:p')

    ReplyDelete

Note: Only a member of this blog may post a comment.