Tab autocompletion in bash vi shell mode - bash

When using MSYS on a windows platform, I "set -o vi" to use the vi shell mode. Tab autocompletion for files and directories stops working. How to I renable this while remaining in vi shell mode?

Try:
bind -q complete
to see if it's set.
To set it at the Bash prompt:
bind '"\C-i":complete'
It should already be set by default, but it may be overridden in /etc/inputrc or ~/.inputrc possibly inside a $if mode=vi / $endif block. You can set for subsequent shell starts by adding this line to your ~/.inputrc file:
"\C-i": complete

For dir/file name completion try: ESC-\ or ESC-= or ESC-*
In my case (ubuntu 18.04) it doesn't work for commands.

Related

commenting not working in iTerm2 on macOS Big Sur

I'm using iTerm2(Z shell) on macOS 11.5.2. Because I use certain Perl one-liners (one line of Perl command in a shell application like iTerm2) a lot, it would be a lot easier if adding some comments to the one-liners so I could navigate back to it using the search function of iTerm2.
Normally, it's like perl path-to-perl-snippet/xxx.pl --an-arg xxx --other-args xxx a.txt #this is doing some task to do some work with a.txt.
Recently, I clean install the system and find some issues with commenting using #, i.e. the content behind # is interpreted by the command. This is not what I intend; It should be just comments.
At first, I thought it was a Perl problem. But the simplest command ls #display list also has the exact same problem, giving the following error ls: #display: No such file or directory ls: list: No such file or directory"
The expected behavior should be like executing ls (without the #display list) in the iTerm2, which is to display all the files under current directory.
So the real problem probably doesn't lie with Perl. It could be a setting problem with iTerm2 or other settings.
Any suggestions would be helpful. Thank you.
Newer versions of OS X (as of 10.15 Catalina) use the zsh shell by default, which has an "interactivecomments" shell option. It is set "off" by default. Turn it on with:
setopt interactivecomments
To preserve this setting for future shells, edit that line into your ~/.zshrc file.
The setting is documented in the man zshoptions section or online at https://zsh.sourceforge.io/Doc/Release/Options.html#index-comments_002c-in-interactive-shells. It is listed there as INTERACTIVE_COMMENTS, but the introduction to that section says:
These names are case insensitive and underscores are ignored. For example, ‘allexport’ is equivalent to ‘A__lleXP_ort’.
This means that you have several ways to enable the option (a partial list):
set -o interactivecomments
set -o Interactive_Comments
set -k
setopt interactivecomments
as well as disabling it (a partial list):
set +o interactivecomments
set +o Interactive_Comments
set +k
setopt nointeractivecomments
Whether or not # introduces a comment in an interactive shell is controlled by the interactive_comments option. It should be enabled by default, but if it has been disabled, run
shopt -s interactive_comments

Can I set an environment variable on Bash's command line?

I am trying to set an environment variable for Bash. However, I need this to be set before any of the shell's startup scripts (including /etc/profile), because /etc/profile acts differently based on the value of this variable.
Specifically, I want to create a shortcut to MinTTy that works like git-bash, but I need to set the MSYSTEM environment variable before the shell starts, or at least before it starts processing any startup scripts.
A solution that has MinTTy setting the environment variable before it starts the shell will also be accepted.
Edit:
What I am really looking for is sort of a command-line option to BASH that will set an environment variable, somewhat akin to the -D option to most C (and other) compilers. This would be a "general case" solution. Alternatively, a similar option (command line or configuration) to MinTTy will also do the job.
For my specific need, I have an idea for a potential work-around: Run a BASH script - with no startup scripts - that sets my required variable and execs another shell as a login shell.
Define the target of your shortcut file as follows:
C:\cygwin64\bin\mintty.exe /bin/bash -l -c "MSYSTEM=MINGW64 exec -l bash"
This command:
invokes bash directly as a login shell (-l)
passes it a command (-c) that defines the environment variable of interest (MSYSTEM=MINGW64) and then invokes a new copy of bash (exec -l bash), which inherits the existing environment, plus the new definition, but sources the profile(s) again, due to -l
(and prepends - to the executable name reported in $0 (-bash), as would happen if you started Mintty with just -, which is what the regular Cygwin64 Terminal shortcut does).
An alternative is to set the environment variable in Windows first.
[Not an option for the OP] If the environment variable should always have the same value, set it persistently as follows: run sysdm.cpl, go to the Advanced tab, click on Environment Variables... and define variable MSYSTEM as needed.
To define the variable ad-hoc, create a batch file as follows and make the shortcut target that batch file:
#echo off
# Define the env. variable with the desired value.
set "MSYSTEM=MINGW64"
# Invoke Mintty with a login shell, which will now see the env. variable.
# Adjust the path to mintty.exe as needed.
c:\cygwin64\bin\mintty.exe -
Note: Opening the batch file from a shortcut briefly opens a regular console window before opening Mintty, which may be undesired.
A simple helper WSH script, as demonstrated in this answer of mine, can prevent this.
You should just be able to do the same as you do in command prompt. Therefore, you can do:
set VAR=VarContents
Although I already accepted an answer above, I found this link that specifically addresses the second part of my question (Mintty specific) or an alternative way of setting an environment variable before running a command.
The contents of the Windows shortcut can be:
C:\cygwin64\bin\mintty.exe -t "Title" /bin/env "MSYSTEM=MINGW64" /bin/bash -l
(Suggested by Mintty Tips:Setting environment variables.)

Can I set the vim colorscheme from the command line?

I have a hotkey set to automatically create some aliases and install a script whenever I SSH into a server (for my work). Does anyone know of a way to set the vim colo from the command line so I can use it in my hotkey? Thank you!
You can run commands when starting vim:
vim +'colorscheme blue' my_file
See man vim:
-c {command}
{command} will be executed after the first file has been read.
{command} is interpreted as an Ex command. If the {command} contains
spaces it must be enclosed in double quotes (this depends on the shell
that is used). Example: Vim "+set si" main.c
Note: You can use up to 10 "+" or "-c" commands.

Cygwin Terminal and zsh strange characters used in username

Hi I've recently installed zsh using cygwin on my Windows machince but when I type zsh to start this I get the following:
GG#GG-PC ~
$ zsh
\[\e]0;\w\a\]\n\[\e[32m\]\u#\h \[\e[33m\]\w\[\e[0m\]\n\$
On my mac I am using iTerm2 and this is so much easier to setup on here. Also I am having trouble in setting up the aliases and this is becauses its not setup properly in terms of config file where I can set this up in a separate file.
Any ideas how I can resolve?
It looks like zsh is inheriting the value of PS1 from the previous shell. The PS1 environment variable sets the shell prompt, and zsh used a different format for prompt substitutions than other shells. Try entering the following command after you start zsh:
PS1=$'%{\e]0;%d\a%}\n%F{green}%n#%m %F{yellow}%d%f\n%# '
If that works, add that line to your ~/.zshrc file.
That's also probably a good place to put your aliases.
There might be an issue because you launch zsh from bash actually and not cygwin.
One thing you can do is to launch zsh as the starting shell of mintty (the window that wraps your shell)
Create a shortcut with this inside:
c:\<cygwin-folder>\bin\mintty.exe -i /Cygwin-Terminal.ico /usr/bin/zsh --login -
Yo need to update .zshrc with your required theme and then
source .zshrc

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via
set -o vi
or
set -o emacs
What is actually going on here? I've been reading a book where it claims the bash shell uses either of these editors for the input to the shell itself, but I thought it may have used readline.
Bash is still using readline. Readline uses either emacs or vi mode and setting the mode switches between the various editor modes. You can check the lib/readline folder in the base source code to see the various key bindings.
According to the man page BASH_BUILTINS(1) (on Fedora 8):
set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
... (skipping all the single letter options)
-o option-name
The option-name can be one of the following:
...
emacs Use an emacs-style command line editing interface. This is
enabled by default when the shell is interactive, unless the
shell is started with the --noediting option.
...
vi Use a vi-style command line editing interface.
I interpret that to mean that bash is directly interpreting the commands for line editing. This option simply sets which command set to use. See the man page for readline(3).
It uses the keystrokes that are familiar to users of one of those editors to edit the command line.
Readline is the facility that provides that feature to Bash and other programs.
From man bash:
READLINE
This is the library that handles reading input when using an interac‐
tive shell, unless the --noediting option is given at shell invocation.
Line editing is also used when using the -e option to the read builtin.
By default, the line editing commands are similar to those of emacs. A
vi-style line editing interface is also available. Line editing can be
enabled at any time using the -o emacs or -o vi options to the set
builtin (see SHELL BUILTIN COMMANDS below). To turn off line editing
after the shell is running, use the +o emacs or +o vi options to the
set builtin.
From what I know, readline is what provides the line-editing functionality for bash.
One proviso: when you press v in vi command mode, you get the full blown vi editor to edit your command line.
From man bash:
READLINE
This is the library that handles reading input when using an interactive shell, unless the --noediting option is given at shell invocation. By default, the line editing commands are similar to those of emacs. A vi-style line editing interface is also available. To turn off line editing after the shell is running, use the +o emacs or +o vi options to the set builtin.
When the shell presents you with a prompt (unless you're in non-editing mode), you're already using readline. You'll either be in emacs mode or vi insert mode (which is why you can just use ESC to get back to vi command mode).

Resources