Run (neo)vim motions, both insert and normal mode headlessly through commands with stdin - neovim

I'm creating a vim game where the purpose is to use vim motions to alter a buffer. It's a web game so I would have to emulate the inputs being sent from the client to the server, one challenge is going from insert mode to normal mode somehow. I'm running neovim behind an API, and this is how I'm able to run things from the app/frontend:
$ echo "foo" | nvim --headless --clean +"norm l" +"%print" +"echo getpos('.')" +q! -
I'm echoing "foo" into vim and using the norm command to alter it.
Inside the +"norm l" command I'm able to input vim motions, in this case moving the cursor right one character.
It works well, however, it can only handle things through normal mode. If I were to input ifoo<Esc>, which for me would translate to i, foo and then hitting the escape key, <Esc> is treated as an literal. Same thing with <C-c> and jj.
$ echo "foo" | nvim --headless --clean +"norm ifoo<Esc>" +"%print" +"echo getpos('.')" +q! -
foo<Esc>foo
[0, 1, 8, 0]
Is there a way to do this that I'm not seeing?

Related

Fish is not returning to new line after executing bind

I'm binding Ctrl+f to fg so I can Ctrl+z and Ctrl+f back into vim. However after two times doing the combo twice, it ruins the terminal:
user#pc:~$ vim
Job 1, 'vim' has stopped
user#pc:~$ Send job 1, “vim” to foreground
Job 1, 'vim' has stopped
^F▉
Cursor is now not on a line (which begins with user#pc) and executing any command show it as string (^Fas example).
Pressing enter fixes this, but there should be a better solution.
Edit: using bind \cf 'fg; commandline -f repaint' shows the PS1, but it's still in text mode, meaning pressing Ctrl+f, doesn't run fg but outputs ^F.
Solution was to instad of using:
bind \cf 'fg'
use:
bind \cf 'fg; commandline -f execute'
It ocassionaly does an extra enter, but whatever.

Bind Ctrl-C in fish vi-mode to switch from insert mode to normal mode

I would like to use Ctrl-C to go from insert to normal mode in fish vi-mode, as I do in vim. I'm fairly new to fish and couldn't get it to work, though I tried this in my config.fish:
bind -M insert \cc set fish_bind_mode 'default'
Strangely I am able to go from Visual to Normal mode with Ctrl-C.
Thanks
Binding [ctrl-C] would require that you also change the stty intr character. Something that is currently impossible from within fish due to how it handles tty modes. You could, however, change it before starting fish. That would then free the character to be bound as you wish. Though you'll need to do this to get the correct behavior:
bind -M insert \cc 'set fish_bind_mode default; commandline -f repaint'

Emacsclient crashes when evaluating window functions

To not bore anyone here with specifics, whenever I evaluate an expression similar to this one:
emacsclient -t -e '(set-buffer *scratch*)'
the client will flash up on the terminal and crash.
This seems to be happening with all window-changing functions. Is the client not supposed to work like that? Running this in a normal emacs session does not cause this problem.
You're also using -t, but I'm not exactly sure why.
Is your emacs running in another tty session?
Or is your emacs running in windowing mode (e.g. on X Windows)?
If I have emacs running in windowing mode and I run the following command from another xterm window, then everything works exactly as I would expect it to:
emacsclient -c -e '(set-buffer "*scratch*")
Note in particular the -c option, and the fact that the buffer name is a string and so must be enclosed in double-quotes.

Set size of a cygwin terminal window

I have a small perl script, which is executed in a cygwin terminal and prints out a formatted table.
On Default window size, cygwin will insert an line break if the text gets too long and thereby destroy the format of my table.
Is there a way from my perl script to set the cygwin window to a bigger size to avoid that kind of problem?
If you happen to be running this from a shortcut where you could add flags to the mintty command, you can set the size then. The benefit is that it looks smoother without the twitchy resize.
$ /cygdrive/c/tools/cygwin/bin/mintty.exe --help
Usage: mintty [OPTION]... [ PROGRAM [ARG]... | - ]
Start a new terminal session running the specified program or the user's shell.
If a dash is given instead of a program, invoke the shell as a login shell.
Options:
-c, --config FILE Load specified config file (cf. -C or -o ThemeFile)
-e, --exec ... Treat remaining arguments as the command to execute
-h, --hold never|start|error|always Keep window open after command finishes
-p, --position X,Y Open window at specified coordinates
-p, --position center|left|right|top|bottom Open window at special position
-p, --position #N Open window on monitor N
-s, --size COLS,ROWS Set screen size in characters (also COLSxROWS)
-s, --size maxwidth|maxheight Set max screen size in given dimension
-t, --title TITLE Set window title (default: the invoked command) (cf. -T)
-w, --window normal|min|max|full|hide Set initial window state
-i, --icon FILE[,IX] Load window icon from file, optionally with index
-l, --log FILE|- Log output to file or stdout
--nobidi|--nortl Disable bidi (right-to-left support)
-o, --option OPT=VAL Set/Override config file option with given value
-B, --Border frame|void Use thin/no window border
-R, --Reportpos s|o Report window position (short/long) after exit
--nopin Make this instance not pinnable to taskbar
-D, --daemon Start new instance with Windows shortcut key
-H, --help Display help and exit
-V, --version Print version information and exit
See manual page for further command line options and configuration.
If you are using mintty as your terminal emulator (it has been the default
terminal emulator for Cygwin for the past couple of years), you can use ANSI
escape codes to manipulate the terminal.
You can test this by running the following snippet of Perl code to change the size of your terminal emulator window:
# If terminal supports ANSI escape sequences
$lines = 80;
$columns = 100;
print "\e[8;$lines;${columns}t";
Note: This doesn't work if run while in a screen window and I don't know why. According to the screen man page, this escape sequence should be supported.
Explanation
The syntax of ANSI escape sequences isn’t the easiest to read but here’s the
documentation that provides the basis of the above sequence.
The \e prints an Escape character which begins the ANSI escape sequence.
This is also known as the Control Sequence Introducer (CSI).
The specific sequence ending with t comes from this List of xterm control
sequences
CSI Ps ; Ps ; Ps t
Window manipulation (from dtterm, as well as extensions).
These controls may be disabled using the allowWindowOps
resource. Valid values for the first (and any additional
parameters) are:
…
Ps = 8 ; height ; width -> Resize the text area to given
height and width in characters. Omitted parameters reuse the
current height or width. Zero parameters use the display's
height or width.
You don't even need Perl, you can do the same in Bash:
echo -en "\e[8;35;100t";
Or why not a script:
#!/bin/bash
# minsize - A TTY re-size escape sequence for use with mintty Cygwin
# Usage: minsize <width> <height>
WIDTH=$1
HEIGHT=$2
echo -en "\e[8;${HEIGHT};${WIDTH}t";
Note, that on other *nixes there is ttysize available.

edit commandline with $EDITOR in tcsh

Today's Daily Vim says this:
Assuming you're using the bash shell, the following can be helpful when composing long command lines.
Start typing on the command line and then type Ctrl-x Ctrl-e, it should drop you into your system's default editor (hopefully Vim) and allow you to edit the command line from there. Once finished, save the command line, and bash will run the command.
Is there any way to do this in tcsh?
A little explanation for the uninitiated.
bindkey -v
puts you in vi-mode (oh yeah!)
and hitting v from there would take you to $EDITOR -- and all is good with the world from there on.
Hmmm... IIRC, tcsh uses a command called bindkey. Try bindkey -v at the command line. Then hit escape followed by v. It's been a while since I used tcsh so the details are a bit fuzzy. When in doubt, Google it.