Matchit not working
Asked Answered
G

8

32

I am using Macvim 7.3 snapshot 57. I can't seem to get matchit to work in any of my files.

I press % on an opening tag. It doesn't take me to the closing tag...

My vimrc file:

" 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
Genocide answered 2/9, 2011 at 9:19 Comment(1)
github.com/tpope/vim-endwiseBlend
A
30

Since Vim comes shipped with matchit plugin, all I needed to do was activate it:

vim ~/.vimrc

Then add the following into your .vimrc:

set nocompatible
filetype plugin on
runtime macros/matchit.vim
Amah answered 1/10, 2014 at 15:37 Comment(2)
On MacVim (77) I tried all the other suggestions, this is the only one that worked (the three lines in the .vimrc). Thanks!Tunic
My understanding is that the existence of a ~/.vimrc file automatically turns off Vi compatibility. So says :help compatible, as well: "When a |vimrc| or |gvimrc| file is found while Vim is starting up, this option is switched off." Indeed, I omitted the set nocompatible line from my ~/.vimrc file and was still able to use matchit.vim by adding the following two lines.Holmes
W
22

This line

runtime macros/matchit.vim

is the standard way of activating matchit and it works on all my machines.

Does matchit work after you type

:runtime macros/matchit.vim

in normal mode ?

Waxler answered 2/9, 2011 at 11:54 Comment(5)
Stupid question: matchit is activated for html,eruby,rb,css,js,xml, do you, by any chance try to use it with some other filetype?Waxler
Try :help matchit-install, it's specifically written with plugin documentation in mind but it could help you.Waxler
Typing :runtime macros/matchit.vim in normal mode doesn't work right away which threw me off. You have to also do a :filetype detect to activate it for your open files. runtime macros/matchit.vim in vimrc does indeed work.Damascus
This is incredibly helpful, built in and completely non-obvious that it's a better (i.e., functional and maintained) solution than ruby-matchit.vimDealer
Functional, yes, but maintained? I have not updated matchit since 2008.Outwear
C
22

The page of the matchit plugin says:

Make sure you have a line like

:filetype plugin on

in your vimrc file. This enables filetype plugins, many of which tell matchit.vim which matching pairs to use.

Chimp answered 12/10, 2011 at 0:20 Comment(0)
S
9

FYI: in vim 8 runtime macros/matchit.vim becomes packadd! matchit.

Sexpartite answered 8/6, 2018 at 23:55 Comment(2)
Many thanks for letting me know. I suppose this is why this kind of thing is discouraged. I have removed the offending link. For posterity it was this: fossies.org/diffs/vim/7.4_vs_8.0/runtime/macros/…Sexpartite
And in case anyone else gets confused like me, the ! is used in your .vimrc, while if you run the command interactively you must not include it.Crumble
G
6

I started having the same issue after I updated some of my vim plugings to the latest version for 7.3.

But when I run

:MatchDebug

it fixes the issue for me.

Gecko answered 14/11, 2012 at 20:29 Comment(2)
Yup, this fixed it for me as well. Not sure what's going on.Bales
Well, :MatchDebug calls s:Match_debug(), the first line of which is let b:match_debug = 1 " Save debugging information.. (The rest of the function just defines some menus.) If you search for b:match_debug, you will see that, if it is set, then the script will not use its cached data.Outwear
H
2

I had a problem with matchit finding the correct matching brace in C++/C when there were commented braces. The following steps, taken from this forum post written by this guy, solved it for me and also pretty much explained the way the whole thing works:

  1. Create the folder ~/.vim/plugin if it is not already there:

    mkdir ~/.vim/plugin 
    
  2. Create a file with the name ~/.vim/plugin/matchit.vim :

    vi ~/.vim/plugin/matchit.vim 
    

    and the following contents:

    runtime macros/matchit.vim 
    
  3. Create the directory ~/.vim/doc if it is not already there:

    mkdir ~/.vim/doc
    
  4. Copy /usr/share/vim/vim73/macros/matchit.txt to ~/.vim/doc/ :

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

    vi
    

    and execute the following in it:

    :helptags ~/.vim/doc 
    
  6. Make sure that your ~/.vimrc includes one of the following:

    source $VIMRUNTIME/vimrc_example.vim 
    

    or

    runtime vimrc_example.vim 
    

    or

    filetype plugin on 
    

    or

    filetype plugin indent on 
    
  7. Add the following autocommand in your 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. Restart Vim.

Harrison answered 15/2, 2014 at 3:28 Comment(2)
What does let b:match_words = &matchpairs do?Trakas
Quick disclaimer: not being the original author (sources cited in my answer) this is my interpretation, which I accepted and therefore used as my solution. Now this is out of the way, it searches and matches closing bracket/parenthesis/etc patterns. You can see some examples here: github.com/isaacs/.vim/blob/master/macros/matchit.txtHarrison
G
0

I've had similar problem. I've tried runtime macros/matchit.vim with VIM-provided script and it didn't work. So I've downloaded this script in version 1.13.2 from http://www.vim.org/scripts/script.php?script_id=39, unzipped it into ~/vimfiles and it works!

Galarza answered 1/8, 2012 at 11:28 Comment(0)
C
0

For Lua you need to add this line to your init.lua:

vim.g.loaded_matchit = true

This was on Linux when I instlled with Homebrew

Canso answered 12/4 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.