Vim 使用 Vexplore 和終端自動啟動

Vim 使用 Vexplore 和終端自動啟動

我想將 vim 設定為在大多數情況下表現得像傳統的 ide。我想做的是,當我輸入命令 vim 時,它會以這種方式打開:

Vim 視窗

問題是它是這樣打開的:

vim 演示

我希望終端自動啟動,但在上一個視窗中。我嘗試閱讀文檔,但我不知道如何閱讀文檔並將其放入程式碼中,我的 vimrc 中的大部分程式碼都是複製貼上的,還有一些我已經知道的基礎知識。第一張圖片一樣自動啟動。

另外,還有一個問題我不確定在 vim 中是否可能,有時在 NetRW 中,當我單擊緩衝區時,緩衝區會擦除目錄樹,然後會出現一個空白緩衝區。是否有辦法停用某些命令幹擾終端機緩衝區和目錄樹列表?一個例子是,如果我嘗試打開一個文件,並且最後一個緩衝區是終端緩衝區,它會問我是否要保存當前緩衝區(終端)並打開該文件,我希望它能夠更聰明地在非終端緩衝區而不是終端。除了輸入命令之外,基本上完全忽略終端。

這是我的 vimrc,可能很混亂,因為我除了複製和貼上之外什麼也沒做:

set mouse=a
set number
imap jj <Esc>
set autochdir
map <Tab> <C-W>W:cd %:p:h<CR>:<CR>
map <C-l> :q!<CR>
map <C-t> :term bash<CR>
" Specify a directory for plugins
" - For Neovim: ~/.local/share/nvim/plugged
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.vim/plugged')

" Make sure you use single quotes

" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
Plug 'junegunn/vim-easy-align'

" Any valid git URL is allowed
Plug 'https://github.com/junegunn/vim-github-dashboard.git'

" Multiple Plug commands can be written in a single line using | separators
Plug 'SirVer/ultisnips' | Plug 'honza/vim-snippets'

" On-demand loading
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }

" Using a non-master branch
Plug 'rdnetto/YCM-Generator', { 'branch': 'stable' }

" Using a tagged release; wildcard allowed (requires git 1.9.2 or above)
Plug 'fatih/vim-go', { 'tag': '*' }

" Plugin options
Plug 'nsf/gocode', { 'tag': 'v.20150303', 'rtp': 'vim' }

" Plugin outside ~/.vim/plugged with post-update hook
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }

" Unmanaged plugin (manually installed and updated)
Plug '~/my-prototype-plugin'

" Initialize plugin system
call plug#end()


function! NetrwOpenMultiTab(current_line,...) range
   " Get the number of lines.
   let n_lines =  a:lastline - a:firstline + 1

   " This is the command to be built up.
   let command = "normal "

   " Iterator.
   let i = 1

   " Virtually iterate over each line and build the command.
   while i < n_lines
      let command .= "tgT:" . ( a:firstline + i ) . "\<CR>:+tabmove\<CR>"
      let i += 1
   endwhile
   let command .= "tgT"

   " Restore the Explore tab position.
   if i != 1
      let command .= ":tabmove -" . ( n_lines - 1 ) . "\<CR>"
   endif

   " Restore the previous cursor line.
   let command .= ":" . a:current_line  . "\<CR>"

   " Check function arguments
   if a:0 > 0
      if a:1 > 0 && a:1 <= n_lines
         " The current tab is for the nth file.
         let command .= ( tabpagenr() + a:1 ) . "gt"
      else
         " The current tab is for the last selected file.
         let command .= (tabpagenr() + n_lines) . "gt"
      endif
   endif
   " The current tab is for the Explore tab by default.

   " Execute the custom command.
   execute command
endfunction

" Define mappings.
augroup NetrwOpenMultiTabGroup
   autocmd!
   autocmd Filetype netrw vnoremap <buffer> <silent> <expr> t ":call NetrwOpenMultiTab(" . line(".") . "," . "v:count)\<CR>"
   autocmd Filetype netrw vnoremap <buffer> <silent> <expr> T ":call NetrwOpenMultiTab(" . line(".") . "," . (( v:count == 0) ? '' : v:count) . ")\<CR>"
augroup END

" Toggle Vexplore with Ctrl-E
function! ToggleVExplorer()
  if exists("t:expl_buf_num")
      let expl_win_num = bufwinnr(t:expl_buf_num)
      if expl_win_num != -1
          let cur_win_nr = winnr()
          exec expl_win_num . 'wincmd w'
          close
          exec cur_win_nr . 'wincmd w'
          unlet t:expl_buf_num
      else
          unlet t:expl_buf_num
      endif
  else
      exec '1wincmd w'
      Vexplore
      let t:expl_buf_num = bufnr("%")
  endif
endfunction

" NeTRW Explorer Settings

let g:netrw_banner = 0
let g:netrw_liststyle = 3
let g:netrw_browse_split = 4
let g:netrw_altv = 1
let g:netrw_winsize = 15
augroup ProjectDrawer
  autocmd!
  autocmd VimEnter * :Vexplore
augroup END
map <silent> <C-n> :Vexplore<CR>

" Per default, netrw leaves unmodified buffers open. This autocommand
" deletes netrw's buffer once it's hidden (using ':q', for example)
autocmd FileType netrw setl bufhidden=delete
autocmd TabNew * call feedkeys(":Vexplore\<CR>", 'n')

" terminal split below
set splitbelow
autocmd VimEnter * :term bash

答案1

請改用此自動命令:

autocmd VimEnter *
    \ Vexplore |
    \ execute "wincmd l" |
    \ rightbelow term bash

當您進入 Vim 時,這將執行整個序列一次。我使用續行符(後續行以反斜線開頭)和欄來運行多個命令。另請注意,autocmd執行 Ex 命令,因此前面的命令:並不是真正必要的。

第一個命令將如預期開啟左側的 NERDTree 視窗。

其次,wincmd l將移動到右側的視窗(參見:help :wincmd)。這是您的嘗試中缺少的步驟,這導致下一步分割 NERDTree 而不是主視窗。需要wincmd在 內部運行execute,否則它將嘗試解釋|其後面的 ,這將停止作為分隔符號工作。

最後,最後一個命令打開終端,使用明確rightbelow(請參閱 參考資料:help :rightbelow)打開底部的分割。就是這樣!

相關內容