3 ms·
This is a little off topic, but some people might find this useful. I have bindings that allow me to execute the current line or paragraph under the cursor in t
by ckw 3y ago
This is a little off topic, but some people might find this useful. I have bindings that allow me to execute the current line or paragraph under the cursor in the underlying shell and then inject the result into the current buffer.
"execute the contents of the current line => sx
exe "nnoremap " . g:leader_prime . "x :call ExecuteCurrentLine()\<CR>"
Here leader_prime is just a second leader key I use: 's', but you could map the command however you like. ExecuteCurrentLine() is implemented as:
function! ExecuteCurrentLine()
let l:com = substitute(getline('.'), '\#', '\\#', '')
exe "norm! mc"
exe "norm! o\<Esc>"
exe "r! " . l:com
exe "norm! mr"
exe "norm! o\<Esc>"
endfunction
Two more mappings:
exe "nnoremap " . g:leader_prime . "p :call ExecuteCurrentParagraph()\<CR>"
exe "nnoremap " . g:leader_prime . "c :call ExecuteCurrentLineWithBC()\<CR>"
and their implementations:
function! ExecuteCurrentParagraph()
let l:init = line('.')
exe "norm! {j"
let l:start = line('.')
exe "norm! }"
let l:end = line('.')
exe "norm! o\<Esc>"
exe "r! " . join(getline(l:start,l:end), ' ')
exe "norm! o\<Esc>"
endfunction
function! ExecuteCurrentLineWithBC()
let l:com = getline('.')
exe "norm! o\<Esc>"
exe "r! " . "echo 'scale=12;" . l:com . "' | bc"
exe "norm! o\<Esc>"
endfunction
As written above, I type 'sx' to execute the current line under the cursor as a shell command, and then inject the result back into the buffer where I am. 'sp' does the same, but for the entire paragraph the cursor is on; this is really convenient for things like a large sql query which is more readable written over many lines. And finally 'sc' executes the current line in the bc calculator program in the underlying shell, so you can evaluate arithmetic expressions in the buffer.
- kps 3y agoWhy not just use `!`?
- ckw 3y agoSee my reply to AdieuToLogic.
- AdieuToLogic 3y agoAnd see my reply to you :-).
- AdieuToLogic 3y ago> I have bindings that allow me to execute the current line or paragraph under the cursor in the underlying shell and then inject the result into the current buffer. As @kps mentioned in a peer comment, using the built-in ex[0] '!' command can do what the vim script above does. For example, to execute the line where the cursor is currently placed and replace the same line's content using POSIX shell: :. !sh To do the same with multiple lines I like to mark the first command, then the last, and run: :'a,'b !sh Where the "a" buffer is the first command and the "b" buffer is the last command in this example. There are many other ways to specify ranges too. I recommend beginning with vim's builtin help: :help range And going from there. 0 - https://man.freebsd.org/cgi/man.cgi?query=ex&apropos=0&sektion=0&manpath=FreeBSD+13.2-RELEASE+and+Ports&arch=default&format=html https://man.freebsd.org/cgi/man.cgi?query=ex&apropos=0&sekti...
- kps 3y ago^ What they said. (I was terse on my way to bed yesterday.) The big benefit of `!` is that, like anything else in visual mode, it works with motion operators. I often write fragments of shell, e.g. to generate something repetitive, and run it with `!!` (doubling for the current line, same as things like `dd` or `yy`), `!}` (block), or `!G' (rest of file).
- ckw 3y agoFirst, I don't want to type <shift><semicolon><period><space><shift><1><s><h><enter>. I want to type at most two characters, because I do this a lot. I chose sx, but really any good <leader><foo> combination would be fine. Note they're both on the same hand; I had pretty bad rsi for a couple years and was typing almost exclusively with my left hand. Second, :. !sh eats the command. I want the command to remain in place in the buffer. Imagine you have a buffer full of useful commands that you use repeatedly with minor tweaks, and you use the output of those commands to write other bespoke commands, and so on. Third, :. !sh leaves you sitting above the result of the command. I rarely want to be there. I want to be positioned right after the result, on a new line, like my buffer is a shell-- I see the end of the result and can immediately begin typing the next command.
- 3y ago