Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert new lines when editing some script with fish's built-in editor?

Tags:

fish

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

like image 355
Freewind Avatar asked Dec 23 '22 08:12

Freewind


2 Answers

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:

option to send escape

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.

like image 123
ridiculous_fish Avatar answered May 14 '23 19:05

ridiculous_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.

like image 26
Zanchey Avatar answered May 14 '23 20:05

Zanchey