当Word到自动完成不被空白包围时,Vim自动完成失败

时间:2014-03-06 02:54:04

标签: vim editor vi

任何人都可以帮我调试为什么自动完成无法在这种情况下工作? (作为一个例子,我在TAB中实际选择Tab键进行自动完成):

my $variable;

while(<>) {
    if($_ =~ m/$variabTAB/i)

但在这里工作正常:

$second_var = $variabTAB

我相信这是因为第二个案例中有一个单词是自动完成的,换句话说就是用空格包围。它失败了,因为第一个例子有其他字符填充的单词。

以下是我的vimrc:

set history=700
filetype on
filetype indent on
set autoread
let mapleader = ","
let g:mapleader = ","
nmap <leader>w :w!<cr>
set so=7
set wildmenu
set wildignore=*.o,*~,*.pyc
set ruler
map <C-J> <C-W>j<C-W>_
map <C-K> <C-W>k<C-W>_
set winminheight=0
set winwidth=80
set cmdheight=2
set hid
set backspace=eol,start,indent
set whichwrap+=<,>,h,l
set ignorecase
set smartcase
set nohlsearch
set incsearch
set lazyredraw
set magic
set showmatch
set matchtime=3
set mat=2
set noerrorbells
set novisualbell
set tm=500
set joinspaces
set nolist
set scrolloff=4
set foldminlines=1
set completeopt=menu
colorscheme desert
set background=dark
if has("gui_running")
    set guioptions-=T
    set guioptions+=e
    set t_Co=256
    set guitablabel=%M\ %t
endif
set guioptions=gmrLt
set guifont=Lucida_Consoloe:h8:cANSI
set encoding=utf8
set ffs=unix,dos,mac
set directory=./_backup
set backupdir=./.backup
set nowb
set noswapfile
set expandtab
set smarttab
set softtabstop=4
set smartindent
set nonumber
set nowarn
set shiftwidth=4
set tabstop=4
set lbr
set tw=500
set ai "Auto indent
set si "Smart indent
set wrap "Wrap lines
vnoremap <silent> * :call VisualSelection('f')<CR>
vnoremap <silent> # :call VisualSelection('b')<CR>
map <space> /
map <c-space> ?
map <silent> <leader><cr> :noh<cr>
map <leader>bd :Bclose<cr>
map <leader>ba :1,1000 bd!<cr>
map <leader>tn :tabnew<cr>
map <leader>to :tabonly<cr>
map <leader>tc :tabclose<cr>
map <leader>tm :tabmove
map <leader>te :tabedit <c-r>=expand("%:p:h")<cr>/
map <leader>cd :cd %:p:h<cr>:pwd<cr>
try
  set switchbuf=useopen,usetab,newtab
  set stal=2
catch
endtry
set laststatus=2
set statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{getcwd()}%h\ \ \ Line:\ %l
map 0 ^
vnoremap <silent> gv :call VisualSelection('gv')<CR>
map <leader>g :vimgrep // **/*.<left><left><left><left><left><left><left>
map <leader><space> :vimgrep // <C-R>%<C-A><right><right><right><right><right><right><right><right><right>
vnoremap <silent> <leader>r :call VisualSelection('replace')<CR>
map <leader>cc :botright cope<cr>
map <leader>co ggVGy:tabnew<cr>:set syntax=qf<cr>pgg
map <leader>n :cn<cr>
map <leader>p :cp<cr>
map <leader>ss :setlocal spell!<cr>
map <leader>sn ]s
map <leader>sp [s
map <leader>sa zg
map <leader>s? z=
noremap <Leader>m mmHmt:%s/<C-V><cr>//ge<cr>'tzt'm
map <leader>q :e ~/buffer<cr>
map <leader>pp :setlocal paste!<cr>
function! Smart_TabComplete()
  let line = getline('.')                         " current line
  let substr = strpart(line, -1, col('.')+1)      " from the start of the current
                                                  " of the cursor
  let substr = matchstr(substr, "[^ \t]*$")       " word till cursor
  if (strlen(substr)<2)                          " nothing to match on empty string
    return "\<tab>"
  endif
  let has_period = match(substr, '\.') != -1      " position of period, if any
  let has_slash = match(substr, '\/') != -1       " position of slash, if any
  if (!has_period && !has_slash)
    return "\<C-X>\<C-P>"                         " existing text matching
  elseif ( has_slash )
    return "\<C-X>\<C-F>"                         " file matching
  else
    return "\<C-X>\<C-O>"                         " plugin matching
  endif
endfunction

inoremap <tab> <c-r>=Smart_TabComplete()<CR>

1 个答案:

答案 0 :(得分:1)

你的预感是对的。这一行来自你的完成功能

let substr = matchstr(substr, "[^ \t]*$")       " word till cursor

抓取光标前面的所有非空白字符;在你的例子中m/$variab

因为它包含斜杠,然后会触发文件完成,这自然不会考虑$variable

要解决此问题,您可以切换到仅匹配关键字字符(\k*$,默认完成考虑的内容),但这可能会对文件完成情况产生负面影响。