Coder Social home page Coder Social logo

md-img-paste.vim's Introduction

md-img-paste.vim

Another modified 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 'melvio/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'

Prefix imgdir with file name

To prefix imgdir with the name of the file, add the following to your .vimrc :

let g:mdip_imgdir_filename_prefix=1

In this case, the images you copy into a file called test.md would be located in test_img

Warning: You can't use both mdip_imgdir_absolute and mdip_imgdir_filename_prefix at the same time.

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:

Filetype Function name Content
Markdown MarkdownPasteImate ![Image](path)
Latex LatexPasteImate \includegraphics{path} \caption{Image}
N/A EmptyPasteImate path

PRs welcome

For linux user

This plugin gets clipboard content by running the xclip command.

install xclip first.

Acknowledgements

Thanks to Karl Yngve Lervåg and Rich for help on vi.stackexchange.com. Thanks to @jdorel for providing a few lines of code.
Thanks to the contributors of the original project: https://github.com/ferrine/md-img-paste.vim

md-img-paste.vim's People

Contributors

brentyi avatar caglartoklu avatar cinghiopinghio avatar diegopenhanut avatar eternityz avatar ferrine avatar florianwanders avatar hensur avatar hritik14 avatar jdorel avatar joereddington avatar krisgry avatar lovepocky avatar lucasdf avatar matchachoco010 avatar melvio avatar superwf avatar vegovs avatar vertexi avatar w3bdev1 avatar wow-yes avatar xiguichen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.