Skip to content

Instantly share code, notes, and snippets.

@chemzqm
Created April 11, 2019 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chemzqm/477e185e220784a0031ffcc69e1878a7 to your computer and use it in GitHub Desktop.
Save chemzqm/477e185e220784a0031ffcc69e1878a7 to your computer and use it in GitHub Desktop.
system.vim
" ============================================================================
" Description: Some system commands for vim
" Author: Qiming Zhao <chemzqm@gmail.com>
" Licence: Vim licence
" Version: 0.3
" Last Modified: Jul 07, 2018
" ============================================================================
if exists('did_system_loaded') || v:version < 700
finish
endif
let did_system_loaded = 1
if !executable('rmtrash')
throw 'rmtrash is not executable'
endif
augroup vim_system
autocmd!
autocmd BufWritePre * :call s:CheckDirectory(expand('<afile>'), +expand('<abuf>'))
augroup end
" 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(...)
checktime
if a:0 ==# 0
if input('rm file ' . expand('%') . '[y/n]', 'y') =~? 'y'
let file = expand('%:p')
let buf = bufnr('%')
execute 'Bdelete ' . buf
if filereadable(file)
call s:system('rmtrash '.file)
endif
endif
else
if input('rm files ' . join(a:000, ' ') . '[y/n]', 'y') =~? 'y'
for str in a:000
if bufexists(str)
execute 'bwipeout ' . str
endif
if filereadable(str)
call s:system('rmtrash ' . str)
end
endfor
endif
endif
return v:shell_error
endfunction
function! s:Rename(name, bang)
let l:name = expand('%:p:h').'/'.a:name
let l:oldfile = expand('%:p')
let bufnr = bufnr('%')
let cwd = getcwd()
if filereadable(l:name)
if (a:bang ==# '!')
call s:system('rmtrash ' . l:name)
else
echohl WarningMsg
echomsg 'File '.a:name.' already exists (use ! to override)'
echohl None
return
endif
endif
if bufexists(l:name)
silent exe bufnr(l:name) . 'bwipe!'
endif
let save_cursor = getcurpos()
let res = rename(l:oldfile, l:name)
if res != 0
echohl Error | echon 'Rename failed!' | echohl None
return
endif
let newbuf = bufnr(l:name)
if newbuf == -1
let l:name = l:name =~# '^'.cwd ? l:name[len(cwd) + 1 :] : l:name
silent exe 'keepalt edit '.l:name
else
if bufnr('%') != newbuf
silent exe 'keepalt buffer '.newbuf
endif
endif
silent exe bufnr.'bdelete!'
call setpos('.', save_cursor)
return 1
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
endif
return v:shell_error
endfunction
function! s:CheckDirectory(file, bufnr) abort
if empty(getbufvar(a:bufnr, '&buftype')) && a:file !~# '\v^\w+\:\/'
let dir = fnamemodify(a:file, ':h')
if !isdirectory(dir)
if input('Create directory' . dir . '? [y/n]', 'y') =~? 'y'
call mkdir(dir, 'p')
endif
endif
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment