Open File in Default Editor from Bash

Opening default text editor in Bash?

The user's chosen editor should be in $EDITOR, but you must still choose a sane default.

"${EDITOR:-vi}" file.txt

open a file in editor and then save it back by terminal on bash

You can use "vim" or "vi".

you will be able to open and edit the files via shell.
You can do that with emacs too, but less likely it is already installed.

vi/vim require some key-strokes in order to edit and save :)

i = insert, which means you have to press 'i' before you start writing.

esc = exit mode (such as after you press 'i').

:w = save. only after you pressed 'esc'.

:q = quit.

:q! = force quit.

*For further information google it. this is just to help you start

Open file in $EDITOR by default in zsh

Zsh does support this through suffix aliases. Try this:

% alias -s rb=$EDITOR
% touch foo.rb
% foo.rb # it opens

I have a long list of suffix aliases in ~/.zshrc. This ruby extension is not a great example since program files often want to be executed, and now it surprisingly opens. It also may cause confusion with scripts in your path with conflicting names (which foo.rb?). It makes more sense for things like pdf, flac, mp4, csv, etc, when you don't want to remember which is your favorite app for the file type:

% alias -s pdf=evince

Command to open file with git

Git has nothing to do with how you open/edit files in your project. Configuring the editor in git is only so that git internal things that require an editor (commit messages for example) can use your preferred editor.

If you just want to open files from the command line (cmd.exe) as if they were double clicked in the windows explorer, I think you can use start <filename>.

How to open a file from CLI using a text editor of my choice, if the name of application has multiple strings?

I believe to open Visual studio code through the terminal you need to call it as 'code', so in your case it would be "start code filename.ext".
Be advised I am working in linux, it may be slightly different in windows. You can also look at the properties of vscode and it should say there how to open it.

How do I use Rust to open the user's default editor and get the edited content?

I could be wrong, but I think the way git commit works is that it creates a temporary file (.git/COMMIT_EDITMSG) and then as a sub-process it opens that file in the user's $EDITOR and then waits for the editor's process to exit/return. That will only happen when the user closes their editor. Which basically means that they can use the full power of their choice of editors, i.e. navigate in the file, save their changes, etc.

Thus, in Rust, you could use std::process::Command to open the user's editor, which you can get via the std::env::var. You can store the temporary file in a specific location if your application has one (like the .git directory for git or ~/.config/<your-app>/<your-file>, etc.) or you could create a temporary one inside the system's temporary directory returned to you by the std::env::temp_dir. (Alternatively you could use the excellent third party crate to directly create a temporary file only: tempfile)

Here's a working example using the above mentioned technique:

use std::{
env::{temp_dir, var},
fs::File,
io::Read,
process::Command,
};

fn main() {
let editor = var("EDITOR").unwrap();
let mut file_path = temp_dir();
file_path.push("editable");
File::create(&file_path).expect("Could not create file");

Command::new(editor)
.arg(&file_path)
.status()
.expect("Something went wrong");

let mut editable = String::new();
File::open(file_path)
.expect("Could not open file")
.read_to_string(&mut editable);

println!("File content:\n{}", editable);
}

How do I edit a file after I shell to a Docker container?

As in the comments, there's no default editor set - strange - the $EDITOR environment variable is empty. You can log in into a container with:

docker exec -it <container> bash

And run:

apt-get update
apt-get install vim

Or use the following Dockerfile:

FROM  confluent/postgres-bw:0.1

RUN ["apt-get", "update"]
RUN ["apt-get", "install", "-y", "vim"]

Docker images are delivered trimmed to the bare minimum - so no editor is installed with the shipped container. That's why there's a need to install it manually.

EDIT

I also encourage you to read my post about the topic.



Related Topics



Leave a reply



Submit