Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open term in same folder as text file I'm editing #3684

Open
PetrLaskevic opened this issue Mar 8, 2025 · 5 comments
Open

Open term in same folder as text file I'm editing #3684

PetrLaskevic opened this issue Mar 8, 2025 · 5 comments

Comments

@PetrLaskevic
Copy link

Hi, I'm learning micro right now, and wanted to have a shell alongside the file I'm editing - in a similar style to VS Code for example.

The problem is, the term command turns on the shell at my home directory (/home/petr), and not the directory I'm editing my file in (/home/petr/Documents/petrlaskevic.github.io). This means I'd have to cd my way through.

Could there be an option to open the shell at the current folder? (like term . for example?)

The ideal workflow for me would be:

1.) open a file index.html in /home/petr/Documents/petrlaskevic.github.io/index.html
2.) hsplit index.html (or better yet, a command for hsplitting the current file - like hsplit . - to not have to write the whole name)
3.) ctrl e
4.) term . to open the shell at /home/petr/Documents/petrlaskevic.github.io

I believe this would be a fine addition to micro :)

Many programs have opening in a similar vein (with . as the argument).

@matthias314
Copy link
Contributor

matthias314 commented Mar 8, 2025

In my init.lua I have the following code that always changes the active directory to the directory of the currently active file:

local filepath = import("path/filepath")

function onSetActive(bp)
      if #bp.Buf.Path ~= 0 then
          bp:CdCmd({filepath.Dir(bp.Buf.AbsPath)})
      end
end

I think this would address this issue. It find it also useful when opening new files.

The problem is that the code above only works if onSetActive is always called when a bufpane becomes active. Currently this is not the case, see #3563. Some time ago I submitted a fix #3584 that works well for me and that I think would be of use to others, too.

@matthias314
Copy link
Contributor

Regarding the splitting, I'm using the following functions, bound to some keys. The functions hsplit and vsplit split the current buffer, unsplit closes all panes of the current tab except for the active one. (Micro has an Unsplit action, but it's not clear to me what it is intended to do, see #2045.)

local micro = import("micro")
local buffer = import("micro/buffer")

function hsplit(bp)
    local buf, err = buffer.NewBufferFromFile(bp.Buf.AbsPath)
    if err then
        micro.InfoBar():Error("Cannot split this pane")
    else
        local newbp = bp:HSplitBuf(buf)
        local loc = bp.Cursor.Loc
        newbp:GotoLoc(buffer.Loc(loc.X, loc.Y))
    end
end

function vsplit(bp)
    local buf, err = buffer.NewBufferFromFile(bp.Buf.AbsPath)
    if err then
        micro.InfoBar():Error("Cannot split this pane")
    else
        local newbp = bp:VSplitBuf(buf)
        local loc = bp.Cursor.Loc
        newbp:GotoLoc(buffer.Loc(loc.X, loc.Y))
    end
end


function unsplit(bp)
    local tab = bp:Tab()
    local ids = {}
    for i = 1, #tab.Panes do
        local id = tab.Panes[i]:ID()
        if id ~= bp:ID() then
            table.insert(ids, id)
        end
    end
    for _, id in ipairs(ids) do
        tab:SetActive(tab:GetPane(id))
        tab:CurPane():Quit()
    end
end

@Neko-Box-Coder
Copy link
Contributor

micro opens the terminal at the current working directory, similar to other code editors like VSCode.
You will need to cd to the directory first before editing the file.

@PetrLaskevic
Copy link
Author

PetrLaskevic commented Mar 15, 2025

2.) hsplit index.html (or better yet, a command for hsplitting the current file - like hsplit . - to not have to write the whole name)

Regarding 2.), I found that the Tab key works to autocomplete the name, and if more match, pressing Tab again cycles through the results :) I feel like this autocomplete could be mentioned in the docs somewhere :)

@PetrLaskevic
Copy link
Author

micro opens the terminal at the current working directory, similar to other code editors like VSCode. You will need to cd to the directory first before editing the file.

Thanks, this does indeed work nicely! However I think there should be some quick hsplit current file command for when I need to have files from multiple folders (opened with ctrl o in micro), and don't want to retype the path then (folder autocomplete helps, but still, some hsplitc (c for current) command would be nice)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants