Skip to content

Instantly share code, notes, and snippets.

@0racle
Last active May 4, 2023 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0racle/202ede4d88eb9b1bd1f489382f45ece6 to your computer and use it in GitHub Desktop.
Save 0racle/202ede4d88eb9b1bd1f489382f45ece6 to your computer and use it in GitHub Desktop.
Vim Things

For most languages, I will have a "shebang" line at the top.

The shebang could be #!/usr/bin/env python3, but it could just as easily be -- !/usr/bin/env runhaskell or // !cargo run

This function reads everthing after the ! and uses that command to run the curent file

function! RunBang()
    if (&modified)
        :write
    endif
    if (getline(1) =~ '!')
        let l:bang = split(getline(1), '!')[1]
        let l:path = shellescape(expand('%:p'), 1)
        :exec '!' l:bang l:path
    endif
endfunc

This is the same function, but instead it opens a Vim terminal in a vertical split and runs it there. I wrote this when I first started playing with terminals in vim, but I don't use it much.

function! RunBangInTerminal()
    if (&modified)
        :write
    endif
    if (getline(1) =~ '!')
        let l:path = expand('%:p')
        let l:cwd = expand('%:p:h')
        let l:bang = split(getline(1), '!')[1]
        call term_start(split(l:bang, ' ') + [l:path], #{ cwd: l:cwd, vertical: 1, norestore: 1 })
    endif
endfunc

If you use Tmux, you could open a repl in a split pane, and then use (a keymap to) this fuction to send the current line to the Tmux pane

function! TmuxSendLine()
    let l:line = escape(getline("."), '\')
    if len(l:line)
        let l:line = substitute(l:line, '"', '\\"', 'g')
        call job_start('tmux send -t {last} "' . l:line . '" Enter')
    endif
endfunction

Again, here's a similar function that does the same, but it sends the current line to a vim terminal (presumably in a vim split running a repl)

function! TermSendLine()
    let l:line = getline(".")
    if len(l:line)
        call term_sendkeys(get(t:, 'last_term', 0), l:line . "\<cr>")
    endif
endfunction

This above function relies on a last_term variable being set, which I do with an autocmd

augroup TerminalMode
    autocmd!
    autocmd TerminalOpen * set nonumber norelativenumber  " disable line numbers
    autocmd TerminalOpen *
      \ if &buftype ==# 'terminal' |
      \   let t:last_term = +expand('<abuf>') |
      \ endif
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment