add cast, tags
This commit is contained in:
		@@ -1,24 +1,30 @@
 | 
			
		||||
---
 | 
			
		||||
title: Vim Send To Terminal
 | 
			
		||||
date: 2023-07-15T20:18:03-04:00
 | 
			
		||||
asciinema: true
 | 
			
		||||
tags:
 | 
			
		||||
    - vim
 | 
			
		||||
categories:
 | 
			
		||||
    - development
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
### Semi automatic scripts with vim `:terminal`
 | 
			
		||||
 | 
			
		||||
<!--more-->
 | 
			
		||||
 | 
			
		||||
For a long time, I used the below to send current line to vim's `:terminal`
 | 
			
		||||
Sometimes, fully automating a task is not worth the effort. So I end up running
 | 
			
		||||
a set of commands usually from a cheat sheet text file slightly modifying the
 | 
			
		||||
arguments each time. I used the below in vim to send line under cursor to vim's
 | 
			
		||||
`:terminal` open in a split 
 | 
			
		||||
```vim
 | 
			
		||||
:call term_list()[0]->term_sendkeys(getline('.') .. "\<CR>")
 | 
			
		||||
```
 | 
			
		||||
To send another line `@:` and then for every other line `@@`.  This works because, last
 | 
			
		||||
command run is stored in `register :` and the last macro executed using `@` is
 | 
			
		||||
stored in `register @`.
 | 
			
		||||
 | 
			
		||||
This is very useful if you have a text file with complex shell commands. To run
 | 
			
		||||
again, `@:` and then again, `@@`. This works because, last command run is
 | 
			
		||||
stored in `register :` and the last macro executed using `@` is stored in
 | 
			
		||||
`register @`. And then to run another day, do `:call te<UP arrow>` to recall
 | 
			
		||||
from vim's command history.
 | 
			
		||||
 | 
			
		||||
Since this was very useful, I then wrapped it in a function and added a mapping
 | 
			
		||||
To do the same another day, `:call te<UP arrow>` to recall from vim's command
 | 
			
		||||
history. Or better, add a function and mapping.
 | 
			
		||||
 | 
			
		||||
```vim
 | 
			
		||||
def SendToTerminal()
 | 
			
		||||
@@ -32,14 +38,16 @@ enddef
 | 
			
		||||
nnoremap <silent><leader>s call SendToTerminal()<CR>
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Adding more features
 | 
			
		||||
### More cool features
 | 
			
		||||
 | 
			
		||||
So far good for shell commands. But when working with python, had to send a
 | 
			
		||||
block of functions to the `ipython` shell. So added support for range of lines.
 | 
			
		||||
But then when sending a range of lines to shell, sometimes there had to be a
 | 
			
		||||
small delay (sleep) between commands so that the previous command can complete
 | 
			
		||||
and not eat the rest of commands. Then finally added support for sending `ctrl`
 | 
			
		||||
characters like `ctrl c`, `ctrl l`
 | 
			
		||||
For most use-cases that was enough. However added a few more nice features
 | 
			
		||||
which is very helpful when you need it
 | 
			
		||||
 | 
			
		||||
1. Support sending a range of lines (visual selection) at a time. E.g. Send a
 | 
			
		||||
   function block to a python shell
 | 
			
		||||
2. Add a delay between lines in milliseconds. This is useful when the previous
 | 
			
		||||
   command reads from standard input and takes some time to complete
 | 
			
		||||
3. Support sending `ctrl` characters like `ctrl d`, `ctrl c` etc,
 | 
			
		||||
 | 
			
		||||
```vim
 | 
			
		||||
vim9script
 | 
			
		||||
@@ -72,6 +80,8 @@ vnoremap <silent><leader>s :SendToTerm<CR>
 | 
			
		||||
nnoremap <silent><leader>s :SendToTerm<CR>
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### More cool mapping
 | 
			
		||||
 | 
			
		||||
Wouldn't it be nice to just double-click commands with mouse? Like a simple GUI! ;)
 | 
			
		||||
 | 
			
		||||
```vim
 | 
			
		||||
@@ -93,7 +103,11 @@ autocmd BufNewFile,BufRead cheat.sh {
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Demo!
 | 
			
		||||
### Demo
 | 
			
		||||
 | 
			
		||||
{{< asciinema key="vimstt" >}}
 | 
			
		||||
 | 
			
		||||
### Getting the scripts
 | 
			
		||||
 | 
			
		||||
### What about neovim/tmux/screen?
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user