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
Christian Fazzini
  • 19,613
  • 21
  • 110
  • 215

7 Answers7

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
Kingsley Ijomah
  • 3,273
  • 33
  • 25
  • 1
    On MacVim (77) I tried all the other suggestions, this is the only one that worked (the three lines in the `.vimrc`). Thanks! – Ivan Maeder Jul 09 '15 at 08:50
  • 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. – M12 Jan 17 '17 at 06:11
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 ?

romainl
  • 186,200
  • 21
  • 280
  • 313
  • 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? – romainl Sep 05 '11 at 16:03
  • 7
    Try `:help matchit-install`, it's specifically written with plugin documentation in mind but it could help you. – romainl Sep 05 '11 at 16:06
  • 10
    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. – Christopher Camps Oct 23 '12 at 21:56
  • 1
    This is incredibly helpful, built in and completely non-obvious that it's a better (i.e., functional and maintained) solution than ruby-matchit.vim – Dav Clark Jan 22 '13 at 00:33
  • 1
    Functional, yes, but maintained? I have not updated matchit since 2008. – benjifisher Jan 09 '14 at 02:50
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.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
speedbacon
  • 221
  • 1
  • 3
8

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

Jacob Degeling
  • 358
  • 3
  • 12
  • 1
    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: https://fossies.org/diffs/vim/7.4_vs_8.0/runtime/macros/matchit.txt-diff.html – Jacob Degeling Jun 10 '21 at 11:06
  • 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. – jwd Dec 23 '21 at 17:38
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.

dkinzer
  • 32,179
  • 12
  • 66
  • 85
  • Yup, this fixed it for me as well. Not sure what's going on. – bhilburn Jul 13 '13 at 20:57
  • 2
    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. – benjifisher Jan 09 '14 at 02:58
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.

George
  • 774
  • 2
  • 9
  • 18
  • What does `let b:match_words = &matchpairs ` do? – trusktr Apr 12 '18 at 01:00
  • 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: https://github.com/isaacs/.vim/blob/master/macros/matchit.txt – George Apr 15 '18 at 15:25
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!

Grzegorz Grzybek
  • 6,152
  • 3
  • 29
  • 42