Repository: bogado/file-line Branch: main Commit: 559088afaf10 Files: 2 Total size: 3.1 KB Directory structure: gitextract_55bwf53o/ ├── README.md └── plugin/ └── file_line.vim ================================================ FILE CONTENTS ================================================ ================================================ FILE: README.md ================================================ # File-line ### Plugin for vim to enable opening a file in a given line When you open a `file:line`, for instance when coping and pasting from an error from your compiler vim tries to open a file with a colon in its name. Examples: vim index.html:20 vim app/models/user.rb:1337 With this little script in your plugins folder if the stuff after the colon is a number and a file exists with the name especified before the colon vim will open this file and take you to the line you wished in the first place. This script is licensed with GPLv3 and you can contribute to it on github at [github.com/bogado/file-line](https://github.com/bogado/file-line). ## Install details If you use `Bundle`, add this line to your `.vimrc`: Bundle 'bogado/file-line' And launch `:BundleInstall` in vim. Or just copy the file into your plugins path (`$HOME/.vim/plugin` under unixes). ================================================ FILE: plugin/file_line.vim ================================================ " Avoid installing twice or when in unsupported Vim version. if exists('g:loaded_file_line') || (v:version < 701) finish endif let g:loaded_file_line = 1 " below regexp will separate filename and line/column number " possible inputs to get to line 10 (and column 99) in code.cc are: " * code.cc(10) " * code.cc(10:99) " * code.cc:10 " * code.cc:10:99 " " closing braces/colons are ignored, so also acceptable are: " * code.cc(10 " * code.cc:10: let s:regexpressions = [ '\(.\{-1,}\)[(:]\(\d\+\)\%(:\(\d\+\):\?\)\?' ] function! s:reopenAndGotoLine(file_name, line_num, col_num) if !filereadable(a:file_name) return endif let l:bufn = bufnr("%") exec "keepalt edit " . fnameescape(a:file_name) exec a:line_num exec "normal! " . a:col_num . '|' if foldlevel(a:line_num) > 0 exec "normal! zv" endif exec "normal! zz" exec "bwipeout " l:bufn exec "filetype detect" endfunction function! s:gotoline() let file = bufname("%") " :e command calls BufRead even though the file is a new one. " As a workaround Jonas Pfenniger added an " AutoCmd BufRead, this will test if this file actually exists before " searching for a file and line to goto. if (filereadable(file) || file == '') return file endif let l:names = [] for regexp in s:regexpressions let l:names = matchlist(file, regexp) if ! empty(l:names) let file_name = l:names[1] let line_num = l:names[2] == ''? '0' : l:names[2] let col_num = l:names[3] == ''? '0' : l:names[3] call s:reopenAndGotoLine(file_name, line_num, col_num) return file_name endif endfor return file endfunction " Handle entry in the argument list. " This is called via `:argdo` when entering Vim. function! s:handle_arg() let argname = expand('%') let fname = s:gotoline() if fname != argname let argidx = argidx() exec (argidx+1).'argdelete' exec (argidx)'argadd' fnameescape(fname) endif endfunction function! s:startup() autocmd BufNewFile * nested call s:gotoline() autocmd BufRead * nested call s:gotoline() if argc() > 0 let argidx=argidx() silent call s:handle_arg() exec (argidx+1).'argument' " Manually call Syntax autocommands, ignored by `:argdo`. doautocmd Syntax doautocmd FileType endif endfunction if !isdirectory(expand("%:p")) autocmd VimEnter * call s:startup() endif