Get last argument of last command on ESC-. ZSH Vim mode - bash

I want to have the same function of Bash+emacs_mode on Zsh+vi_mode, when you type ESC+. you get the last argument of the last command under the cursor. How can I get it on ZSH?

You can use the same widget (insert-last-word) in both viins and emacs mode; it just isn't bound to a key by default in viins mode.
Run the following in the current shell (and add it to .zshrc for it to take effect in future shells).
bindkey -M viins '\e.' insert-last-word

Related

aliases are not stored in zsh history

Whenever I execute a command using an alias, this command is not stored in the shell's command history.
So if I run history these commands do not appear in the list.
Nor do they appear when I press CTRL + r for reverse searching the command history.
When I press the keyboard's arrow up for scrolling through the last commands, I will see an aliased command only if it was the last command I ran. Other aliased commands are will not be displayed.
For example:
$ cd my-repo
$ gs # an alias to git status
$ history
Outputs the following:
2374 cd my-repo
(the gs command is not displayed)
A few notes:
gs is only an example. The issue is far more annoying in more complex commands since I have to retype them all over again instead of executing them from history (e.g. k get pods | grep <pod_name>, where k=kubectl).
gs is defined so: alias gs=' git status'.
I also have a few functions in ~/.alias, e.g.:
mkcd () {
mkdir -pv $1
For some reason, mkcd (or any other function in the alias file) is included in the history.
I do not mind if it prints out gs or expands to git status, I'll take any of the two...
I am using zsh with oh-my-zsh on macOS (Monterey). My shell aliases are defined in ~/.alias which is sourced in ~/.zshrc (source ~/.alias).
This happens both in iTerm2 and in the default Mac terminal.
Thank you for taking the time to help :-)
I will assume that your example alias is exactly what you have in your ~/.alias file.
So you have aliases like this (notice the space character in front of git command):
alias gs=' git status'
There is an shell option called HIST_IGNORE_SPACE which is doing exactly what you are experiencing - in short it will not add command to the history when it starts with space character. Example:
echo 'This command will make it to the history.'
echo 'This poor command will be forgotten.'
You can check your current options using setopt or specifically:
setopt | grep 'histignorespace'
So there are two ways how you can fix this - either by fixing your aliases to not start with space or, If you really don't want this functionality at all, by unsetting it in your ~/.zshrc like this:
unsetopt HIST_IGNORE_SPACE

How to get Bash's revert-line in zsh

Since macOS changed to zsh as default, I try to update my keybindings for it.
In bash I could use the following command:
bind '"\C-a": revert-line' # Revert/resets the changed history line while you are on it with the the cursor
I do not find any function like that on zsh.
bindkey "^a" what-to-put-here
Do you know how to accomplish it with zsh?
revert-line is a function provided by readline. While, ZSH doesn't depend on readline. ZSH has its own editor in command line called ZLE(ZSH Line Editor).
There doesn't seem to be a reset all function in the ZLE builtins. Correct me if I'm wrong.
undo may be an alternative for you.
undo
Incrementally undo the last text modification. When called from a user-defined widget, takes an optional argument indicating a previous state of the undo history as returned by the UNDO_CHANGE_NO variable; modifications are undone until that state is reached, subject to any limit imposed by the UNDO_LIMIT_NO variable.
bindkey "^a" undo

Paste bash command, but make sure it doesn't run

Sometimes when you ctrl-v with bash it will run the command even though you didn't intend to run it yet - is there a way to paste a command into the bash shell / terminal making sure you don't actually run any of the command(s)?
if you could set what was on the terminal prompt programmatically, you could do this with bash on MacOS:
export BASH_PROMPT="$(pbpaste)"
which ties into my other question that I just asked:
How to change the value that's in the prompt
There is a Readline variable:
enable-bracketed-paste
When set to On, Readline will configure the terminal in a way that will enable it to insert each paste into the editing buffer as a single string of characters, instead of treating each character as if it had been read from the keyboard. This can prevent pasted characters from being interpreted as editing commands. The default is off.
To turn this on, put something like
set enable-bracketed-paste on
into your ~/.inputrc.
This was introduced in Bash 4.4 / Readline 7.0.
Use ^X^E aka Ctrl+X Ctrl+E in bash to open your $EDITOR for command entry.
Paste and/or edit as much as you want, across as many lines as you want. When you're done, save and exit, and bash will run it.
(In vi mode, the shortcut is v)

How to change shortcut for iTerm2 clear lines?

Ctrl + l is the default shortcut for clear lines in iTerm2, I want to change it to Cmd + l, but can't find this action:
BTW I'm using zsh.
Ctrl-L or "form feed" is part of the ANSI/VT100 protocol (http://wiki.bash-hackers.org/scripting/terminalcodes), it's not specific to iTerm2.
Your best best is to use Applescript to send Ctrl-L to the terminal when Cmd-L is pressed.
This is an zsh binding is not an iTerm binding. Ctrl+l is the default binding for the clear-screen widget in zsh. The fact that it also works in bash (and maybe other shells) is merely convention. In bash - or rather readline, bash's command line editor - it is the default binding for a command that is also named clear-screen.
Generally, you can change a key binding in zsh with the command bindkey KEYSEQUENCE WIDGET. Unfortunately not all modifiers might be supported by iTerm2 for use with the shell. You can test, whether it is supported by running cat -v and then pressing the desired key combination. If Cmd+l is supported, than the output shown should be more than just "l". If it something more or other than just "l", then you can use the output to bind it. For example if cat -v shows "^[l" than you can bind it with bindkey '^[l' clear-screen and if you want to remove the default binding, you can do so with bindkey -r '^l'.

How to mark the start of the shell session from Vim?

When I am using vim-figitive it often shells out for commands like git push and so on.
Every time it opens a shell I see some leftovers from the previous session, something like:
Press ENTER or type command to continue
It sets me off every time because I can't figure out if this is the output from the current session or the previous one.
Is there a way to mark it with some sort of line every time I shell out from vim? Something like this in .zshrc would do it I think:
if [ -n $VIM ]; then
echo "----------------------------------------------------------------------------------"
fi
Update
It works when I run :sh, but not when I run commands from vim-fugitive. Is there is a way to intercept system() calls or whatever it is using?
From #tpope:
My recommendation would be to look into disabling the "alternate
screen" for your terminal emulator (or terminal multiplexor). That's
what I do.
Just add the following to tmux.conf
# tmux.conf
# For tmux , the alternate-screen option defaults to on
set-window-option -g alternate-screen off

Resources