Tips to work over SSH

Hi, everybody!

Those of you that use Kakoune over SSH could give me some tips about your setup?

I’m thinking of things like making new and terminal commands work. And clipboard integration (if possible) would be a plus. And, obviously, any other clever trick you currently use and I couldn’t anticipate would be nice.

Regarding new, I don’t have tmux installed on the remote machine. Is there another way? More specifically, would it be possible to configure my remote Kakoune to communicate to my local Kitty to handle that?

Thanks in advance.

Ow man so many questions I can’t answer. But when I’m editing remote I try to use mosh. It’s great for connections which aren’t rock solid (all of them here).

I haven’t personally used it myself, but I have heard a lot of good things about sshfs. It essentially allows for mounting a remote filesystem. That should allow you to use kitty with your local kakoune config. Apart from that, I don’t know how to help, but I would second harrts’ suggestion with mosh - it’s a lifesaver on unstable networks.

sshfs tends to not do great with lots of small files. I much prefer the experience of SSH over sshfs. Even using it locally (to a VM on the same machine) made it very slow.

Fortunately, I don’t have problems with the stability of my connection. But it’s nice to know a tool like mosh exists. Thanks!

sshfs is not a good option for my setup. But thanks anyway =)

One thing I discovered recently is the ControlMaster configuration option of ssh that allows to share a single connection for multiple sessions. This means ssh wont ask you for a password if you already have an active session, making opening a new client on the existing session faster (just ssh user@host kak -c session, no password asked, no connection delay).

I would love to find a way to have a terminal implementation that works through ssh (new uses terminal as a backend so we would get that for free), for this to work we would need the kakoune server running remotely to somehow launch a new terminal emulator on the local machine, I can think of a couple ways to achieve this:

  • Use a remote -> local ssh session, ControlMaster would help again here avoiding the need to enter a password, but that is not always practical (the local machine might not have a sshd)
  • Emit some escape sequence to notify the local terminal emulator to open a new client, this would be terminal emulator dependent, it looks like kitty and iterm2 could support that.
4 Likes

Kitty seems a interesting development in terminal land in general. I have high hopes for the project.

Thanks, @mawww!

One think that puzzles me is that Kitty advertises in different places that many of its features work even over ssh, but don’t explain how can we use it. I’ve even found an issue in which someone asked for clarifications on this regard, but the answer from the developer wasn’t satisfatory.

No tips as such, but you could use a raspberry-pi as a server with a large thumb drive for storage and test, test, test or ‘use a [Docker] container terminal in a browser, attach to someone else’s terminal, and share control’ (Docker in Practice, p.32). Docker-Terminal: a terminal for Docker containers.

As you probably know SSH is the sledgehammer protocol as bypasses firewall and mounts an entire file system (cool, but very heavy). If your use case is just files not the traversal of an entire filesystem you could try Syncthing SSH Tunneling if firewall is the issue.

Read more here or here if you like. You can also get on board here. Ah nondescript here links, an internet bloggers gem (google analytics it’s all on you).

Side note: I actually got into an argument with another student over having to click reference links to do some research gathering as there was no search term box to enter a phrase into. It’s official, clicking links is just too damn hard work! Bye :wave:.

I have a remote development server on which I can’t install too many dependencies.
To get my local development environment while offloading work to the remote server I scp individual files for editing and have adjusted all other commands to run remotely (make, grep, git, repl, etc). This requires a lot of configuration but I am happy with the result. I get low-latency typing, my local kakoune setup/tooling, and offload any serious tasks to the server.

Here is a snippet for how I open remote files:

declare-option str remote_base_path "/tmp/kakoune-remote-files"
declare-option str file_host ""
declare-option str file_path ""
declare-option str bufname ""

provide-module remote-files %{

define-command open_remote_file -params 2.. -override %{
  evaluate-commands %sh{
    mkdir -p "$kak_opt_remote_base_path" > /dev/null
    localfile=$(mktemp -p "$kak_opt_remote_base_path" "$1-XXX-$(basename $2)" -u) 
    scp "$1:$2" "$localfile" > /dev/null
    printf %s\\n "
      edit! '$localfile' $3
      set buffer file_host '$1'
      set buffer file_path '$2'
      hook -always buffer BufWritePost .* %{ nop %sh{
        scp $localfile '$1:$2'
      }}
      hook -always -once buffer BufClose .* %{
      nop %sh{
        rm $localfile
      }}
      eval_bufname
    "
  }
}

define-command reload_remote_file -override %{
  nop %sh{
    scp "$kak_opt_file_host:$kak_opt_file_path" "$kak_buffile" > /dev/null
  }
}

define-command eval_bufname -override %{
  evaluate-commands %sh{
    shorten(){
        PATHLENGTH=$(echo -n $1 | wc -m)
        MAXLEN="$2"
        if  [ "$PATHLENGTH" -gt "$MAXLEN" ]
        then
          PATHREP="...$(echo -n $1 | tail -c $MAXLEN)"
        else
          PATHREP="$1"
        fi
        echo $PATHREP
    }
    case $kak_buffile in
      "$kak_opt_remote_base_path"*)
        shortened_file_path=$(shorten "$kak_opt_file_path" 20)
        echo "set buffer bufname 'ssh:$kak_opt_file_host:$shortened_file_path'"
        ;;
      *)
        shortened_file_path=$(shorten "$kak_buffile" 30)
        echo "set buffer bufname '$shortened_file_path'"
        ;;
    esac
  }
  set-option buffer modelinefmt '%opt{bufname} %val{cursor_line}:%val{cursor_char_column} {{context_info}} {{mode_info}} - %val{client}@[%val{session}]'
}

}

For commands to execute remotely, I have a project_host and project_path option that specify the current directory. Then I edit grep and the like to execute remotely. For example:

define-command -params .. -override -docstring %{
    project_grep [<arguments>]: grep utility wrapper
    All optional arguments are forwarded to the grep utility
    Passing no argument will perform a literal-string grep for the current selection
} project_grep %{ evaluate-commands %sh{
    if [ $# -eq 0 ]; then
        case "$kak_opt_grepcmd" in
        ag\ * | git\ grep\ * | grep\ * | rg\ * | ripgrep\ * | ugrep\ * | ug\ *)
            set -- -F "${kak_selection}"
            ;;
        ack\ *)
            set -- -Q "${kak_selection}"
            ;;
        *)
            set -- "${kak_selection}"
            ;;
        esac
    fi
     output=$(mktemp -d "${TMPDIR:-/tmp}"/kak-grep.XXXXXXXX)/fifo
     mkfifo ${output}
     if [ -z "${kak_opt_project_host}" ]
     then
       ( { trap - INT QUIT; cd $kak_opt_project_path; ${kak_opt_grepcmd} "$@" 2>&1 | tr -d '\r' | awk -v var="${kak_opt_project_path}/" '{ print var $0}'; } > ${output} 2>&1 & ) > /dev/null 2>&1 < /dev/null
     else
       ( { trap - INT QUIT; echo "cd $kak_opt_project_path; ${kak_opt_grepcmd} \"$@\" '.' " | ssh -t $kak_opt_project_host  2>&1 | tr -d '\r' | awk -v var="/ssh:${kak_opt_project_host}:${kak_opt_project_path}/" '{ print var $0}'; } > ${output} 2>&1 & ) > /dev/null 2>&1 < /dev/null
     fi
     printf %s\\n "evaluate-commands -try-client '$kak_opt_toolsclient' %{
               edit! -fifo ${output} *grep*
               set-option buffer filetype grep
               set-option buffer jump_current_line 0
               hook -always -once buffer BufCloseFifo .* %{ nop %sh{ rm -r $(dirname ${output}) } }
           }"
}}

I don’t have a package published for any of this because it is a series of poorly scripted temporary fixes that became permanent for me.

2 Likes