1

I am beginning to learn bash and emacs and wanted to change my default editor from vi to emacs. After going through many great questions throughout different stackexchange communities, I have not figured it out and apparently have (hopefully only temporarily) messed up my v command when using less. Now when I press v while reading a file in less, instead of opening vi (the default) or emacs (what I'd like), it does nothing; i.e. it stays in less, as far as I can tell, and I cannot edit any text. Fortunately I can still exit using q and I don't notice anything else acting up.

What I did and when things changed:

I am using the terminal emulator provided with mac osX, but I do have XQuartz downloaded. Using homebrew cask I installed emacs.

A lot of questions on stackexchange did a great job explaining the $EDITOR and $VISUAL variables that can be set in my .bash_profile. Since my macbook comes with an older version of emacs already installed, I made sure to use the current version of emacs installed by homebrew.

Since I'm just beginning to learn all of this stuff, I couldn't determine the best way of setting everything up. Here is how I currently have it in my .bash_profile:

# changing emacs from the default osX version to the version obtained from homebrew                 
alias emacs="/usr/local/Cellar/emacs/25.2/Emacs.app/Contents/MacOS/Emacs"
alias enw="emacs -nw"

# setting emacs as the editor in term instead of vi                                                
EDITOR="enw";export EDITOR
VISUAL="enw";export VISUAL

After adding the above code to my .bash_profile nothing significant (that I could tell) changed. I used echo to verify that the $EDITOR and $VISUAL variables were correctly changed.

When I would use less to read a file, I could still type v, but my terminal would use vi to edit the file instead of the desired emacs. So I tried changing how I defined $EDITOR and $VISUAL, e.g. by using the full path as in my alias emacs=... with the -nw flag instead of enw. And I also tried removing the quotations. None of this did anything different that I could tell.

Unfortunately since I'm just starting out and I do not have any concrete tasks to work on I'm just messing around with basic commands and files, which is why I have been using the less command. Currently I am not familiar with any other way of using the $EDITOR variable while in the terminal. So I'm not sure if my issue only pertains to v or if it is an issue with $EDITOR.

Eventually I saw some questions relating to this where it was recommended to add ALTERNATE_EDITOR="";export ALTERNATE_EDITOR and so I tried that out without really understanding why. Also no discernible difference with the v command in less.

After some more searching I found this question, which seems like the closest to my original plan (although I wasn't trying to pipe anything as I understand it; and I do not understand what 'paging' is unfortunately). So I changed $EDITOR="enw" to $EDITOR="env TERM=xterm-256color enw" and this was roughly when v stopped doing anything while reading a file with less.

Before doing the last code addition, I checked what $TERM normally is, which was already xterm-256color for me. Now I am not saying this is what messed up my v command, it's just the timeline where v stopped invoking vi while in less. I changed everything back to the very first code that I tried, but v still won't work for me.

(Sorry this is so long, just trying to explain the process in case it helps!) Now, if I comment out my $EDITOR additions to my .bash_profile everything goes back to normal, which is good I guess. So I can use v in less as long as I am not using $EDITOR and $VISUAL in my .bash_profile. However I want to have these changed and so this is where my dilemma is. This is probably a trivial question, but I would greatly appreciate any advice or insight.

PS- While trying to trouble shoot this issue I came across emacsclient and I can use that command, but I don't understand why I should use that instead of my enw.

1 Answer 1

2

Aliases are only "active" in the current shell session. less will not be able to invoke an alias through $EDITOR or $VISUAL (and no other process except from possibly the shell itself would either).

Instead, set e.g. VISUAL to

export VISUAL='/usr/local/Cellar/emacs/25.2/Emacs.app/Contents/MacOS/Emacs -nw'

or install emacs in a standard location accessible through your $PATH, either by compiling it yourself, or using something like HomeBrew, MacPorts, or Fink, and then simply use export VISUAL='emacs -nw'.

1
  • I swear that I tried this but clearly I messed it up somewhere. I just did exactly as you recommended and it works perfectly now, thank you very much! And thanks for explaining the part about aliases only being active in the shell, I was unaware of this.
    – Sleepy 17
    May 30, 2017 at 17:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .