原文见:http://www.vim.org/tips/tip.php?tip_id=1119
created: | February 3, 2006 20:51 | complexity: | intermediate | |||
author: | Matt Zyzik | as of Vim: | 6.0 |
I personally use Vim for all text editing, even software development. At one point I stopped using IDEs. One major reason is that Vim can do all the major things I need from IDEs (tabs, file trees, greping, syntax highlighting, indentation, completion, "quickfix"ing, etc.). Vim 7 will be introducing one of the most critical features: completion (intellisense).
Still Vim needs plugins to do some IDE-like things that aren't built in.
One thing you might want is the vtreeexplorer (http://vim.org/scripts/script.php?script_id=184) plugin, which allows you to have a "file list" in a vertically split window. Another thing worth considering is the minibufexpl (http://vim.org/scripts/script.php?script_id=159) plugin, which will allow for tabs of all your files (works really well).
Yet another thing is in a former tip of mine (http://www.vim.org/tips/tip.php?tip_id=1078), which allows you to delete a buffer without actually closing the window. Also, below, I have a new, more complicated version of it. The below script will actually create a scratch buffer if there are no listed buffers left. The script, in addition, takes care of a small annoyance. Before, if you 1) open vim, 2) :e a file, 3) :bd, 4) :e the same file... then there will be two buffers listed (that file and a [no name] buffer). The following script ensures this doesn't happen.
(Everything in this tutorial and in most of my other tips/tutorials assumes the user does "set hidden".)
Using this Kwbd function (:call Kwbd(1)) will make Vim behave like an IDE; or maybe even better.
"
here is a more exotic version of my original Kwbd script
"
delete the buffer; keep windows; create a scratch buffer if no buffers left
function Kwbd(kwbdStage)
if
(a:kwbdStage
==
1
)
if
(!buflisted(winbufnr(0)))
bd!
return
endif
let g:kwbdBufNum
=
bufnr(
"
%
"
)
let g:kwbdWinNum
=
winnr()
windo call Kwbd(
2
)
execute
"
normal
"
. g:kwbdWinNum .
""
let g:buflistedLeft
=
0
let g:bufFinalJump
=
0
let l:nBufs
=
bufnr(
"
$
"
)
let l:i
=
1
while
(l:i
<=
l:nBufs)
if
(l:i
!=
g:kwbdBufNum)
if
(buflisted(l:i))
let g:buflistedLeft
=
g:buflistedLeft
+
1
else
if
(bufexists(l:i)
&&
!strlen(bufname(l:i))
&&
!g:bufFinalJump)
let g:bufFinalJump
=
l:i
endif
endif
endif
let l:i
=
l:i
+
1
endwhile
if
(!g:buflistedLeft)
if
(g:bufFinalJump)
windo
if
(buflisted(winbufnr(0)))
|
execute
"
b!
"
. g:bufFinalJump
|
endif
else
enew
let l:newBuf
=
bufnr(
"
%
"
)
windo
if
(buflisted(winbufnr(0)))
|
execute
"
b!
"
. l:newBuf
|
endif
endif
execute
"
normal
"
. g:kwbdWinNum .
""
endif
if
(buflisted(g:kwbdBufNum)
||
g:kwbdBufNum
==
bufnr(
"
%
"
))
execute
"
bd!
"
. g:kwbdBufNum
endif
if
(!g:buflistedLeft)
set buflisted
set bufhidden
=
delete
set buftype
=
nofile
setlocal noswapfile
normal athis
is
the scratch buffer
endif
else
if
(bufnr(
"
%
"
)
==
g:kwbdBufNum)
let prevbufvar
=
bufnr(
"
#
"
)
if
(prevbufvar
>
0
&&
buflisted(prevbufvar)
&&
prevbufvar
!=
g:kwbdBufNum)
b
#
else
bn
endif
endif
endif
endfunction