Yet simple tool to paste images into markdown files
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_imdir_intext = /relative/path/to/imgdir
on linux).
Using Vundle
Plugin 'ferrine/md-img-paste.vim'
Add to .vimrc
autocmd FileType markdown nnoremap <buffer> <leader>i :call mdip#MarkdownClipboardImage()<cr>
""Default name of markdown's alternative name of images.
" let g:mdip_imgname = 'image'
""Image dir path relative to expand('%:p:h'). And this will be expanded.
let g:mdip_imgdir = '%:p:t:r'
""Use prefix/postfix, for example: <filename>.asserts like typora.
let g:mdip_imgdir_prefix = ''
let g:mdip_imgdir_postfix = '.asserts'
This plugin gets clipboard content by running the xclip
command.
install xclip
first.
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.