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

Run multiple commands in layouts #1099

Closed
Korbeil opened this issue Feb 23, 2022 · 16 comments
Closed

Run multiple commands in layouts #1099

Korbeil opened this issue Feb 23, 2022 · 16 comments

Comments

@Korbeil
Copy link

Korbeil commented Feb 23, 2022

Hey,

I would like to create layout for my projects but to do that I need to run multiple commands.
For example here is a script I'm running when starting a project:

cd ~/path/to/my/project/
pipenv run fab create_database
pipenv shell

First part could be removed if we add a cwd configuration in the layout parts section.
For the 2 last lines I would need to run multiple commands, do you think that is possible ?

Thanks for the awesome tool :)

@a-kenji
Copy link
Contributor

a-kenji commented Feb 23, 2022

Hey! Thanks for creating this issue! Yes, that is possible.

https://zellij.dev/documentation/layouts.html

@Korbeil
Copy link
Author

Korbeil commented Feb 23, 2022

Hey! Thanks for creating this issue! Yes, that is possible.

zellij.dev/documentation/layouts.html

I did not find how to run multiple commands or setting a working directory here, could you give me an example maybe ?

@a-kenji
Copy link
Contributor

a-kenji commented Feb 23, 2022

Sure!

Here is a small example:

tabs:
  - name: tab 1
    run:
      command: {cmd: fish, args: ["-C $HOME/Downloads && fish -C ls && fish"]}

@Korbeil
Copy link
Author

Korbeil commented Feb 23, 2022

hmmm I'm using bash at the moment and with the following layout:

---
tabs:
  - name: Application
    run:
      command: {cmd: bash, args: ["-c cd /tmp && bash"]}

But the only output I get is:

 $ zellij --layout-path ./zellij.yaml 
Bye from Zellij!

Maybe I did something wrong ?

@a-kenji
Copy link
Contributor

a-kenji commented Feb 24, 2022

Every shell is different and -c in bash doesn't work like -C in fish.

I think this should work, but I don't use bash often:

---
tabs:
  - name: Application
    run:
      command: {cmd: bash, args: ["-c", "cd /tmp && exec bash"]}

@welcoMattic
Copy link
Contributor

@a-kenji I've just tested, and it doesn't work, neither with zsh. Is it feasible to pass a full string command like:

---
tabs:
  - name: Application
    run:
      command: "cd /tmp && exec bash"

And it will be up to zellij to encapsulate it with the current shell "-c" option?

@a-kenji
Copy link
Contributor

a-kenji commented Feb 24, 2022

I tested it now and it does work for both bash and zsh for me.

---
tabs:
  - name: tab 1
    run:
      command: {cmd: bash, args: ["-c", "cd /tmp && exec bash"]}

Is there more information that you can maybe give me where it fails.
Does it show an error? What are your steps?
Can you run the arguments when you put them in a script and run it then?

And it will be up to zellij to encapsulate it with the current shell "-c" option?

Maybe? I do want to have the ergonomics of the command improved. Especially on holding a pane open. I am not sure how feasible it is to integrate behavior that is very shell dependent.

@a-kenji
Copy link
Contributor

a-kenji commented Feb 24, 2022

@welcoMattic

Just to make sure, did you have the arguments split like this:

["-c", "cd /tmp && exec bash"]

?

@welcoMattic
Copy link
Contributor

welcoMattic commented Feb 24, 2022

@a-kenji Yes, I have args splitted. But there are 2 things:

  1. command: {cmd: bash, args: ["-c", "cd /tmp && exec bash"]} works, I have a zellij instance, with bash in a pane.
  2. command: {cmd: bash, args: ["-c", "cd /tmp && ls"]} doesn't work, I have instantly Bye from Zellij!

What is needed here, is to execute some command to prepare a pane for a project (installing dependancies, running containers, etc).

@a-kenji
Copy link
Contributor

a-kenji commented Feb 24, 2022

Does this work:

tabs:
  - name: tab 1
    run:
      command: {cmd: "bash", args: ["-c", "cd /tmp && ls && exec bash"]}

?
ls returns and we don't hold on a returning command yet. But you can work around it while keeping a command open.

@welcoMattic
Copy link
Contributor

Yes, it works. Not optimal, but it does the job. Isn't is possible to plan to run a collection of commands instead of only one?

@a-kenji
Copy link
Contributor

a-kenji commented Feb 24, 2022

Ideally: yes, that is what #851 proposes. It just is not there yet sadly.
What you can already do is bind this process to a shortcut which is also not ideal yet, because we don't have the hold attribute yet, but something like that can be found here: #1061 .

@welcoMattic
Copy link
Contributor

Thanks, I will keep an eye on future updates. Meanwhile, I will use your trick to run some commands in a pane. Thanks a lot!

cc @Korbeil sorry to squatting your issue, but I know we have the same issue 😄

@Korbeil
Copy link
Author

Korbeil commented Feb 24, 2022

No problem, it solved my issue so I'm glad you did :) Thank you both and thank for zellij @a-kenji !

@GregPK
Copy link

GregPK commented Mar 12, 2024

If you're here and confused about the yaml layout, this is a how to do this in KDL:

Bash:

pane command="bash" {
    args "-c" "cd /tmp && ls"
}

Fish:

pane command="fish" {
    args "--command" "cd /tmp && ls"
}

@ohare93
Copy link

ohare93 commented May 15, 2024

Had this issue, but when running as a command with a pipe. Here's how I eventually resolved it:

> zrf --cwd "/mnt/c/Projects/mystuff" git ls-tree -r HEAD ./src/myproject | git hash-object --stdin

error: Found argument '--cwd /mnt/c/Projects/mystuff ...' which wasn't expected, or isn't valid in this context.

It seems zrf can't take --cwd since it's already an alias. Gonna have to be specific

> zellij run -f --cwd "/mnt/c/Projects/mystuff" git ls-tree -r HEAD ./src/myproject | git hash-object --stdin

The pipe caused the last part to be considered outside my zellij command 🥹 Need to encapsulate it

> zellij run -f --cwd "/mnt/c/Projects/mystuff" "git ls-tree -r HEAD ./src/myproject | git hash-object --stdin"

error: Found argument 'git ls-tree -r HEAD ./src/myproject | git hash-object --stdin' which wasn't expected, or isn't valid in this context

Command needs to come after -- when adding more options

> zellij run -f --cwd "/mnt/c/Projects/mystuff" -- "git ls-tree -r HEAD ./src/myproject | git hash-object --stdin"

Command not found: git ls-tree -r HEAD ./src/myproject | git hash-object --stdin
If you were including arguments as part of the command, try including them as 'args' instead.

> zellij run -f --cwd "/mnt/c/Projects/mystuff" -- zsh -c "git ls-tree -r HEAD ./src/myproject | git hash-object --stdin"

Works! 👍

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

5 participants