Giter VIP home page Giter VIP logo

autolink.vim's Introduction

autolink.vim

This vim plugin automatically finds and inserts URLs for links in Markdown and ReST documents. You can use a search engine (DuckDuckGo) or the current tab in Safari or Chrome.

Search for URLs

autolink.vim uses the DuckDuckGo search engine API to find URLs matching the link IDs in Markdown and reStructuredText reference-style links. It automatically inserts the first link found.

For example, say you have this document:

I think [Markdown][] is really great.

[Markdown]: need a URL for this

With your cursor on the last line, you can type <leader>ac (for auto-complete link) and the last line will become:

[Markdown]: http://daringfireball.net/projects/markdown/

Behind the scenes, the plugin searches on DuckDuckGo for the word "Markdown" and inserts the first result's URL, a reasonable guess for a relevant link on the subject, in the appropriate place. This also works in ReST documents on hyperlink target lines like .. _Markdown: link goes here.

Create Link Definitions

This plugin can help you insert the markup for reference-style links. For example, suppose you have just typed this paragraph:

I prefer the [vim text editor][vim].

Type <leader>am (for auto-make link) to add a definition for the link below the current paragraph:

I prefer the [vim text editor][vim].

[vim]: 

This also works in ReST for reference-style links like `foo`_.

The plugin is careful to insert newlines between text paragraphs and blocks of link references to make your document look nice.

Grab a Browser Tab

You can also grab the URL for your browser's current tab and insert that. (This currently works for Safari and Chrome and only on OS X.) Just type <leader>aB and the URL will appear (magically!) at your cursor.

All Together Now

To insert and complete a link definition in one fell swoop, use <leader>al (for auto-link, the name of this plugin). This will add a reference to your link after the current paragraph, fill it out with a search result, and then jump back to your current cursor position to let you keep writing. (If your leader is \, then it's like typing mq\am\ac`q.)

To do this with your browser's foremost tab instead, type <leader>ab (the b is for browser).

Bonus: Open a Search

Sometimes, you aren't feeling lucky. To open the current link's keys as Google search terms, type \as for auto-search. You can then click the result you want and flip back to \ab to insert the page you find.

Installing

The plugin requires vim to be built with Python bindings. If you're using Pathogen, just clone this repository into your bundles directory (and run :Helptags). Otherwise, place files in plugin, autoload, and doc into the corresponding directories in ~/.vim.

To-Do

  • Automatic activation when completing a link reference (i.e., after typing ] in Markdown)?
  • Optionally use the link text, rather than the reference ID, as the search terms.
  • Options to use subsequent results (if the first link isn't good).
  • Handle web service errors (avoid inserting "None", possible retry).

Credits

This plugin is by me, Adrian Sampson. This is my first bit of vimscript hackery and is very experimental---I apologize for weirdnesses arising from my unfamiliarity with writing vim plugins.

The first-search-result idea is shamelessly borrowed from Brett Terpstra, whose ingenious "Lucky Link" service for Mac OS X is the first place I saw this kind of functionality.

Jim Kalafut is responsible for switching the plugin over from the defunct Blekko API to DuckDuckGo. Thanks, Jim!

The code is available under the MIT license.

autolink.vim's People

Contributors

alexandreroberts avatar kalafut avatar sampsyo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

autolink.vim's Issues

Feature request: using in wiki filetype too

I would like to be able to use the <leader>am feature to create reference links below a paragraph while in .wiki buffers in addition to .md buffers. The syntax for these links is the same as in Markdown, so I think that all that needs to be changed is that this functionality is activated for wiki buffers.

Search in browser

A shortcut to open Google in the browser to a search term from the document (\ag?). Ideally, this could optionally use I'm Feeling Lucky and open the browser in the background so I can keep typing.

Link auto-complete always results in None

Hey Adrian! I just came across AutoLink, and being a big fan of Beets, I immediately needed to try it out. ๐Ÿ˜

When I type the following, move the cursor to the last line, and tap <leader>ac โ€ฆ

[Beets][beets music]

[beets music]:

โ€ฆ the returned URL is None (and it's always None, for any search terms):

[Beets][beets music]

[beets music]: None

I opened a Python REPL and tested the underlying get_link function to retrieve the result for the "beets music" search terms, and the resulting html variable seems to contain the expected search result:

[...]
            <div class="result results_links results_links_deep web-result ">


          <div class="links_main links_deep result__body"> <!-- This is the visible part -->

          <h2 class="result__title">

            <a rel="nofollow" class="result__a" href="/l/?kh=-1&amp;uddg=http%3A%2F%2Fbeets.io%2F"><b>beets</b>: the <b>music</b> geek&#x27;s media organizer</a>

          </h2>
[...]

Perhaps the markup has changed, and the regex needs to be updated? What do you think?

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.