这个vimscript函数有什么问题?

时间:2016-01-08 08:29:00

标签: vim

我尝试创建一个vim脚本,在调用按键时显示空格但调用函数时出错。这是代码:

scriptencoding utf-8
set encoding=utf-8

let s:showingWhiteSpaces=0
function! ShowWhiteSpace()
  if s:showingWhiteSpaces
    set nolist
    noh
    let s:showingWhiteSpaces=0
    echo "we are here1"
  else
    set list
    highlight ExtraWhitespace ctermbg=darkgreen guibg=darkgreen
    :match ExtraWhitespace /\S\zs\s\+$
    let s:showingWhiteSpaces=1
    echo "we are here2"
  endif
endfunction

_vimrc也是:

set nocompatible                " choose no compatibility with legacy vi
syntax enable
set encoding=utf-8
set showcmd                     " display incomplete commands
filetype plugin indent on       " load file type plugins + indentation

"" Whitespace
set nowrap                      " don't wrap lines
set tabstop=2 shiftwidth=2      " a tab is two spaces (or set this to 4)
set expandtab                   " use spaces, not tabs (optional)
set backspace=indent,eol,start  " backspace through everything in insert mode

"" Searching
set hlsearch                    " highlight matches
set incsearch                   " incremental searching
set ignorecase                  " searches are case insensitive...
set smartcase                   " ... unless they contain at least one capital letter

" Shortcut to rapidly toggle `set list`
set listchars=tab:▸\ ,eol:¬

nnoremap <leader>l call ShowWhiteSpace()<CR>

colors monokai

" Try the following if your GUI uses a dark background.
:highlight ExtraWhitespace ctermbg=darkgreen guibg=darkgreen

当尝试热键时,没有任何反应,当从正常模式调用该函数时:调用ShowWhiteSpace(),会发生以下错误:

E475: Invalid argument: /\S\zs\s\+$

1 个答案:

答案 0 :(得分:1)

试试这个:

match ExtraWhitespace /\S\zs\s\+$/

更新:修复映射

nnoremap <leader>l :call ShowWhiteSpace()<CR>