Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/vimrunner/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ def echo(*expressions)
command "echo #{expressions.join(' ')}"
end

# Public: Send keys as if they come from a mapping or typed by a user.
#
# Vim's usual remote-send functionality to send keys to a server does not
# respect mappings. As a workaround, the feedkeys() function can be used
# to more closely simulate user input.
#
# Any keys are sent in a double-quoted string so that special keys such as
# <CR> and <C-L> can be used. Note that, as per Vim documentation, such
# keys should be preceded by a backslash, e.g. '\<CR>' for a carriage
# return, '<CR>' will send those four characters separately.
#
# Examples
#
# vim.command 'map <C-R> ihello'
# vim.feedkeys '\<C-R>'
#
# Returns nothing.
def feedkeys(string)
string = string.gsub('"', '\"')
server.remote_expr(%Q{feedkeys("#{string}")})
end

# Public: Sets a setting in Vim. If +value+ is nil, the setting is
# considered to be a boolean.
#
Expand Down
19 changes: 19 additions & 0 deletions spec/vimrunner/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ module Vimrunner
end
end

describe "#feedkeys" do
before :each do
client.edit 'some_file'
client.command 'map <C-R> ihello'
end

it "sends keys as if they come from a mapping or user" do
client.feedkeys('\<C-R>')
client.write
File.read('some_file').strip.should eq 'hello'
end

it "handles quotes" do
client.feedkeys('\<C-R>\'"')
client.write
File.read('some_file').strip.should eq 'hello\'"'
end
end

describe "#command" do
it "returns the output of a Vim command" do
client.command(:version).should include '+clientserver'
Expand Down