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

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

Related

Is there a way to edit last Octave command and /or script typed in Octave CLI

typing and executing a single line command in octave cli is simple.
for example a=1.
If one wants to edit this command and execute it again it is possible by navigating the history with the up/down keys.
But when executing a multi line command-script, one can still navigate to a single line and edit it, but how to execute the all script again, without going line by line in the history and "enter" the line?
for example:
for i=1:6
a(i) = i;
end
Is there a way to open the all script in an editor, edit, and re-execute it?
Yes there is, via the history command.
E.g. history -q 3 will display the last 3 commands -- the -q switch means do not prepend line numbers, such that the output is copy-pasteable.
You can also use the -w switch to redirect the output to a file, which you could then modify in your editor and call as a script, as you suggest.
If you're using the GUI, you can also use the history pane to highlight the lines you're interested in (in the order that you want them!), and paste directly into the editor window.

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.

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

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.

How to open a file and select/highlight several lines on Sublime from the command line?

I know subl myfile.txt:5 would open “myfile.txt” on line 5. I however want to be able to, from the command line, open a file with say lines 5,9,15 highlighted or selected. I know adding –command should enable me to do that, but how? What would the command be?
There's no built-in command that I know of that can do this, but one can easily create one.
(Technically, it could be done using the bookmarks functionality from the Default package, and the built-in "Expand Selection to Line" functionality. However, experience shows that it would be better and more reliable to write a command in ST specifically for this purpose.)
In ST:
from the Tools menu -> Developer -> New Plugin...
select all and replace with the following
import sublime
import sublime_plugin
class SelectSpecificLinesCommand(sublime_plugin.TextCommand):
def run(self, edit, lines):
self.view.sel().clear()
for line in lines:
position = self.view.text_point(int(line) - 1, 0) # internally, line numbers start from 0
self.view.sel().add(self.view.line(position))
save it, in the folder ST recommends (Packages/User/) as something like select_lines.py (file extension is important).
subl myfile.txt
subl --command "select_specific_lines { \"lines\": [5, 9, 15] }" (this style of escaping the quotes for JSON strings works from the Windows Command Prompt and Linux's Bash)
Why did I specify the command on a separate line / call to subl? Because of these 2 caveats:
ST must already be running, otherwise commands specified on the command line may not get executed, because the plugins haven't loaded yet.
the command could get executed before the file is loaded.
Arguably, point 2 could still happen with multiple invocations of subl, but hopefully it is less likely. There is an open issue on the ST bug tracker for better command line command handling: https://github.com/SublimeTextIssues/Core/issues/1457.