gokcehan/lf

`.bash_aliases` don't work in shell mode

Closed this issue ยท 12 comments

When aliases are in .bashrc, they work fine with lf shell mode. But don't inherit when separated into .bash_aliases even though though they work fine with terminal itself.

If you switch from bash to sh do those aliases work? LF runs commands in unix with sh by default, not bash

Edit: Played around with my local bash , which also imports from a bash_aliases file, I see my bash aliases.

In general aliases (and other things like shell completions) don't work because the command line in lf is not an interactive shell. The command line is more like a text box input where the result is executed as a script using a non-interactive shell.

There are workarounds discussed in #1094 and #1388, and perhaps other issues (search for the word 'alias'). but I think the command line was not intended to work this way. For common operations that would be used frequently, you can save them as cmds, and for once-off operations you can press w to open a shell.

Is there a way (possible) to inherit bash stuff into lf's textbox like shell ? At least .bash_aliases ? That would make sense too.

You mean having lf directly parse the contents of .bashrc/.bash_aliases and use it to expand aliases before sending commands to the actual shell? I'm hesitant to support this kind of idea - IMO the main focus of lf should be on managing files, not editing shell commands.

Anyway I find it strange that alias work from .bashrc, but not from .bash_aliases. Shouldn't there be something in your .bashrc file that sources the contents of .bash_aliases?

Sourcing .bash_aliases from bashrc obviously works in terminal sessions. But strangely doesn't in lf's shell drop in. I did test it by keeping aliases in .bashrc (without bash_aliases), as well as in bash_aliases (without in bashrc).
Else I wouldn't report this bug in the first place.
I might keep my aliases in rc itself, but my bashrc is already very tidy.

I'm not sure how you managed to get .bashrc aliases working in the first place, but anyway I found a method using the BASH_ENV environment variable, similar to what was discussed in #577.

Using the following lfrc:

set shell bash
set shellopts -O:expand_aliases

Run BASH_ENV=~/.bash_aliases lf, which will source ~/.bash_aliases and expand aliases when running shell commands.

Nop.

set shellopts -O:expand_aliases

completely borks lf, even if I set $BASH_ENV. It neither did source .bash_aliases.

I'm not sure how you managed to get .bashrc aliases working in the first place

I'm not sure why it ain't possible. Here's my .bashrc & lfrc (without .bash_aliases). Aliases work in lf shell drop in just fine.

lf runs shell commands in a non-interactive shell, which means aliases won't get expanded. I tried copying your config, and as I expected, typing something like !la doesn't work:

--: line 1: la: command not found
Press any key to continue

The idea behind my suggestion is that lf eventually end up calling something below:

BASH_ENV=~/.bash_aliases bash -c -O expand_aliases la

Which should also work if you type it on the command line outside of lf.

Otherwise, I don't have much advice apart from backing up your config files and trying things out on a clean setup.

I tried copying your config, and as I expected, typing something like !la doesn't work

Strange. No issues here.

$[~/.local/share]$ !la
la
.                                gnome-settings-daemon  icons       org.gnome.TextEditor  Trash
..                               gnome-shell            keyrings    pikaur                webkitgtk
applications                     gnupg                  lf          qutebrowser           xorg
backgrounds                      gstreamer-1.0          mime        recently-used.xbel    yarn
com.github.johnfactotum.Foliate  gvfs-metadata          nvim        rofimoji              zathura
desktop-directories              icc                    onlyoffice  sounds
$[~/.local/share]$

Btw, BASH_ENV=~/.bash_aliases in .bash_profile & set shellopts -O:expand_aliases in my lfrc did expand .bash_aliases:

# .bash_aliases
alias hello='echo "wassup!"'

But works only when dropping in home directory

$[~]$ hello
wassup!

Doesn't work in other home subdirectories though

$[~/.local/share]$ hello
bash: hello: command not found

It even works in regular terminal (foot)

$[~]$ cd .local/share/
$[~/.local/share]$ hello
wassup!
$[~/.local/share]$ 

Just not in subdirectories in lf shell drop in. Am I missing something ? What's the default mapping for w ? Caz that's what I use to drop into shell. Tq for your help btw.

Oh, so when you say shell mode, you mean the shell you get when you press w? I think there's been huge misunderstanding from me then, which I'm starting to suspect based on your last post. I thought you meant the shell/shell-pipe/shell-wait/shell-async commands, which you get by directly typing in lf. Apologies for the confusion.

In that case w just starts whatever you have defined as your $SHELL environment variable, which I'm pretty sure should be interactive and should source your .bashrc file. The thing is that AFAIK .bash_aliases isn't a special file in any way that bash would know about. If your .bashrc file doesn't source it but you have aliases available normally, then something else on your system must be sourcing it.

No prob ! I could've explained well about the "shell drop in" that I complain about. Apologize caz english ain't my native tongue.

If your .bashrc doesn't source it but you have aliases available normally, then something else on your system must be sourcing it.

As I said, I do source .bash_aliases from .bashrc. Without it, doesn't work anyways.

Lmao. Small blunder from my side !
[[ -f .bash_aliases ]] && . .bash_aliases was wrong.
That was relative location. Fixed by providing absolute one
[[ -f ~/.bash_aliases ]] && . ~/.bash_aliases