Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Examples:

vim index.html:20
vim app/models/user.rb:1337
vim lib/some.pm#L69

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
Expand Down
30 changes: 25 additions & 5 deletions plugin/file_line.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ let g:loaded_file_line = 1
" closing braces/colons are ignored, so also acceptable are:
" * code.cc(10
" * code.cc:10:
let s:regexpressions = [ '\(.\{-1,}\)[(:]\(\d\+\)\%(:\(\d\+\):\?\)\?' ]
"
" * code.cc#L10
"
let s:regexpressions = get(g:, 'file_line_regexpressions', [ '\(.\{-1,}\)\%((\|:\|#L\)\(\d\+\)\%(:\(\d\+\):\?\)\?' ])

function! s:reopenAndGotoLine(file_name, line_num, col_num)
if !filereadable(a:file_name)
Expand All @@ -35,7 +38,10 @@ function! s:reopenAndGotoLine(file_name, line_num, col_num)
exec "filetype detect"
endfunction

function! s:gotoline()
" Returns actual file name (without :* part)
" If is_goto parameter is 1, then file will be re-opened at the line parsed from
" :* part
function! s:get_file_name_and_goto(is_goto)
let file = bufname("%")

" :e command calls BufRead even though the file is a new one.
Expand All @@ -53,14 +59,27 @@ function! s:gotoline()
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)
let col_num = l:names[3] == ''? '0' : l:names[3]
if (a:is_goto == 1)
call s:reopenAndGotoLine(file_name, line_num,
\ col_num)
endif
return file_name
endif
endfor
return file
endfunction

" Get the actual file name
function! s:file_name()
return s:get_file_name_and_goto(0)
endfunction

" Open file at the line after :* part
function! s:gotoline()
return s:get_file_name_and_goto(1)
endfunction

" Handle entry in the argument list.
" This is called via `:argdo` when entering Vim.
function! s:handle_arg()
Expand All @@ -87,6 +106,7 @@ function! s:startup()
endif
endfunction

if !isdirectory(expand("%:p"))
" Only use file_line upon files (not directory), and only if file already exists
if (!isdirectory(expand("%:p")) && filereadable(expand(s:file_name())))
autocmd VimEnter * call s:startup()
endif