Created
September 22, 2013 02:44
-
-
Save xaviershay/6656216 to your computer and use it in GitHub Desktop.
Creates a mapping that will type out the given text into vim.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: | |
# | |
# ruby vimtype.rb | pbcopy | |
# type text | |
# ^D | |
# | |
# That outputs a map command bound to <leader>x, in vim type : then paste in | |
# the command, :set paste, then hit <leader>x | |
string = ARGF.read | |
def format(x) | |
case x | |
when "\n" then "<cr>" | |
when "|" then "\\|" | |
else x | |
end | |
end | |
def delay | |
n = if rand < 0.1 | |
150 | |
else | |
50 | |
end | |
":sleep #{n}ms<Cr>" | |
end | |
buffer = "" | |
sequence = string.chars.reduce(":set paste<cr>") {|s, x| | |
if x =~ /\s/ | |
buffer += x | |
else | |
unless buffer.empty? | |
s << "a#{buffer.chars.map {|y| format y }.join}<Esc>" | |
s << delay | |
buffer = "" | |
end | |
s << "a#{format x}<Esc>" | |
s << delay | |
end | |
s | |
} | |
puts "map <leader>x #{sequence}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment