final first post

This commit is contained in:
Balakrishnan Balasubramanian 2023-07-26 15:18:06 -04:00
parent c14a238f5f
commit c91d595938
3 changed files with 25 additions and 20 deletions

View File

@ -1,3 +1,3 @@
baseURL: http://blog.balki.me/ baseURL: http://balki.me/
languageCode: en-us languageCode: en-us
title: Balki's Blog title: Balki's Blog

View File

@ -50,8 +50,7 @@ which is very helpful when you need it
3. Support sending `ctrl` characters like `ctrl d`, `ctrl c` etc, 3. Support sending `ctrl` characters like `ctrl d`, `ctrl c` etc,
```vim ```vim
vim9script def g:SendRangeToTerminal(start_line: number, end_line: number)
def SendRangeToTerminal(start_line: number, end_line: number, _ = 0)
const terms = term_list() const terms = term_list()
if terms->empty() if terms->empty()
echomsg "No Terminal windows found" echomsg "No Terminal windows found"
@ -60,14 +59,14 @@ def SendRangeToTerminal(start_line: number, end_line: number, _ = 0)
var line_num = start_line var line_num = start_line
for line in getline(start_line, end_line) for line in getline(start_line, end_line)
line_num += 1 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() if !spl_cmd->empty()
const [_, cmd, arg1; _] = spl_cmd const [_, cmd, arg1; _] = spl_cmd
if cmd == "sleep" if cmd == "sleep"
timer_start(arg1->str2nr(), funcref('SendRangeToTerminal', [line_num, end_line])) timer_start(str2nr(arg1), (_) => g:SendRangeToTerminal(line_num, end_line))
return return
elseif cmd == "ctrl" elseif cmd == "ctrl"
terms[0]->term_sendkeys(nr2char(arg1->char2nr() - 96)) terms[0]->term_sendkeys(nr2char(char2nr(arg1) - 96))
continue continue
endif endif
endif endif
@ -75,7 +74,7 @@ def SendRangeToTerminal(start_line: number, end_line: number, _ = 0)
endfor endfor
enddef 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> vnoremap <silent><leader>s :SendToTerm<CR>
nnoremap <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> nnoremap <silent><2-LeftMouse> :SendToTerm<CR>
``` ```
Or just `Enter`? Or just hit `Enter`?
```vim ```vim
nnoremap <buffer> <CR> :SendToTerm \| norm j<CR> 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 ```vim
autocmd BufNewFile,BufRead cheat.sh { autocmd BufNewFile,BufRead cheat.sh nnoremap <buffer> <CR> :SendToTerm \| norm j<CR>
nnoremap <buffer> <silent><2-LeftMouse> :SendToTerm<CR> autocmd BufNewFile,BufRead cheat.sh nnoremap <buffer> <silent><2-LeftMouse> :SendToTerm<CR>
nnoremap <buffer> <CR> :SendToTerm \| norm j<CR>
}
``` ```
### Demo ### Demo
{{< asciinema key="vimstt" >}} {{< 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 ### neovim/tmux/screen
[vim-slime](https://github.com/jpalardy/vim-slime) since 2007. However it does
not support adding a sleep or sending arbitrary `ctrl` characters without Since neovim uses a different terminal API, above snippets don't work in
additional mappings 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.

View File

@ -19,6 +19,10 @@
<style> <style>
{{- block "styles" . -}} {{- block "styles" . -}}
{{- end -}} {{- end -}}
div.highlight pre {
{{/* code highlighting */}}
padding: 1rem;
}
</style> </style>
<script src="/assets/bootstrap.min.js" defer></script> <script src="/assets/bootstrap.min.js" defer></script>
@ -45,6 +49,8 @@
{{ block "main" . }} {{ block "main" . }}
{{ end }} {{ end }}
</main> </main>
<hr>
<div style="height: 300px;"><!-- This space is intentionally left blank --></div>
</div> </div>
</body> </body>