When inputing multiline script in fish shell, e.g. I have input these script
$ for file in *.txt
echo $file
end
and my caret is after the word end
. Now I want to insert a line before it, make it like:
$ for file in *.txt
echo $file
echo "hello" // I want to insert this line
end
But I found if I move my caret up and after echo $file
, and press enter
(or cmd/option/ctrl+enter
), it just run the entire script without inserting a new line. I have to copy them to another editor and copy back after editing.
Is there any way to do it?
Update:
I just uploaded a screen recording https://asciinema.org/a/i7pOWhpdXUu0RLVOAMjVJTXbn. In the recording, I moved my caret up to after echo
and pressed option + enter
, but it executed the script instead of inserting a new line
fish binds escape + newline to unconditionally insert a newline. On a Mac, you would typically press option + return. However Mac terminal emulators do not send an escape-newline by default. With iTerm2 you can request that option acts as escape, under Preferences->Profiles->Keys:
Now the binding will be active and option-return will unconditionally insert a newline.
(You could instead add a key mapping for just this case if you prefer.)
You can confirm what the terminal receives from the emulator with fish_key_reader
which is installed alongside fish.
In the default bindings, Alt-Enter will always insert a new line:
> bind|fgrep \\n
bind \e\n commandline\ -i\ \\n
bind \e\r commandline\ -i\ \\n
...
Depending on your system configuration, the Enter/Return key may send either a newline character (\n
) or a carriage-return character (\r
), so that's why there's two entries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With