These are useful maps for quickly popping in titles/urls/links open in Safari browser into vim
- These maps run apple scripts which are posted below
- These lines get added to your vim configuration file
" safari browser maps
" Copy and paste **url** of current open Safari tab into vim
inoremap <Leader>gu <esc>:silent !~/bin/copy_safari_url.osa <cr>a <esc>"+p<cr>
nnoremap <Leader>gu :silent !~/bin/copy_safari_url.osa <cr>"+p<cr>
" Copy and paste **title** of current open Safari tab into vim
inoremap <Leader>gt <esc>:silent !~/bin/copy_safari_title.osa <cr>a <esc>"+p<cr>
nnoremap <Leader>gt :silent !~/bin/copy_safari_title.osa <cr>"+p<cr>
" Copy and paste **markdown link** of current open Safari tab into vim using title and url
inoremap <leader>gg <esc>:set fo-=t <cr>:noautocmd !~/bin/copy_safari_title.osa <cr>:let @+ = substitute(@+, '\|', '\\|', 'g')<cr>:let @x=@+<cr>:noautocmd !~/bin/copy_safari_url.osa <cr>:let @+ = substitute(@+, '(', '%28', 'g')<cr>:let @+ = substitute(@+, ')', '%29', 'g')<cr>:let @y=@+<cr>:let @z='[' . getreg('x') . '](' . getreg('y') . ')'<cr>"zp:set fo+=t<cr>a
nnoremap <leader>gg :set fo-=t <cr>:noautocmd !~/bin/copy_safari_title.osa <cr>:let @+ = substitute(@+, '\|', '', 'g')<cr>:let @x=@+<cr>:noautocmd !~/bin/copy_safari_url.osa <cr>:let @+ = substitute(@+, '(', '%28', 'g')<cr>:let @+ = substitute(@+, ')', '%29', 'g')<cr>:let @y=@+<cr>:let @z='[' . getreg('x') . '](' . getreg('y') . ')'<cr>"zp:set fo+=t<cr>
Note the :noautcmd
in the calls to the external commands. This prevents
any autocmds that are triggered when the buffer is changed from running.
Applescripts
Add these Applescripts to a directory in your shell’s bin $PATH
#!/usr/bin/osascript
tell application "Safari"
set theURL to URL of current tab of front window
set the clipboard to [theURL](theURL)
end tell
#!/usr/bin/osascript
tell application "Safari"
set theTitle to the name of the current tab of window 1
set the clipboard to theTitle
end tell
Other notes linking here:
View the edit history