final first post
This commit is contained in:
		@@ -50,8 +50,7 @@ which is very helpful when you need it
 | 
			
		||||
3. Support sending `ctrl` characters like `ctrl d`, `ctrl c` etc,
 | 
			
		||||
 | 
			
		||||
```vim
 | 
			
		||||
vim9script
 | 
			
		||||
def SendRangeToTerminal(start_line: number, end_line: number, _ = 0)
 | 
			
		||||
def g:SendRangeToTerminal(start_line: number, end_line: number)
 | 
			
		||||
    const terms = term_list()
 | 
			
		||||
    if terms->empty()
 | 
			
		||||
        echomsg "No Terminal windows found"
 | 
			
		||||
@@ -60,14 +59,14 @@ def SendRangeToTerminal(start_line: number, end_line: number, _ = 0)
 | 
			
		||||
    var line_num = start_line
 | 
			
		||||
    for line in getline(start_line, end_line)
 | 
			
		||||
        line_num += 1
 | 
			
		||||
        const spl_cmd = line->matchlist('\vVIMST (sleep|ctrl) ([0-9]+|[a-z])?')
 | 
			
		||||
        const spl_cmd = line->matchlist('\vVIMST (sleep|ctrl) ([0-9]+|[a-z])')
 | 
			
		||||
        if !spl_cmd->empty()
 | 
			
		||||
            const [_, cmd, arg1; _] = spl_cmd
 | 
			
		||||
            if cmd == "sleep"
 | 
			
		||||
                timer_start(arg1->str2nr(), funcref('SendRangeToTerminal', [line_num, end_line]))
 | 
			
		||||
                timer_start(str2nr(arg1), (_) => g:SendRangeToTerminal(line_num, end_line))
 | 
			
		||||
                return
 | 
			
		||||
            elseif cmd == "ctrl"
 | 
			
		||||
                terms[0]->term_sendkeys(nr2char(arg1->char2nr() - 96))
 | 
			
		||||
                terms[0]->term_sendkeys(nr2char(char2nr(arg1) - 96))
 | 
			
		||||
                continue
 | 
			
		||||
            endif
 | 
			
		||||
        endif
 | 
			
		||||
@@ -75,7 +74,7 @@ def SendRangeToTerminal(start_line: number, end_line: number, _ = 0)
 | 
			
		||||
    endfor
 | 
			
		||||
enddef
 | 
			
		||||
 | 
			
		||||
command -range -bar SendToTerm :call <SID>SendRangeToTerminal(<line1>, <line2>)
 | 
			
		||||
command -range -bar SendToTerm :call g:SendRangeToTerminal(<line1>, <line2>)
 | 
			
		||||
vnoremap <silent><leader>s :SendToTerm<CR>
 | 
			
		||||
nnoremap <silent><leader>s :SendToTerm<CR>
 | 
			
		||||
```
 | 
			
		||||
@@ -88,30 +87,30 @@ Wouldn't it be nice to just double-click commands with mouse? Like a simple GUI!
 | 
			
		||||
nnoremap <silent><2-LeftMouse> :SendToTerm<CR>
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Or just `Enter`?
 | 
			
		||||
Or just hit `Enter`?
 | 
			
		||||
 | 
			
		||||
```vim
 | 
			
		||||
nnoremap <buffer> <CR> :SendToTerm \| norm j<CR>
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Of course, mapping `Enter` for any file is a bad idea. So lets just map in our cheat file
 | 
			
		||||
 | 
			
		||||
Automatically add those mappings for `cheat.sh`
 | 
			
		||||
```vim
 | 
			
		||||
autocmd BufNewFile,BufRead cheat.sh {
 | 
			
		||||
    nnoremap <buffer> <silent><2-LeftMouse> :SendToTerm<CR>
 | 
			
		||||
    nnoremap <buffer> <CR> :SendToTerm \| norm j<CR>
 | 
			
		||||
}
 | 
			
		||||
autocmd BufNewFile,BufRead cheat.sh nnoremap <buffer> <CR> :SendToTerm \| norm j<CR>
 | 
			
		||||
autocmd BufNewFile,BufRead cheat.sh nnoremap <buffer> <silent><2-LeftMouse> :SendToTerm<CR>
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Demo
 | 
			
		||||
 | 
			
		||||
{{< asciinema key="vimstt" >}}
 | 
			
		||||
 | 
			
		||||
### Getting the scripts
 | 
			
		||||
### Using
 | 
			
		||||
 | 
			
		||||
### What about neovim/tmux/screen?
 | 
			
		||||
Copying above snippets to your vimrc should work in a recent vim. Checked in
 | 
			
		||||
ubuntu 22.04 and archlinux. Git: [repo](https://gitea.balki.me/balki/vimfun/src/branch/main/stt/stt8.vim)
 | 
			
		||||
 | 
			
		||||
I am not the only one who thought about this. See
 | 
			
		||||
[vim-slime](https://github.com/jpalardy/vim-slime) since 2007. However it does
 | 
			
		||||
not support adding a sleep or sending arbitrary `ctrl` characters without
 | 
			
		||||
additional mappings
 | 
			
		||||
### neovim/tmux/screen
 | 
			
		||||
 | 
			
		||||
Since neovim uses a different terminal API, above snippets don't work in
 | 
			
		||||
neovim. [vim-slime](https://github.com/jpalardy/vim-slime) plugin (available
 | 
			
		||||
since 2007!) supports different types of terminal. However it does not support
 | 
			
		||||
adding delay and `ctrl` characters in text.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user