Last active
January 19, 2016 06:32
-
-
Save chemzqm/b798a1c51f533bbc9667 to your computer and use it in GitHub Desktop.
Some system commands for vim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" ============================================================================ | |
" Description: Some system commands for vim | |
" Author: Qiming Zhao <[email protected]> | |
" Licence: Vim licence | |
" Version: 0.2 | |
" Last Modified: January 19, 2016 | |
" ============================================================================ | |
if exists('did_system_loaded') || v:version < 700 | |
finish | |
endif | |
let did_system_loaded = 1 | |
" remove files or file of current buffer | |
command! -nargs=* -complete=file Rm call s:Remove(<f-args>) | |
command! -nargs=+ -complete=dir Mdir call s:Mkdir(<f-args>) | |
" rename current file to a new name | |
command! -nargs=1 -complete=file -bang Rename call s:Rename(<q-args>, '<bang>') | |
" copy content to system clipboard, used for ternimal vim | |
command! -nargs=0 -range=% Copy :call s:Copy(<line1>, <line2>) | |
function! s:Copy(l1, l2) | |
let lines = getline(a:l1, a:l2) | |
let stdin = join(lines, "\n") . "\n" | |
call s:system('pbcopy', stdin) | |
endfunction | |
function! s:Mkdir(...) | |
for str in a:000 | |
call mkdir(str, 'p') | |
endfor | |
endfunction | |
function! s:Remove(...) | |
if a:0 ==# 0 | |
if input('rm file ' . expand('%') . '[y/n]') =~? 'y' | |
let file = expand('%:p') | |
let buf = bufnr('%') | |
execute 'Bdelete ' . buf | |
call s:system('rm -f '.file) | |
endif | |
else | |
if input('rm files ' . join(a:000, ' ') . '[y/n]') =~? 'y' | |
for str in a:000 | |
if bufnr(str) != -1 | |
execute 'bwipeout ' . str | |
endif | |
call s:system('rm -rf ' . str) | |
endfor | |
endif | |
endif | |
endfunction | |
function! s:Rename(name, bang) | |
let l:name = a:name | |
let l:oldfile = expand('%:p') | |
if bufexists(fnamemodify(l:name, ':p')) | |
if (a:bang ==# '!') | |
silent exe bufnr(fnamemodify(l:name, ':p')) . 'bwipe!' | |
else | |
echohl ErrorMsg | |
echomsg 'A buffer with that name already exists (use ! to override).' | |
echohl None | |
return 0 | |
endif | |
endif | |
let l:status = 1 | |
let v:errmsg = '' | |
silent! exe 'saveas' . a:bang . ' ' . l:name | |
if v:errmsg =~# '^$\|^E329' | |
let l:lastbufnr = bufnr('$') | |
if expand('%:p') !=# l:oldfile && filewritable(expand('%:p')) | |
if fnamemodify(bufname(l:lastbufnr), ':p') ==# l:oldfile | |
silent exe l:lastbufnr . 'bwipe!' | |
else | |
echohl ErrorMsg | |
echomsg 'Could not wipe out the old buffer for some reason.' | |
echohl None | |
let l:status = 0 | |
endif | |
if delete(l:oldfile) != 0 | |
echohl ErrorMsg | |
echomsg 'Could not delete the old file: ' . l:oldfile | |
echohl None | |
let l:status = 0 | |
endif | |
else | |
echohl ErrorMsg | |
echomsg 'Rename failed for some reason.' | |
echohl None | |
let l:status = 0 | |
endif | |
else | |
echoerr v:errmsg | |
let l:status = 0 | |
endif | |
return l:status | |
endfunction | |
function! s:system(cmd, ...) | |
if a:0 == 1 | |
let output = system(a:cmd, a:1) | |
else | |
let output = system(a:cmd) | |
endif | |
if v:shell_error && output !=# "" | |
echohl Error | echon output | echohl None | |
else | |
echo 'done' | |
endif | |
return !v:shell_error | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment