UP | HOME

Emacs Tramp tricks

Table of Contents

emacs_logo.png

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

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

these are the basic types…here is an example

/ssh:myname@192.168.8.1:/etc/passwd

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@192.168.8.2:~/mytext.org 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".

/sudo:root:/etc/passwd

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

/sudo:user@host:/dir/file

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

/sudoedit::/etc/passwd

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

Tramp Combination

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.

/ssh:user@192.168.8.5|sudo:root@192.168.8.5:/etc/passwd

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..

/ssh:user@192.168.8.5|ssh:user@192.168.8.2|sudo:root@192.168.8.2:/etc/passwd

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.

E.g.

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

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
#+end_src
#+RESULTS: debootstrap_search
:results:
community/debootstrap 1.0.123-1
    Bootstrap a basic Debian system
:end:

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/ {
    split($1,a,"/");
    print a[1],"->",a[2]
  }
#+end_src

#+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.

gcc
clang
emacs
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;
      }'
done
Name Version URL
gcc 15.1.1+r7+gf36ec88aa85a-1 https://gcc.gnu.org
clang 20.1.8-1 https://clang.llvm.org/
emacs 30.1-5 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.

hostname
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.

#CALL: host-info[:dir /ssh:calle@192.168.1.2:/tmp]()

alarm
192.168.1.2/24
5.5.0-1-ARCH
unset apa
declare -A apa
apa['fisk']='pisk
PUSK'
apa['sump']='rump
SNUSK'
#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

Computers
192.168.1.2
192.168.1.8
192.168.1.10
# 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}
done
cpu 75889 1101 19454 4501180 17824 11277 1459 0 0 0
cpu0 5226 8 1045 378224 576 226 278 0 0 0
cpu1 5144 66 2238 368145 1816 8186 109 0 0 0
cpu2 6164 58 1600 376439 916 292 95 0 0 0
cpu3 6487 103 1390 376133 1108 177 119 0 0 0
cpu4 6698 88 1344 376094 1113 203 77 0 0 0
cpu5 7036 143 1697 375146 1279 172 138 0 0 0
cpu6 5736 30 1141 375828 2628 199 89 0 0 0
cpu7 5583 78 3252 374317 1893 965 143 0 0 0
cpu8 7786 46 1431 373959 2078 204 61 0 0 0
cpu9 6980 65 1349 376175 975 170 69 0 0 0
cpu10 6415 225 1515 375289 1781 266 103 0 0 0
cpu11 6627 186 1448 375424 1655 213 172 0 0 0

Date: 2020-07-28 Tue 00:00

Author: Calle Olsen

Created: 2025-08-04 Mon 20:04

Validate