sourcing nvim config throwing error with fish shell - bash

I recently switched to fish shell from zsh and I have been encountering errors when I update my nvim config file
It appears the error comes when checking for the $TMUX environmet variable in a conditional statement. I added set shell=/bin/bash to the config solve the problem reloading vim but the error persisted.
Here is the error from when I try source ~/.config/nvim/init.vim:
~/.config/nvim/init.vim (line 99): Illegal command name “exists("$TMUX")”
if exists("$TMUX")
^
from sourcing file ~/.config/nvim/init.vim
called on line 1 of file -
in function “sv”
called on standard input
source: Error while reading file “/home/slick/.config/nvim/init.vim”
Confused because I am not very solid with vimscript and am not sure which language the conditional statement in the nvim config should be written in or how to resolve this error.

source ~/.config/nvim/init.vim
is a Neovim command, not a shell command. There's no reason whatsoever to expect your shell to understand Neovim's scripting language so running that command in your shell makes no sense at all.
Run it in Neovim, not in your shell.

Vim scripts that run external commands expect a POSIX compatible shell. Fish is explicitly not POSIX compatible. Add set shell=/bin/sh (or some other POSIX shell) to your vimrc.

Related

Refactoring bash command into tcsh command

I have problem with refactoring bash command into tcsh-friendly command. Don't know the tcsh syntax very well so the error im receiving doens't give me any clue.
The whole bash script was about adding modules on server
MODULEPATH=/app/modules/0/modulefiles:/env/common/modules
export MODULEPATH
module() { eval `/app/modules/0/bin/modulecmd sh "$#"` ;}
I changed first two commands to tcsh already
setenv MODULEPATH /app/modules/0/modulefiles:/env/common/modules
set MODULEPATH
But i dont know how to change the syntax of last command. Console is returning me error "Badly placed ()'s.".
Can I ask for little lesson what to change in this command to be tcsh-friendly?
chepner is right saying tcsh doesn't have functions at all and you'd have to write an alias instead. That's not much of an effort for your one-line function:
alias module 'eval `/app/modules/0/bin/modulecmd sh \!*`'
Basically, we prepend alias, remove () and {}, quote and replace "$#" with \!*.
The module command you want to port from bash to tcsh already comes with an initialization file to the tcsh shell. For all shells or scripting languages module is compatible with, there is an initialization file provided in the init directory of the software.
So from your example, Modules is installed in /app/modules/0, so you should have a /app/modules/0/init directory and a /app/modules/0/init/tcsh script to initialize the module command for tcsh. You just have to source it to initialize the module command:
source /app/modules/0/init/tcsh
As Armali says, the module command on tcsh is defined with the alias shell command.
With recent version of Modules (version 4+), you also have the possibility to define the module command in your current shell session with the autoinit subcommand of the modulecmd script:
eval `/app/modules/0/bin/modulecmd tcsh autoinit`

bash not finding function when new bash shell open

I am on MacOS. I recently upgraded to Mojave. I'm running the Homebrew version of GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.5.0).
When I open a new bash shell by issuing the bash command, I get the following error every time I issue a new command:
bash: parse_git_branch: command not found
The error is getting generated from the following line in my .bashrc file that customizes my command line with the git :
export PS1="\[\033[32m\]iMac5K# \[\033[33;1m\]\w:\[\033[m\]\[\033[33m\]\$(parse_git_branch)\[\033[00m\] "
(Note: my .bashrc file is sourced by my .bash_profile file.)
The parse_git_brach is in my .bashrc file so I'm not sure why I am getting this error. Even after I manually source .bashrc, I still get the error.
Issuing which bash yields:
/usr/local/bin/bash
Thanks.
When you just run bash without -l or -i, it doesn't execute .bash_profile or .bashrc itself, but only honors variables it received through the environment.
When you export a variable, you're exposing it to child processes... through the environment.
So, your child shell receives the PS1 definition, but it doesn't receive the function that PS1 requires!
You have some options here:
Export the function alongside the PS1 definition that uses it. That is, export -f parse_git_branch. This has an important caveat in that only shells which read exported functions (which is to say, in practice, bash) will get any benefit from this fix.
Stop exporting the PS1. That is, just take the export off the line PS1='...'.
Set BASH_ENV=$HOME/.bashrc and ENV=$HOME/.bashrc, which will instruct even noninteractive shells to run .bashrc (of course, this can change the way scripts execute, and is thus at a risk for causing bugs in other software; moreover, the latter means your .bashrc needs to be written to be safe for non-bash shells).

Why doesn't .vimrc get executed?

When I try to use . .vimrc it gives and error:
bash: runtime! command not found
bash: syntax: command not found
bash: filetype: command not found
bash: filetype: command not found
It just randomly stopped working for no reason, all other dotfiles seem to work fine. The .vimrc contains this:
runtime! archlinux.vim
set number
set noswapfile
set nobackup
syntax on
set autoindent
set smartindent
set smarttab
filetype plugin on
filetype indent on
set incsearch
set hlsearch
It also gave an error inside a comment when it was there.
The . (or source) command is a bash command which reads a file (which should be a valid bash script) in the context of the current shell instance.
The .vimrc file is not a bash script, it's something that's read and processed by vim rather than bash.
It's no different to trying to compile C code with a Pascal compiler. The file content is not suitable for what you're trying to do with it. The .vimrc file should be automatically picked up next time you run a vim instance.
~/.vimrc is the runtime configuration file for vim i.e. the file will be read by vim when it starts and all the statements are vim specific.
As you are trying to source the file in bash, you are getting the errors as bash has no idea of the vim specific statements like runtime, syntax etc.
use vimorvi instead of source command to activate .vimrc.Because .vimrc is not *.sh like .bashrc and etc.

Why is this tcsh shell script giving the error while setting PATH LD_LIBRARY_PATH

I have created a tcsh shell script as follows:
#!/bin/tcsh
setenv PATH ""
setenv PATH .:$HOME/bin:/usr/sbin:/usr/bin:/bin:/usr/X11R6/bin:/usr/local/cuda/bin:/usr/local/bin:/usr/bin:$PATH
setenv LD_LIBRARY_PATH ""
setenv LD_LIBRARY_PATH .:/usr/local/cuda/lib:/usr/local/cuda/lib64/:/usr/local/cuda:/usr/lib:/usr/lib32:/usr/local/cuda/bin:/usr/local/lib/:${LD_LIBRARY_PATH}
Then I have made this script executable and when I try to execute it as
./script.sh, it gives following errors:
script.sh: 3: setenv: not found
script.sh: 4: setenv: not found
script.sh: 6: setenv: not found
script.sh: 7: setenv: not found
Any pointers, how to set these paths in my shell script?
I don't get the same error on my system.
UPDATE : See the last two paragraphs for my best guess about what's going on.
My initial best guess was that you can fix the problem by changing shebang
#!/bin/tcsh
to
#!/bin/tcsh -f
(Or use
#!/bin/csh -f
The features that tcsh adds to the original csh are mostly useful for interactive use rather than scripting.)
The -f option tells tcsh not to process your .login and .cshrc or .tcshrc files when it starts up. Generally you don't want a script to do this; it makes the script's behavior dependent on your own environment setup. Perhaps you have something in your .login that does something odd with setenv, though I can't think of what it might be. Try adding the -f and see if that helps. Even if it doesn't, you should do that anyway.
(And don't use -f for /bin/sh or /bin/bash scripts; it means something else, and isn't needed.)
Some other observations:
Setting $PATH and $LD_LIBRARY_PATH to the empty string is useless. Just delete those two lines.
EDIT :
On re-reading your question, I see what you're doing there. You set $PATH to the empty string, and then prepend more text to it:
setenv PATH ""
setenv PATH this:that:$PATH
That makes more sense than I thought it did, but it's still simpler to write one command:
setenv PATH this:that
Putting . in your $PATH, especially at the beginning, is a bad idea. Think about what happens if your run the script in a directory where someone has deposited a command name ls that does something nasty. If you want to execute a command in the current directory, use ./command. (Putting . at the end of $PATH is safer, but still not a very good idea.)
(And using tcsh or csh as a scripting language (as opposed to an interactive shell) is widely considered to be a bad idea as well. This article, even if it doesn't persuade you to give up on tcsh scripting, will at least make you aware of the pitfalls.)
Oh, and if it's a tcsh script, why do you call it script.sh? Suffixes on file names aren't required under Unix-like systems (unlike Windows), but usually a .sh suffix implies that it's a Bourne shell script. Call it script.tcsh, or script.csh, or just script.
EDIT :
Taking a closer look at the error message you're getting, it looks like the errors are coming from /bin/sh, not from tcsh.
On my system, when I change setenv to Setenv (a nonexistent command), running the script with tcsh gives me:
Setenv: Command not found.
Setenv: Command not found.
Setenv: Command not found.
Setenv: Command not found.
which doesn't match the error messages you showed us. When I run it explicitly as /bin/sh foo.tcsh (leaving the setenv commands alone), I get:
foo.tcsh: 3: setenv: not found
foo.tcsh: 4: setenv: not found
foo.tcsh: 6: setenv: not found
foo.tcsh: 7: setenv: not found
which does match the format of the errors you got.
You say that /bin/tcsh --version gives correct results, so that's not the problem. Somehow the script is being executed by /bin/sh, not by tcsh.
Here's my best guess about what's going on. You're running on Cygwin, or maybe MSYS, but you're invoking your script from a cmd shell, not from a Cygwin shell. Your Windows system has been configured to recognize a .sh suffix to indicate that the file is a script to be executed by C:\cygwin\bin\sh.exe (as I mentioned before, file suffixes don't usually matter on Unix, or in the Cygwin environment, but they do on Windows).
The simplest solution would probably be to rewrite the script to conform to Bourne shell syntax. But there should be ways to cause Windows to invoke Cygwin's tcsh to execute it. If I've guess right, let us know and we can probably come up with a solution.
I don't see anything wrong with your script. It works fine on my box.
The only reason for that error (that I can think of) is tcsh is somehow not being used as the interpreter.
I can reproduce the error if I add a line feed before #!/bin/tcsh. When the shebang is not the first chars in the file, the interpreter directive does not take effect and your current shell is used (I'm guessing your sheel is not c-shell variant (csh or tcsh)?).
So, check that #!/bin/tcsh is indeed the first line in the file, with no whitespace before.
To determine which interpreter is actually being used, try adding this to your script:
echo "$shell" ## prints shell name if tcsh or csh
echo "$BASH" ## prints /bin/bash if bash
example:
[me#home]$ bash x.sh # run using bash
/bin/bash
[me#home]$ tcsh x.sh # run using tcsh
/bin/tcsh
BASH: Undefined variable.
just hit this problem, and it turned out to be due to the script file having Windows EOLs. Once i cleaned it up, all was good.

Vimperator - Bash - can't use alias

I am using the Firefox plugin Vimperator that simulates vim like behaviour. However, i want to use some Bash commands i specified in my ~/.bashrc.
For example, i have todo.txt.sh bound to an alias 't' so that i can simply type 't add remberToDoThis' to add a task.
It would we wonderful to have the ability to add tasks from within Vimperator by typing
:!t add task
However, this is the message i get:
:!t add task
/bin/bash: t command not found
shell returned 127
I am using Ubuntu 10.10 Maverick Meerkat
Any suggestions?
Thank you for your try, but actually i found out a way:
just do the following in vimperator:
:set shell=/bin/bash
:set shellcmdflag=-ic
by default the shellcmdflag option is only "-c" by adding i we are telling the bash to be interactively, allowing to execute my aliases from the .bashrc
After doing this, it finally works. My aliases are recognized and are working, besides the fist line always telling me no job control available but i can live with that
Vimperator has no knowledge of bash aliases, so you have to call your script directly.
You can however define an abbreviation like:
:cabbr tt !todo.txt.sh
so you can type: :ttspace and have it expanded to !todo.txt.sh.

Resources