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
How to open the default text editor in Linux?
There is no completely reliable concept of "default editor" on Linux, let alone more broadly Unix-like systems.
Traditionally, users would set the environment variable EDITOR
to the path of their editor of choice. If this variable is set, I'm thinking you can be reasonably confident that they will know how to use it, even if they end up in something horrible like nano
.
A slightly newer convention is to set VISUAL
to the preferred "visual editor" - I guess the terminology comes from vi
to contrast against line editors like ed
.
${VISUAL-${EDITOR-nano}} path/to/new/file.txt
On Debianish systems, the system default editor is configurable via alternatives
and available simply with the command editor
.
On XDG systems, of course, you could simply
touch path/to/new/file.txt
xdg-open path/to/new/file.txt
Needless to say, this only works if you have XDG, i.e. In practice a Linux (or maybe modern *BSD) platform with an active graphical session (excludes Mac and pre-XDG graphical systems as well as of course any server environment where there is no GUI).
As an aside, if I can guess even roughly what your script does, it could probably be pared down to a fairly simple sed
script. Remember, sed
can do (almost) everything grep
and tail
can. Maybe see also Combining two sed commands - here is a quick and dirty refactoring.
cd /usr/share/applications
$(sed -n "s:^Exec=\([^%]*\)\(%.\(.*\)\)*:\1\3:p" "$(sed -n "s:^$1=::p" defaults.list | tail -1)" | tail -1) &
However, from quick googling, it looks like /usr/share/applications/defaults.list
is specific to OpenDesktop environments; but it's the system-wide default default - the admin could have installed an override in a different location, and individual users probably have individual preferences on top of that. Finding and traversing this hierarchy is precisely what xdg-open
does, so I'm not going to try to reimplement it in an ad-hoc script of my own, and suggest you shouldn't, either.
There is nothing about graphical environments in your question, so it's unclear whether you are actually looking for a simple editor for beginners who barely know how to click and drool in a graphical environment (in which case I'd say go with touch
followed by xdg-open
) or a competent programmers' editor which way or may not run in a window (maybe try VISUAL
with fallback to EDITOR
, and document that you use this mechanism).
Git Commit - Why isn't GitBash launching my default text editor (sublime) for me to enter commit message
The following should work for you:
git config --global core.editor "'C:/Program Files/Sublime Text 2/sublime_text.exe' -n -w"
The reason your alias works is because it gets expanded by the shell:
alias sublime="C:/Program\ Files/Sublime\ Text\ 2/sublime_text.exe"
sublime -> C:/Program\ Files/Sublime\ Text\ 2/sublime_text.exe ->
'C:/Program Files/Sublime Text 2/sublime_text.exe'
FYI, an alias is only in the shells memory and cannot be used by any process called by the shell, you could create a script called: subl
and place it somewhere in your $PATH
:
$ echo "$PATH"
/usr/local/bin:/usr/bin:/bin:/home/USER/bin
$ cat "$HOME/bin/subl"
#!/bin/sh
'C:/Program Files/Sublime Text 2/sublime_text.exe' "$@"
The above will enable you to use the command subl
everywhere, even as git core.editor:
git config --global core.editor 'subl -n -w'
Alternative you can set $VISUAL
or $EDITOR
environment variables which git will use if no core.editor is set:
export EDITOR="C:/Program Files/Sublime Text 2/sublime_text.exe"
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 to use Visual Studio Code as default editor for git?
In the most recent release (v1.0, released in March 2016), you are now able to use VS Code as the default git commit/diff tool. Quoted from the documentations:
Make sure you can run
code --help
from the command line and you get
help.
if you do not see help, please follow these steps:
Mac: Select Shell Command: Install 'Code' command in path from the Command
Palette.
- Command Palette is what pops up when you press shift + ⌘ + P while inside VS
Code. (shift + ctrl + P in Windows)- Windows: Make sure you selected Add to PATH during the
installation.- Linux: Make sure you installed Code via our new .deb or
.rpm packages.- From the command line, run
git config --global core.editor "code --wait"
Now you can run
git config --global -e
and use VS Code as editor for configuring Git.
Add the following to enable support for using VS Code as diff tool:
[diff]
tool = default-difftool
[difftool "default-difftool"]
cmd = code --wait --diff $LOCAL $REMOTE
This leverages the new
--diff
option you can pass to VS Code to
compare two files side by side.To summarize, here are some examples of where you can use Git with VS
Code:
git rebase HEAD~3 -i
allows to interactive rebase using VS Codegit commit
allows to use VS Code for the commit messagegit add -p
followed bye
for interactive addgit difftool <commit>^ <commit>
allows to use VS Code as diff editor for changes
How to change the text editor in git to Sublime - currently git commit commands are opening in Notepad
Try this:
git config --global core.editor "'c:/program files/sublime text 3/subl.exe' -w"
This worked for me (i.e. on performing a git commit
a Sublime Text window opened up where I was able to type the commit message and after saving and closing the window, I checked that this commit had the commit message that I had just typed in with a git show
) on Windows 10 with Git for Windows version 2.10.2 and Sublime Text 3 build 3126
Note that the windows command line helper subl.exe
was introduced in Build 3065 on 27 August 2014, so this should work on any build including and after #3065 :
Update: Git config scopes!
The output that OP posted in response to the first comment clearly shows the problem:
Command:
git config --list --show-origin | grep -i core.editor
file:C:/Users/ryanj/.gitconfig core.editor='c:/program files/sublime text 3/subl.exe' -w
file:.git/config core.editor=notepad
OP's has a repository level configuration for core.editor
which is notepad and overrides the global configuration which is set to what he expects (i.e. Sublime Text 3).
To fix this, run the following:
git config --unset core.editor
and confirm that git config --list --show-origin | grep -i core.editor
shows you only ONE configuration file (i.e. c:/users/ryanj/.gitconfig) with Sublime Text 3 set as the editor.
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);
}
Related Topics
Calculate Total Disk I/O by a Single Process
Does File ".Bash_History" Always Record Every Command I Ever Issue
Is There a Libc in Kernel Space
Why Use G++ Instead of Gcc to Compile *.Cc Files
How to Remount The /Proc Filesystem in a Docker as a R/W System
Yocto Bitbake Script Not Displaying Echo Statement
Ubuntu Based Docker-Machine Image
Writing a Syscall to Count Context Switches of a Process
Are System Calls on Windows Inherently Slower Than Linux
Watermarking Video from The Linux Command Line
Trouble with Vagrant - "404 - Not Found"
How to Check Character Encoding of a File in Linux
How to Let Users Run a Script with Root Permissions