Use Ranger and Sed to Quickly Add Filetags to Org-roam files
By funcsec
- 3 minutes read - 480 wordsHere is how I used ranger
to add a filetag to org files in a fast and efficient way.
I used this when I had to retroactively tag a bunch of org-roam files after a decision to split up my roam files in a different way.
It makes adding a tag easy and fast!
I modified hundreds of files in just a few minutes, files that I could not pragmatically modify due to no consistency in their content.
Hope it will help you too.
Add a line in the middle of a text file
First I used an org file at /tmp/file.org
to test on.
cat <<EOF > /tmp/file.org
:PROPERTIES:
:ID: 45c20818-f221-4846-a0ab-6e3d3177b593
:END:
#+title: Test file
this is an example file
we will be trying to add a filetag above the title
EOF
I needed it to add a line around the #+title:
part.
Luckily this Stack Overflow answer let me know what I needed from my sed
command.
The command that worked added the new tag line above the existing line, which is fine for this purpose.
The code that worked was
sed -n 'H;${x;s/^\n//;s/#+title\: .*$/\#\+filetags\:\ \:tag\:\n&/;p;}' /tmp/file.org
Which resulted in:
:PROPERTIES:
:ID: 45c20818-f221-4846-a0ab-6e3d3177b593
:END:
#+filetags: :tag:
#+title: Test file
this is an example file
we will be trying to add a filetag after title
Success! The filetag was added above the title.
Writing as a script
Next I made it into a shell script in the home bin $HOME/bin
so I could call it with ranger
for our org files.
#!/usr/bin/env bash
#
# ./org-tag-add
# Add tag before the #+title: in an org file
FILE="$1"
sed -n -i 'H;${x;s/^\n//;s/#+title\: .*$/\#\+filetags\:\ \:tag\:\n&/;p;}' "$FILE"
Make sure to add whatever filetag you want to the above script at \:tag\:
if you want to use this.
I added the sed
flag -i
so that it would overwrite the file.
Modify the ranger config
To make ranger
open .org
files with the new script, I modified $HOME/.config/ranger/rifle.conf
with the following code near the top.
I placed it on line 79 before the main rules.
ext org = $HOME/bin/org-tag-add "$@"
ranger
reads the rifle.conf
file to know how to open files with certain file extensions, like opening .txt
in vim
or nano
depending on how your $EDITOR
bash variable is set.
What this did is open .org
files in the script, which adds the tag and close it.
Conclusion
Next, I opened ranger
, navigated to the directory with my org files and began “opening” files where I wanted to add the filetag specified in the script.
Hitting the l
on each of the files opened, add the tag, and then closed the file.
Much simpler than opening each in vim
or emacs
and copy pasting the line in.
Much faster too.
When I was done I removed the line from $HOME/.config/ranger/rifle.conf
so org files would again open in $EDITOR
.
Image by Adolphe Appian