From c91d5959389618d38befc60bd4d7d60bd9c50037 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Wed, 26 Jul 2023 15:18:06 -0400 Subject: [PATCH] final first post --- config.yaml | 2 +- content/posts/vim-stt.md | 37 ++++++++++++++++++------------------ layouts/_default/baseof.html | 6 ++++++ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/config.yaml b/config.yaml index 9e75e08..45ec734 100644 --- a/config.yaml +++ b/config.yaml @@ -1,3 +1,3 @@ -baseURL: http://blog.balki.me/ +baseURL: http://balki.me/ languageCode: en-us title: Balki's Blog diff --git a/content/posts/vim-stt.md b/content/posts/vim-stt.md index 5780093..8a49cd1 100644 --- a/content/posts/vim-stt.md +++ b/content/posts/vim-stt.md @@ -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 SendRangeToTerminal(, ) +command -range -bar SendToTerm :call g:SendRangeToTerminal(, ) vnoremap s :SendToTerm nnoremap s :SendToTerm ``` @@ -88,30 +87,30 @@ Wouldn't it be nice to just double-click commands with mouse? Like a simple GUI! nnoremap <2-LeftMouse> :SendToTerm ``` -Or just `Enter`? +Or just hit `Enter`? ```vim nnoremap :SendToTerm \| norm j ``` -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 <2-LeftMouse> :SendToTerm - nnoremap :SendToTerm \| norm j -} +autocmd BufNewFile,BufRead cheat.sh nnoremap :SendToTerm \| norm j +autocmd BufNewFile,BufRead cheat.sh nnoremap <2-LeftMouse> :SendToTerm ``` ### 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. diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 2821af4..55f2184 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -19,6 +19,10 @@ @@ -45,6 +49,8 @@ {{ block "main" . }} {{ end }} +
+