diff --git a/vimrc b/vimrc index 3e143ac7..acb2a8d3 100644 --- a/vimrc +++ b/vimrc @@ -36,6 +36,7 @@ map rt :!ctags --extra=+f -R * " make and python use real tabs autocmd FileType make set noexpandtab autocmd FileType python set noexpandtab +autocmd BufRead,BufNewFile {Gemfile,Rakefile,Thorfile} set ft=ruby " allow backspacing over everything in insert mode set backspace=indent,eol,start @@ -58,9 +59,7 @@ cmap =expand("%:p:h") . "/" autocmd VimEnter * NERDTree autocmd VimEnter * wincmd p -function ChangeDirectory(dir) - execute "cd " . a:dir - +function s:UpdateNERDTree() if exists("t:NERDTreeBufName") if bufwinnr(t:NERDTreeBufName) != -1 NERDTree @@ -69,5 +68,54 @@ function ChangeDirectory(dir) endif endfunction -command -nargs=1 -complete=file ChangeDir :call ChangeDirectory() -cabbrev cd =(getcmdtype()==':' && getcmdpos()==1 ? 'ChangeDir' : 'cd') +function s:CommandCabbr(abbreviation, expansion) + execute 'cabbrev ' . a:abbreviation . ' =getcmdpos() == 1 && getcmdtype() == ":" ? "' . a:expansion . '" : "' . a:abbreviation . '"' +endfunction + +function s:FileCommand(name, ...) + if exists("a:1") + let funcname = a:1 + else + let funcname = a:name + endif + + execute 'command -nargs=1 -complete=file ' . a:name . ' :call ' . funcname . '()' +endfunction + +function s:DefineCommand(name, destination) + call s:FileCommand(a:destination) + call s:CommandCabbr(a:name, a:destination) +endfunction + +function ChangeDirectory(dir) + execute "cd " . a:dir + call s:UpdateNERDTree() +endfunction + +function Touch(file) + execute "!touch " . a:file + call s:UpdateNERDTree() +endfunction + +function Remove(file) + let current_path = expand("%") + let removed_path = fnamemodify(a:file, ":p") + + echo current_path . ", " . removed_path + echo getbufvar("%", "&modified") + if (current_path == removed_path) && (getbufvar("%", "&modified")) + echo "You are trying to remove the file you are editing. Please close the buffer first." + else + execute "!rm " . a:file + endif +endfunction + +function Edit(file) + execute "e " . a:file + call ChangeDirectory(system("dirname " . a:file)) +endfunction + +call s:DefineCommand("cd", "ChangeDirectory") +call s:DefineCommand("touch", "Touch") +call s:DefineCommand("rm", "Remove") +call s:DefineCommand("e", "Edit")