how to read file in to existing buffer
I have some existing buffer, I would like to read/merge/insert text from another file, aka: r filename.txt in vi. How to do it in micro? Thanks
You can use textfilter cat filename.txt. See help commands for the description of what the textfilter command does.
There is a caveat though: textfilter passes the selected text through a command (as documented), but if there is no text selected, and the cursor is on a word, it passes this word though this command (and that is not documented, unfortunately). So it may not behave the way as you'd expect: it may unexpectedly replace the current word with the command output, instead of just inserting this output. (But if the cursor is luckily not on a word but on a whitespace, e.g. between words, ~~it will just insert the command output at the cursor.~~ Just noticed that actually even in this case it will replace something: it will replace the current character.)
Hi,
I have a function for inserting a file:
function editor.InsertFile(Current,item)
if io.exists(item) then
local filename,err = ioutil.ReadFile(item)
if not err then
Current.Buf:Insert(editor.GetTextLoc(Current), filename)
end
end
end
This function has dependencies:
-- Import go libraries
local ioutil = import("io/ioutil")
local osutil = import("os")
-- Write io.exists function - this one is for Windows
function io.exists(filename)
-- Check it's a string
if type(filename)~="string" then return false end
-- Check for unacceptable characters
if filename:match("[\n<|>$]") then return false end
-- TODO: Check for valid filename
-- Convert to unix directory separators - for Go
filename = filename:gsub([[/]],[[\]])
-- Use osutils
local fileinfo, err = osutil.Stat(filename)
if osutil.IsNotExist(err) then
return false
else
return true
end
end
-- Set up editor table and functions
editor = {}
function editor.GetTextLoc(Current)
-- Returns Loc-tuple w/ current marked text or whole line (begin, end)
-- https://github.com/NicolaiSoeborg/manipulator-plugin/blob/master/manipulator.lua
local a, b = nil, nil
if editor.HasSelection(Current) then
if Current.Cursor.CurSelection[1]:GreaterThan(-Current.Cursor.CurSelection[2]) then
a, b = Current.Cursor.CurSelection[2], Current.Cursor.CurSelection[1]
else
a, b = Current.Cursor.CurSelection[1], Current.Cursor.CurSelection[2]
end
else
local eol = string.len(Current.Buf:Line(Current.Cursor.Loc.Y))
a, b = Current.Cursor.Loc, buffer.Loc(eol, Current.Cursor.Y)
end
return buffer.Loc(a.X, a.Y), buffer.Loc(b.X, b.Y)
end
function editor.HasSelection(Current)
return Current.Cursor:HasSelection()
end
Sorry this is a bit convoluted, I keep all my Lua code in one file, crediting plugin developers for their contributions.
Hope this helps.
Kind Regards Gavin Holt