Search code examples
fish

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


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


Solution

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