Skip to content
New issue

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

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

Already on GitHub? # to your account

Way to execute shell commands #1541

Closed
IsotoxalDev opened this issue Jan 19, 2022 · 11 comments
Closed

Way to execute shell commands #1541

IsotoxalDev opened this issue Jan 19, 2022 · 11 comments
Labels
A-helix-term Area: Helix term improvements C-enhancement Category: Improvements E-easy Call for participation: Experience needed to fix: Easy / not much E-good-first-issue Call for participation: Issues suitable for new contributors

Comments

@IsotoxalDev
Copy link
Contributor

There are times when one wants execute shell commands in a text editor. Vim achieves this by taking !commands as commands for executing in shell. example: :!pandoc '%' --pdf-engine=xelatex -o ~/Documents/out.pdf &<cr><cr>. This is extremely convinient as i don't have to open a separate terminal to do this command. the % in the command refers to the current file

@IsotoxalDev IsotoxalDev added the C-enhancement Category: Improvements label Jan 19, 2022
@kirawi kirawi added the A-helix-term Area: Helix term improvements label Jan 19, 2022
@sudormrfbin
Copy link
Member

Does https://docs.helix-editor.com/keymap.html#shell fit your needs ?

@IsotoxalDev
Copy link
Contributor Author

Does https://docs.helix-editor.com/keymap.html#shell fit your needs ?

No. It executes the command and pastes the output. There are commands that needs to be run in background or it needs to run and the output needs to be shown on a separate buffer or window.

@dead10ck
Copy link
Member

dead10ck commented Jan 23, 2022

No. It executes the command and pastes the output. There are commands that needs to be run in background or it needs to run and the output needs to be shown on a separate buffer or window.

I'm curious what commands you're running where you need to see the output, but wouldn't be better served just by running it in another terminal window/pane/tab? Like if you have to switch buffers to see the output anyway, how is this better than the shell? If you have a pane with the output at the bottom of the editor, then how is this better than a split tmux pane?

@IsotoxalDev
Copy link
Contributor Author

i want to run a pandoc command which converts the markdown file i am editing into a pdf. I don't have a need to see the output. But i want to execute commands in a keymap. In vim i have mapped space+p as a way to compile my markdown file to a pdf. i don't want to open an external terminal to run just the command to run this simple command. I have zathura on the side of my screen. I use a wm with master and stack layout so having an external terminal reduces my zathura's real estate. Having a output right there is just a convinience.
A way to run a command with | or alt-| or ! or alt-! to be configured using the toml files is not there.
2022-01-23-205013_1920x1080_scrot

@kirawi kirawi added the E-good-first-issue Call for participation: Issues suitable for new contributors label Jan 23, 2022
@kirawi
Copy link
Member

kirawi commented Jan 23, 2022

Should be easy to create such a command via

fn shell_impl(
shell: &[String],
cmd: &str,
input: Option<&[u8]>,
) -> anyhow::Result<(Tendril, bool)> {
use std::io::Write;
use std::process::{Command, Stdio};
ensure!(!shell.is_empty(), "No shell set");
let mut process = match Command::new(&shell[0])
.args(&shell[1..])
.arg(cmd)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
{
Ok(process) => process,
Err(e) => {
log::error!("Failed to start shell: {}", e);
return Err(e.into());
}
};
if let Some(input) = input {
let mut stdin = process.stdin.take().unwrap();
stdin.write_all(input)?;
}
let output = process.wait_with_output()?;
if !output.stderr.is_empty() {
log::error!("Shell error: {}", String::from_utf8_lossy(&output.stderr));
}
let tendril = Tendril::try_from_byte_slice(&output.stdout)
.map_err(|_| anyhow!("Process did not output valid UTF-8"))?;
Ok((tendril, output.status.success()))
}

@archseer archseer added the E-easy Call for participation: Experience needed to fix: Easy / not much label Jan 26, 2022
@hayashikun
Copy link
Contributor

Running with alt-| ignores output and $ also does not insert text so that what you want to do is possible, isn't it?
But it would be nice to have a keymap to just run shell command, ignoring output. (e.g. #, ^, ctrl-| ??)

@IsotoxalDev
Copy link
Contributor Author

It is sure possible to run commands. but I want to bind a shell command to a keymap

@hayashikun
Copy link
Contributor

By adding a command (e.g. :shell) like the commands used in command mode (https://docs.helix-editor.com/commands.html), we can define a keymap that runs a shell command in config.toml.
Is my understanding correct?

@kirawi
Copy link
Member

kirawi commented Feb 19, 2022

I'm pretty sure.

@IsotoxalDev
Copy link
Contributor Author

Ye!!!. That PR looks good

@IsotoxalDev
Copy link
Contributor Author

As PR #1682 has been merged. This Issue can be closed.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-helix-term Area: Helix term improvements C-enhancement Category: Improvements E-easy Call for participation: Experience needed to fix: Easy / not much E-good-first-issue Call for participation: Issues suitable for new contributors
Projects
None yet
Development

No branches or pull requests

6 participants