Skip to content

Instantly share code, notes, and snippets.

@mattn
Created November 20, 2013 11:04
Show Gist options
  • Save mattn/7561411 to your computer and use it in GitHub Desktop.
Save mattn/7561411 to your computer and use it in GitHub Desktop.
let s:prompt = hostname()."#"
let s:history = ''
let s:push_dir = ''
function! s:ShowPrompt()
exec "normal I".s:prompt." "
normal $
endfunction
function! s:DoLeft()
if col('.') > strlen(s:prompt)+1
exec "normal! \<Left>"
endif
endfunction
function! s:DoUp()
if col('.') > strlen(s:prompt)+1
exec "normal! \<Left>"
endif
endfunction
function! s:DoTab()
"setlocal iminsert=0
let lin = getline('.')
let cnt = strlen(s:prompt)+1
let esc = 0
let inp = ""
while cnt < strlen(lin)
if lin[cnt] == "\\"
let cnt = cnt + 1
let inp = inp.lin[cnt]
let esc = esc + 1
elseif lin[cnt] == ' ' || lin[cnt] == '"'
let inp = ''
let esc = 0
else
let inp = inp.lin[cnt]
endif
let cnt = cnt + 1
endwhile
let cmp = escape(expand(inp."*"), ' ')
if cmp != ""
if cmp =~ ".*\n.*"
exec "normal o".cmp
exec "normal o".lin
else
call setline(line('.'), strpart(lin, 0, strlen(lin)-strlen(inp)-esc))
exec "normal a".cmp
endif
endif
endfunction
function! s:DoCR()
let v:errmsg = ''
let cmd = strpart(getline('.'), strlen(s:prompt)+1)
if cmd =~ "^\s*history\s*$"
let cmd = substitute(s:history, "\n\n", "", 'g')
if cmd != ''
exec "normal o" . cmd
endif
let cmd = ''
else
let s:history = substitute(s:history, '[^\n]+\n\(.*\)', '\1', '')."\n".cmd
endif
if cmd =~ "^\s*exit\s*$"
exec "normal :bw!\<CR>\<ESC>"
return
elseif cmd =~ "^\s*pwd\s*$"
exec "normal o".getcwd()
elseif cmd =~ "^\s*cd\s*.*$"
let cmd = substitute(cmd, '^\s*cd\s*\(.*\)$', '\1', '')
if cmd == ""
let cmd = $HOME
endif
exec "silent! lcd ".cmd
elseif cmd =~ "^\s*pushd\s*.*$"
let s:push_dir = substitute(cmd, '^\s*pushd\s*\(.*\)$', '\1', '')
if globpath(getcwd(), s:push_dir) != ""
let s:push_dir = getcwd().s:push_dir
endif
let s:push_dir = expand(s:push_dir)
elseif cmd =~ "^\s*popd\s*.*$"
if s:push_dir != ""
exec "lcd ".s:push_dir
endif
let s:push_dir = ''
elseif cmd != ''
let cmd = substitute(cmd, '\\', '', 'g')
exec "silent r! ".cmd
endif
if v:errmsg != ''
exec "normal o". v:errmsg
endif
if v:errmsg != ''
endif
normal o
call s:ShowPrompt()
endfunction
function! s:DoBS()
if strlen(getline('.')) > strlen(s:prompt)+1
exec "normal! \<DEL>"
endif
endfunction
function! s:VShell()
new VimSHELL
call s:ShowPrompt()
startinsert!
if s:history == ''
let cnt = 1
while cnt < 50
let s:history = s:history."\n"
let cnt = cnt + 1
endwhile
endif
imap <silent> <buffer> <CR> <C-O>:call <SID>DoCR()<CR>
imap <silent> <buffer> <S-CR> <C-O>:call <SID>DoCR()<CR>
imap <silent> <buffer> <BS> <C-O>:call <SID>DoBS()<CR>
imap <silent> <buffer> <Left> <C-O>:call <SID>DoLeft()<CR>
imap <silent> <buffer> <Tab> <C-O>:call <SID>DoTab()<CR><Right>
imap <silent> <buffer> <C-L> <C-O>:redraw<CR>
"imap <silent> <buffer> <Esc> <Nop>
imap <silent> <buffer> <C-W> <Nop>
"imap <silent> <buffer> <Right> <C-O>:call <SID>DoRight()<CR>
"imap <silent> <buffer> <Up> <C-O>:call <SID>DoUp()<CR>
"imap <silent> <buffer> <Down> <C-O>:call <SID>DoDown()<CR>
endfunction
command! VSHELL :call <SID>VShell()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment