How to insert new lines when editing some script with fish's built-in editor? - 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

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.

Related

Find \n and replace with \r\n in VS Code

I have a legacy system that only accepts Windows \r\n and I want to edit a file in VS Code that has just \n to have \r\n.
I'm trying to use a Regex replace:
But this puts literal \r in instead of the whitespace char.
I've tried putting a newline in the replacement using SHIFT+ENTER:
But this just puts in \n.
How do I change the line feed chars used and save the file in VS Code?
There's the text "LF" in the bottom bar on the right, click on it and select "CRLF". Or press Ctrl+Shift+P and enter Change End of Line Sequence.
No idea why your approach doesn't work. Nor does \x0D or \15 get recognized. I'd call it a bug.
For multiple files, on Linux, I'd do it outside of the editor, e.g., with
find somedir -name '*.someext' -exec perl -pi -e 's/\n/\r\n/' {} +
Just press Ctrl+H and select regex replace. Then start input:
Find what: ^\n
Replace with: \r\n
You can try this one
In the local searchbox (Ctrl + F) you can insert newlines by pressing Ctrl + Enter.
https://www.graef.io/find-and-replace-with-a-newline-in-visual-studio-code/
Visual Studio Code provides an option to Select End of Line Sequence in its taskbar on bottom right:
When clicked, it'll provide an option to choose between CRLF (Carriage return and Line feed: Windows default) and LF (Line feed alone: Linux supported):
Make sure to save the file once the EOL sequence is changed.

How to create a key binding that inserts text in the Fish shell

I would like to save typing in the Fish shell by binding a key to a text. When I press the key, the text should be inserted into the shell. The effect should be the same as typing that text.
One of the problems is that the text should not be executed, just inserted. The closest I came is this experiment where the text "whoami" is inserted when I press Alt+G:
bind \eg "echo -n whoami"
However, when I press enter the command is not executed, so the effect is not the same as typing the text directly in the shell.
You want to modify the commandline, which incidentally is possible with the commandline builtin.
bind \eg "commandline -i whoami"

How do you do multiple line entry in the command line REPL of SuperCollider?

Just as the title says, How do you do multiple line entry in the command line REPL of SuperCollider? For example, in Haskell, you use :{ and :} to open and close multiple line entry in ghci.
If you're using sclang with the -i flag (meaning e.g. sclang -i something), the keycode to execute previously entered text is esc, followed by a newline. For example:
~a = {
"test".postln;
}^[
~a.();^[
outputs: test
This works file if you're driving sclang from an IDE or another scripting context (this is used for sclang's unit tests). If you're using the repl, it appears that there ISN'T a way to do multiline entries - the repl uses readline which doesn't have multiline support out of the box. This should probably be filed as a bug.

Clear everything after prompt in fish shell?

I want to make keybinding that simply clears everything I entered after prompt and till the end. The same behavior as what Ctr+c does, but without appending ^C character to the end of current line and newline. Is it doable somehow?
You probably want Ctrlu and/or Ctrlk
Ctrl-u kills characters from your cursor to the start of entry (the prompt)
Ctrl-k kills characters from your cursor to the end of the line.
The deleted characters can be pasted (yanked) with Ctrly
Try this:
function clear_to_end
commandline (commandline --cut-at-cursor)
end
bind \cc clear_to_end
This sets the command line to the current command line, truncated at the cursor.

Vim: from command line, go to end of file and start editing?

I can cause vim to position the cursor at the last line of a file by invoking it with an argument of +:
vi + myfile # "+" = go to last line of file
How can I do this, and then go into append mode, so that the user can start typing at the end of the file?
Something similar to
emacs myfile --eval "(goto-char (point-max))"
One solution could be to use the + parameter to pass a command to execute after reading the file. Vim can take up to 10 commands this way so you can use:
vim "+norm Go" "+startinsert" myfile
The first command norm Go will go to the last line and add a new one.
The second command will start insert mode allowing the user to type in the last line.
Note This solution creates a new line at the end of the file. If you want to edit the end of the last line without creating a new one you could use something like that :
vim "+norm G$" "+startinsert" myfile
But If you do this and your last line already contains some text, you will start inserting text before the last character. I don't know an equivalent to startinsert like startappend so I don't know how to solve this.
The equivalent to startinsert like startappend is startinsert! according to this site. So the answer could be
vim "+norm G$" "+startinsert!" myfile