Skip to content

Instantly share code, notes, and snippets.

@swaroopch
Created August 6, 2010 06:18
Show Gist options
  • Save swaroopch/510925 to your computer and use it in GitHub Desktop.
Save swaroopch/510925 to your computer and use it in GitHub Desktop.
In Vim, command to open a filename under the cursor in a new Vim tab (or if URL, open in browser)
if has('python') " Assumes Python >= 2.6
" Quick way to open a filename under the cursor in a new tab
" (or URL in a browser)
function! Open()
python <<EOF
import re
import platform
import vim
def launch(uri):
if platform.system() == 'Darwin':
vim.command('!open {0}'.format(uri))
elif platform.system() == 'Linux':
vim.command('!gnome-open {0}'.format(uri))
def is_word(text):
return re.match(r'^[\w./?%:#&=-]+$', text) is not None
filename_start = filename_end = vim.current.window.cursor[1] # (row, col)
while filename_start >= 0 and is_word(vim.current.line[filename_start:filename_start+1]):
filename_start -= 1
filename_start += 1
while filename_end <= len(vim.current.line) and is_word(vim.current.line[filename_end:filename_end+1]):
filename_end += 1
filename = vim.current.line[filename_start:filename_end]
if filename.endswith('.rst') or filename.endswith('.txt'):
vim.command('tabedit {0}'.format(filename))
elif filename.lower().startswith('http') or filename.lower().startswith('www.'):
if filename.lower().startswith('www.'):
filename = 'http://{0}'.format(filename)
filename = filename.replace('#', r'\#').replace('%', r'\%')
launch(filename)
else:
launch(filename)
EOF
endfunction
command O call Open()
map <Leader>o :call Open()<CR>
endif " python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment