Emacs Tramp tricks

Table of Contents


Emacs org-mode literate programming

Ok this is a neat thing, something that is really useful.

First lets explain the tramp, tamp is a feature in emacs which can be used to access remote files from within emacs. consider that you want to edit a file on a remote server, though since this server is solely a server, no one has even consider installing emacs and even less consider configuring it. You can access it through ssh, and as usual vi is installed, that's how I did it before. Then I came across tramp, I tried it with some luck and some failures and put it on the shelf and added it to the "CHECK IT OUT" list for some rainy day. Well, the rainy day arrived (quite quickly unfortunatly) and i started fiddling with it, and to my surprise this was awsome. This is what I've been looking for. Editing files with my local emacs on the remote machine is just awesome. Though, I must admit that the URL-parsing was a bit of a hassle. So lets explain it really simple

.e.g. file,ssh,rsh,sudo and many more
system user name
host or ip.

these are the basic types…here is an example


Lets first consider using it on a local file. Usually we do C-x C-f /home/user/mytext.org , the same way we can connect to a remote machine and doing exactly the same: C-x C-f /ssh:user@ Its now possible to edit the file on the remote server the exact same way as it was on a local machine. Quite useful, though in my case most of the time I need to ssh into the machine, and then use sudo to become a superuser. This of course is possible using tramp, though now the url parsing becomes somewhat problematic. So lets explain it in steps. Lets first edit a localfile with "sudo".


The first we want to do is to ssh into the machine, the output from that ssh session is then "piped" into sudo..


when editing a local file with "sudo" you can ommit the "user@host" so that the it becomes sudo::/etc/passwd there is also a "sudoedit" connection type which is similair to sudo , however it does not keep the session running in the background , and is restricted to localhost


That means that using sudo with a user and remote will actually use an ssh session together with sudo.

Tramp Compination

So far we been looking at how to use tramp solely, it is also possible to combine both e.g. ssh and sudo this is done using the "|" command. Lets do an example.


To go even further its possible to connect to a remote machine via ssh and hop to another, and there edit a file using sudo..


In other words, its possible to do all kinds of different kombination, at least in theory.

Org-mode links

Of course the above urls can be used as org-links.


[[/ssh:user@|sudo:root@][edit passwd @]]

But another useful org feature is using src fields creating small scripts runing on local and even remote computer.

Again lets do an example.

#+NAME: debootstrap_search
#+begin_src bash :dir /tmp :results drawer :exports results
   pacman -Ss debootstrap
#+RESULTS: debootstrap_search
community/debootstrap 1.0.123-1
    Bootstrap a basic Debian system

For more information on results see result and evaluation. The above code will execute pacman -Ss debootstrap on the local machine And the result will be added as a result drawer. The result has a name, so we could use another source block and use the result as input. e.g.

#+NAME: awkward
#+begin_src awk :stdin debootstrap_search
  /debootstrap/ {
    print a[1],"->",a[2]

#+RESULTS: awkward
: community -> debootstrap

In the above example I used the output from the debootstrap_search and feeded it into awk where I search for the line debootstrap , and split it up into an array.

echo " Name Version URL"
for var in "${apa[@]}"; do
    pacman -Qi ${var} | awk '
      /(Name|Version|URL)\s+:/ {
      if ($1 == "URL" )
          printf "%s\n",$3;
      else if($1 == "Name" || $1 == "Version")
          printf "%s ",$3;
Name Version URL
gcc 10.2.0-2 https://gcc.gnu.org
clang 10.0.1-1 https://clang.llvm.org/
emacs 27.1-3 https://www.gnu.org/software/emacs/emacs.html

This time I did not use a drawer, instead I want the output as a table.

Calling source block

Re-use of is always fun, so sometimes you want to reuse something previously defined.

ip add | awk '/inet(.*)\/24/ {print $2}'
uname -a | awk '{print $3}'

echo "${in[0]},${in[1]},${in[2]}"
echo "banan,rumpa,pumba"

Now if I want to use that again, but maybe use another host, I could set the dir to that host.

unset apa
declare -A apa
#echo $apa
echo ${apa[fisk]}
echo ${apa[sump]}

Run a docker

Ok, lets setup a docker image We use the results value, we are only intrested in the last command

Download archlinux

docker pull $name

Check docker images


Final example

What I want to do is to setup a table with all the host. For each of the host in the table I want to get the current cpu load, and then in the final table I want to have them in a graph..

Let there be a table

This table consists of all the ip addresses that I want to use The table is eassiest to create with C-| and follow the instructions

# what i want to do here is to iterate over the ip numbers and then
# call another function to perform the necessary evaluation
for ip in ${IPS[@]}; do
    echo ${ip}
cpu 26273 1 6386 5256814 5655 1207 545 0 0 0
cpu0 2072 0 470 438118 460 95 97 0 0 0
cpu1 1942 0 561 438349 464 55 32 0 0 0
cpu2 2533 0 554 437414 560 198 54 0 0 0
cpu3 2504 0 496 437712 460 216 30 0 0 0
cpu4 2619 0 538 437784 472 48 25 0 0 0
cpu5 2214 0 513 438118 616 43 23 0 0 0
cpu6 1737 0 504 438706 461 44 28 0 0 0
cpu7 2087 0 531 438321 431 45 25 0 0 0
cpu8 2051 0 627 438127 416 52 65 0 0 0
cpu9 2585 0 515 437808 289 75 104 0 0 0
cpu10 1846 0 562 438264 576 124 22 0 0 0
cpu11 2078 0 510 438089 445 206 35 0 0 0

Date: 2020-07-28 Tue 00:00

Author: Calle Olsen

Created: 2020-09-13 Sun 17:50