Open file in $EDITOR by default in zsh

2k Views Asked by At

In zsh, if you can open directories without specifying cd command, like this

% pwd
/home/user/test
% ls
integration shared unit
% unit
% pwd
/home/user/test/unit

This effectively saves writing 'cd' everytime you try to open a directory. However it's smart enough not to 'cd' into files. Would it be possible to open a file without specifying $EDITOR command?

For example,

% ls
test
% test
% pwd
/users/home/test
% ls 
test.rb
% test.rb
... opens in an editor ...
1

There are 1 best solutions below

0
On BEST ANSWER

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