• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

md-img-paste-devs/md-img-paste.vim: paste image to markdown

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

md-img-paste-devs/md-img-paste.vim

开源软件地址(OpenSource Url):

https://github.com/md-img-paste-devs/md-img-paste.vim

开源编程语言(OpenSource Language):

Vim Script 95.7%

开源软件介绍(OpenSource Introduction):

md-img-paste.vim

Yet simple tool to paste images into markdown files

Use Case

You are editing a markdown file and have an image on the clipboard and want to paste it into the document as the text ![](img/image1.png). Instead of first copying it to that directory, you want to do it with a single <leader>p key press in Vim. So it hooks <leader>p, checks if you are editing a Markdown file, saves the image from the clipboard to the location img/image1.png, and inserts ![](img/image1.png) into the file.

By default, the location of the saved file (img/image1.png) and the in-text reference (![](img/image1.png) are identical. You can change this behavior by specyfing an absolute path to save the file (let g:mdip_imgdir_absolute = /absolute/path/to/imgdir on linux) and a different path for in-text references (let g:mdip_imgdir_intext = /relative/path/to/imgdir on linux).

Installation

Using Vundle

Plugin 'ferrine/md-img-paste.vim'

Usage

Add to .vimrc

autocmd FileType markdown nmap <buffer><silent> <leader>p :call mdip#MarkdownClipboardImage()<CR>
" there are some defaults for image directory and image name, you can change them
" let g:mdip_imgdir = 'img'
" let g:mdip_imgname = 'image'

Extend to other markup languages

Simply add a custom paste function that accepts the relative path to the image as an argument, and set g:PasteImageFunction to the name of your function. E.g.

function! g:LatexPasteImage(relpath)
    execute "normal! i\\includegraphics{" . a:relpath . "}\r\\caption{I"
    let ipos = getcurpos()
    execute "normal! a" . "mage}"
    call setpos('.', ipos)
    execute "normal! ve\<C-g>"
endfunction

Then in your .vimrc:

autocmd FileType markdown let g:PasteImageFunction = 'g:MarkdownPasteImage'
autocmd FileType tex let g:PasteImageFunction = 'g:LatexPasteImage'

The former sets the (default) markdown paste function for markdown files, while the latter sets the new latex paste function to be used in latex/tex files. The above LatesPasteImage has already been added to the plugin, see plugin/mdip.vim. Existing paste functions:

Finally, add the file type (e.g. tex) to the first line you added, as

autocmd FileType markdown,tex nmap <buffer><silent> <leader>p :call mdip#MarkdownClipboardImage()<CR>
                        '----'
Filetype Function name Content
Markdown MarkdownPasteImage ![Image](path)
Latex LatexPasteImage \includegraphics{path} \caption{Image}
N/A EmptyPasteImage path

PRs welcome

For linux user

This plugin gets clipboard content by running the xclip command.

install xclip first.

Acknowledgements

I'm not yet perfect at writing vim plugins but I managed to do it. Thanks to Karl Yngve Lervåg and Rich for help on vi.stackexchange.com where they proposed a solution for my use case.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap