Effectly copy part of code file from remote machine

Let's say you have a source code file on a remote machine (accessed via SSH) and you want to copy a portion of the content to your local machine. There are many ways to do this, but I want to present an effective way, as it will take advantage of Helix's very convenient block selection feature.

For example, in the following image, the window on the left is accessing a remote machine, via SSH, on which I am opening a source code file using Helix. The window on the right is an editor, e.g. Geany, of your local machine. I want to copy the source of the enable_mqtt_on_gsm_modem function to Geany. In the left window, we should use a slightly modern Terminal application, e.g. Kitty, WezTerm, Alacritty because of a feature that I will explain later.

Figure 1

On the source file, we'll move the cursor to any line inside the enable_mqtt_on_gsm_modem body, then press m. Helix will bring up a menu of m command, let's press a to select "Select around object". Helix then shows the menu of m a.

Figure 2

This time press f, Helix will select the entire code of the enable_mqtt_on_gsm_modem function.

Figure 3

Here, if we don't use "modal editor" applications like Helix, Vim, NeoVim..., we will have to choose blocks with the mouse, and our wrists and shoulders will be very tired.

Next, press the "Space" key, Helix will show the menu of "Space":

Figure 4

Press y. Helix will copy the selected content to the clipboard, and there will be a message at the bottom "Yanked 1 selection to register +":

Figure 5

An interesting point to note here is that we are running Helix on a remote machine, Helix will normally copy to the clipboard of that machine. However, when creating a command-line window, if you use terminal software of the type mentioned above, the terminal will help transfer that content to the clipboard of your local machine!

Since the content to be copied is already in the local machine clipboard, we just need to press "Ctl + V" to paste it into the Geany software:

Figure 6

In the picture, you can also see the yellow text "Text copied to system clipboard", which is because, on the other machine, I also use Zellij, a software which lets me open many "windows" to work but just on the same SSH connection (similar to traditional tmux). Every time it recognizes some content being copied to the clipboard, it will report that text. The main usage of Zellij is not related to the topic of this article so I don't talk much about it, but there is a related feature that every time you are in Zellij and use your mouse to select some texts, it will automatically copy the text to the clipboard!

So I've finished presenting an example to leverage CLI softwares to work efficiently. The key is to choose these kinds of softwares:

  • Text editor with the ability to recognize code objects by semantics.
  • Terminal with advanced features (although advanced features do not come with a "nice interface" as seen in the picture).

Side information: