├── README.md └── plugin └── file_line.vim /README.md: -------------------------------------------------------------------------------- 1 | # File-line 2 | 3 | `file-line` is a plugin for Vim that enables opening a file in a given line. 4 | 5 | This is a personal fork of 6 | [github.com/bogado/file-line](https://github.com/bogado/file-line). The fork 7 | was created to address [this](https://github.com/bogado/file-line/issues/52) 8 | issue, but since the code was so short I decided to rather rewrite it and do 9 | some simplifications. 10 | 11 | ## Installation 12 | 13 | If you use [vim-plug](https://github.com/junegunn/vim-plug), then add the 14 | following line to your `vimrc` file: 15 | 16 | ```vim 17 | Plug 'lervag/file-line' 18 | ``` 19 | 20 | Or use some other plugin manager: 21 | - [vundle](https://github.com/gmarik/vundle) 22 | - [neobundle](https://github.com/Shougo/neobundle.vim) 23 | - [pathogen](https://github.com/tpope/vim-pathogen) 24 | 25 | ## Usage 26 | 27 | When you open a `file:line`, for instance when coping and pasting from an error 28 | from your compiler vim tries to open a file with a colon in its name. 29 | 30 | Examples: 31 | 32 | vim index.html:20 33 | vim app/models/user.rb:1337 34 | 35 | With this little script in your plugins folder if the stuff after the colon is 36 | a number and a file exists with the name specified before the colon vim will 37 | open this file and take you to the line you wished in the first place. 38 | 39 | ## Configuration 40 | 41 | ```vim 42 | " Specify fallback if column is not specified 43 | " * True: Go to first column in line (like normal |) 44 | " * False: Go to first nonblank column (like normal ^) 45 | let g:file_line_fallback_column0 = v:true 46 | 47 | " Disable flashing crosshairs on the cursor line/column 48 | let g:file_line_crosshairs = v:true 49 | 50 | " Customize crosshairs behaviour 51 | let g:file_line_crosshairs_number = 2 52 | let g:file_line_crosshairs_duration = 200 53 | ``` 54 | 55 | ## License 56 | 57 | This script is licensed with GPLv3. 58 | 59 | -------------------------------------------------------------------------------- /plugin/file_line.vim: -------------------------------------------------------------------------------- 1 | if exists('g:loaded_file_line') | finish | endif 2 | let g:loaded_file_line = v:true 3 | 4 | let g:file_line_crosshairs = get(g:, 'file_line_crosshairs', v:true) 5 | let g:file_line_crosshairs_number = get(g:, 'file_line_crosshairs_number', 2) 6 | let g:file_line_crosshairs_duration = get(g:, 'file_line_crosshairs_duration', 200) 7 | let g:file_line_fallback_column0 = get(g:, 'file_line_fallback_column0', v:true) 8 | 9 | augroup file_line 10 | autocmd! 11 | autocmd! BufNewFile,BufRead * nested call s:goto_file_line() 12 | augroup END 13 | 14 | function! s:goto_file_line() 15 | let buffer_name = bufname('%') 16 | if filereadable(buffer_name) || empty(buffer_name) 17 | return 18 | endif 19 | 20 | " Regex to match variants like these: 21 | " * file(10) 22 | " * file(line:col) 23 | " * file:line:column: 24 | " * file:line:column 25 | " * file:line 26 | let matches = matchlist(buffer_name, 27 | \ '\(.\{-1,}\)[(:]\(\d\+\)\%(:\(\d\+\):\?\)\?') 28 | if empty(matches) | return | endif 29 | 30 | let filename = matches[1] 31 | let line = !empty(matches[2]) ? matches[2] : '0' 32 | let column = !empty(matches[3]) 33 | \ ? matches[3] . '|' 34 | \ : (g:file_line_fallback_column0 ? '|' : '^') 35 | 36 | if filereadable(filename) 37 | let bufnr = bufnr('%') 38 | execute 'keepalt edit' fnameescape(filename) 39 | execute 'bdelete' bufnr 40 | 41 | execute line 42 | execute 'normal!' column 43 | normal! m" 44 | normal! zv 45 | normal! zz 46 | if g:file_line_crosshairs 47 | call s:crosshair_flash( 48 | \ g:file_line_crosshairs_number, g:file_line_crosshairs_duration) 49 | endif 50 | endif 51 | endfunction 52 | 53 | " Flash crosshairs (reticle) on current cursor line/column to highlight it. 54 | " Particularly useful when the cursor is at head/tail end of file, 55 | " in which case it will not get centered. 56 | " Ref1: https://vi.stackexchange.com/a/3481/29697 57 | " Ref2: https://stackoverflow.com/a/33775128/38281 58 | function! s:crosshair_flash(number_of_flashes, flash_duration) abort 59 | let cul = &cursorline 60 | let cuc = &cursorcolumn 61 | 62 | for i in range(1, a:number_of_flashes) 63 | set cursorline cursorcolumn 64 | redraw 65 | execute 'sleep' a:flash_duration . 'm' 66 | set nocursorline nocursorcolumn 67 | redraw 68 | execute 'sleep' a:flash_duration . 'm' 69 | endfor 70 | 71 | let &cursorline = cul 72 | let &cursorcolumn = cuc 73 | endfunction 74 | --------------------------------------------------------------------------------