【问题标题】:Matchit not workingMatchit 不工作
【发布时间】:2021-06-10 19:06:59
【问题描述】:

我正在使用 Macvim 7.3 快照 57。我似乎无法让 matchit 在我的任何文件中工作。

我在开始标签上按 %。它不会带我到结束标签...

我的 vimrc 文件:

" Pathogen settings
call pathogen#runtime_append_all_bundles()
call pathogen#helptags()

set nocompatible

set number
set ruler
set cursorline
syntax on

" Disable all blinking
set guicursor+=a:blinkon0

" Whitespace stuff
set nowrap
set tabstop=2
set shiftwidth=2
set expandtab
set cindent
set smartindent
set autoindent
set list listchars=tab:\ \ ,trail:·

" Searching
set hlsearch
set incsearch
set ignorecase
set smartcase

" Status bar
set laststatus=2

" Start without the toolbar
set guioptions-=T

" Default gui color scheme
" "color default
" color molokai
color railscasts+

" Command-/ to toggle comments
map <D-/> :TComment<CR>j

" Remember last location in file
if has("autocmd")
  au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")
    \| exe "normal g'\"" | endif
endif

" Thorfile, Rakefile and Gemfile are Ruby
au BufRead,BufNewFile {Gemfile,Rakefile,Thorfile,config.ru} set ft=ruby

" Open split buffers below instead of above current buffer
set splitbelow

" Session options
let g:session_autoload = 1
let g:session_autosave = 1

" Buffer navigation
map <C-K> <C-W><C-K>
map <C-J> <C-W><C-W>
map <C-H> <C-W><C-H>
map <C-L> <C-W><C-L>

" Rails navigation options
nmap <leader>rc :Rcontroller 
nmap <leader>rv :Rview 
nmap <leader>rm :Rmodel 

" Tab completion
" Also needed for better Rails navigation auto-completion
set wildmode=list:longest,list:full

" Open up side panel left (NERDTree) and right(Tagbar)
" nmap <leader>\ :NERDTreeToggle<CR> :TagbarToggle<CR>
nmap <leader>\ :call ToggleNERDTreeAndTagbar()<CR>

" Allow single click for NERDTree
let NERDTreeMouseMode = 3
let g:NERDTreeWinSize = 30
" autocmd VimEnter * NERDTree

" Tagbar options
let tagbar_singleclick = 1
let g:tagbar_sort = 0
let g:tagbar_width = 30
" autocmd VimEnter * nested TagbarOpen

" The Janus plugin sets this to noequalalways for the Zoominfo plugin
" However, we want to set this to equalalways instead, since we want to
" have equal window height when a new window is opened. i.e. via ctrl+w+s
set equalalways

" Matchit already installed in newer versions of vim.
" Don't need to add this onto pathogen bundle folder. We only need
" to configure it.
" Configure matchit so that it goes from opening tag to closing tag
au FileType html,eruby,rb,css,js,xml runtime! macros/matchit.vim

" Set backup and swp dir. Don't forget to clear tmp dir out once in a while
set backupdir=~/.vim/tmp/backup
set directory=~/.vim/tmp/swp

" Detect if a tab was closed, and ensure that height of main window fills the screen (100% height)
au TabEnter * let &lines = 100

" <leader>\ to open or close NERDTree and Tagbar, under the following conditions:
" 1) Only close both if NERDTree and Tagbar are both opened
" 2) Open both if NERDTree and Tagbar are closed OR if one is already opened
function! ToggleNERDTreeAndTagbar()
  let w:jumpbacktohere = 1

  " Detect which plugins are open
  if exists('t:NERDTreeBufName')
      let nerdtree_open = bufwinnr(t:NERDTreeBufName) != -1
  else
      let nerdtree_open = 0
  endif
  let tagbar_open = bufwinnr('__Tagbar__') != -1

  " Perform the appropriate action
  if nerdtree_open && tagbar_open
      NERDTreeClose
      TagbarClose
  elseif nerdtree_open
      TagbarOpen
  elseif tagbar_open
      NERDTree
  else
      NERDTree
      TagbarOpen
  endif

  " Jump back to the original window
  for window in range(1, winnr('$'))
    execute window . 'wincmd w'
    if exists('w:jumpbacktohere')
      unlet w:jumpbacktohere
      break
    endif
  endfor
endfunction

【问题讨论】:

标签: vim macvim


【解决方案1】:

由于 Vim 附带了 matchit 插件,我需要做的就是激活它:

vim ~/.vimrc

然后将以下内容添加到您的 .vimrc 中:

set nocompatible
filetype plugin on
runtime macros/matchit.vim

【讨论】:

  • 在 MacVim (77) 上我尝试了所有其他建议,这是唯一有效的建议(.vimrc 中的三行)。谢谢!
  • 我的理解是~/.vimrc文件的存在会自动关闭Vi兼容性。 :help compatible 也说:“当 Vim 启动时发现 |vimrc| 或 |gvimrc| 文件时,此选项被关闭。”事实上,我从~/.vimrc 文件中省略了set nocompatible 行,并且通过添加以下两行仍然能够使用matchit.vim
【解决方案2】:

这一行

runtime macros/matchit.vim

是激活 matchit 的标准方式,它适用于我所有的机器。

matchit 输入后是否有效

:runtime macros/matchit.vim

在正常模式下?

【讨论】:

  • 愚蠢的问题:matchit 是为html,eruby,rb,css,js,xml 激活的,你有没有机会尝试将它与其他文件类型一起使用?
  • 试试:help matchit-install,它是专门为插件文档编写的,但它可以帮助你。
  • 在正常模式下输入:runtime macros/matchit.vim 并不能立即起作用,这让我很失望。您还必须执行:filetype detect 才能为您打开的文件激活它。 vimrc 中的runtime macros/matchit.vim 确实有效。
  • 这非常有用,内置并且完全不明显它是比 ruby​​-matchit.vim 更好(即功能和维护)的解决方案
  • 功能,是的,但维护?自 2008 年以来我没有更新 matchit。
【解决方案3】:

The page of the matchit plugin 说:

确保你有一个像

这样的行
:filetype plugin on

在您的vimrc 文件中。这启用了文件类型插件,其中许多告诉 matchit.vim 要使用哪些匹配对。

【讨论】:

    【解决方案4】:

    仅供参考:在 vim 8 中,runtime macros/matchit.vim 变为 packadd! matchit

    【讨论】:

    • 非常感谢您告诉我。我想这就是不鼓励这种事情的原因。我已经删除了有问题的链接。对于后代,它是这样的:fossies.org/diffs/vim/7.4_vs_8.0/runtime/macros/…
    • 如果其他人像我一样感到困惑,! 会在您的 .vimrc 中使用,而如果您以交互方式运行该命令,则必须包含它。
    【解决方案5】:

    在我将一些 vim 插件更新到 7.3 的最新版本后,我开始遇到同样的问题。

    但是当我跑步时

    :MatchDebug

    它为我解决了这个问题。

    【讨论】:

    • 是的,这也为我解决了问题。不知道发生了什么。
    • 嗯,:MatchDebug 调用s:Match_debug(),第一行是let b:match_debug = 1 " Save debugging information.。 (函数的其余部分只是定义了一些菜单。)如果你搜索b:match_debug,你会看到,如果设置了,那么脚本将不会使用它的缓存数据。
    【解决方案6】:

    当有注释的大括号时,我遇到了 matchit 在 C++/C 中找到正确匹配大括号的问题。以下步骤取自 this guy 编写的 this forum post,为我解决了这个问题,并且几乎解释了整个事情的工作方式:

    1. 如果文件夹 ~/.vim/plugin 不存在,则创建它:

      mkdir ~/.vim/plugin 
      
    2. 创建一个文件名 ~/.vim/plugin/matchit.vim :

      vi ~/.vim/plugin/matchit.vim 
      

      及以下内容:

      runtime macros/matchit.vim 
      
    3. 如果目录 ~/.vim/doc 不存在,则创建它:

      mkdir ~/.vim/doc
      
    4. 复制 /usr/share/vim/vim73/macros/matchit.txt 到 ~/.vim/doc/ :

      cp /usr/share/vim/vim73/macros/matchit.txt ~/.vim/doc/
      
    5. 打开vi

      vi
      

      并在其中执行以下操作:

      :helptags ~/.vim/doc 
      
    6. 确保您的 ~/.vimrc 包含以下内容之一:

      source $VIMRUNTIME/vimrc_example.vim 
      

      runtime vimrc_example.vim 
      

      filetype plugin on 
      

      filetype plugin indent on 
      
    7. 在你的 vimrc 中添加以下自动命令:

      " make matchit work on C-like filetypes 
      " c and cpp are already handled by their ftplugin 
      au Filetype css,javascript 
              \ let b:match_words = &matchpairs 
      
    8. 重启 Vim。

    【讨论】:

    • let b:match_words = &amp;matchpairs 是做什么的?
    • 快速免责声明:不是原作者(我的回答中引用的来源)这是我的解释,我接受并因此用作我的解决方案。现在这不碍事了,它搜索并匹配右括号/括号/等模式。你可以在这里看到一些例子:github.com/isaacs/.vim/blob/master/macros/matchit.txt
    【解决方案7】:

    我也遇到过类似的问题。我用 VIM 提供的脚本尝试了runtime macros/matchit.vim,但没有成功。 所以我从http://www.vim.org/scripts/script.php?script_id=39下载了这个1.13.2版本的脚本,解压到~/vimfiles中就可以了!

    【讨论】:

      猜你喜欢
      • 2020-05-28
      • 2015-06-22
      • 2019-01-12
      • 1970-01-01
      • 2021-02-08
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 2021-08-27
      相关资源
      最近更新 更多