forked from johngodlee/ranger_rifle_conf_tut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.html
86 lines (83 loc) · 7.81 KB
/
output.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<style type="text/css">code{white-space: pre;}</style>
<link rel="stylesheet" href="blue_big.css" type="text/css" />
</head>
<body>
<h1 id="intro-to-the-ranger-file-manager-and-rifle.conf-for-macos">Intro to the Ranger File Manager and <code>rifle.conf</code> for macOS</h1>
<p>Ranger is a file manager for the terminal and my default file manager since I moved away from <code>Midnight Commander</code> (<code>mc</code>). Most of my work I do on a MacBook Pro and I found that opening files that weren't text editor compatible was difficult, pressing <Enter> on the highlighted files merely returned an <code>open with:</code> prompt, and nothing I typed seemed to work.</p>
<div class="figure">
<img src="img/ranger_scrot.png" />
</div>
<p>I found out that the config file which controls how Ranger opens files when pressing <Enter> is called <code>rifle.conf</code> and is normally found in <code>~/.config/ranger/rifle.conf</code>.</p>
<p>The default <code>rifle.conf</code> however is dense, complex and doesn't play well with an ordinary mac setup.</p>
<p>So I decided to try and strip down <code>rifle.conf</code> and rebuild it for use on macOS. If you want to skip straight to the <code>rifle.conf</code> that I use, <a href="files/rifle.conf">follow this link</a>.</p>
<p>Now open up your own <code>rifle.conf</code> and start editing:</p>
<pre class="shell-script"><code>vim ~/.config/ranger/rifle.conf</code></pre>
<p>The pre-amble comments give examples of how to set up the file, but this took me a while to understand. I finally decided to remove everything and start from scratch.</p>
<p><code>rifle.conf</code> is structured so each line provides a set of conditions separated by commas (<code>,</code>) e.g. <code>ext mp3</code>, followed by an equals <code>=</code>, then a terminal command, e.g. vlc <code>$@</code>. When Ranger is asked to open a file, it cycles through every line from top to bottom until it finds a line where all the conditions are met, then it runs the terminal command. This means that the top-most line where all the conditions are met will be run. Using this logic you can construct a <code>rifle.conf</code> that works on different systems, if you order potential applications from most to least preferred.</p>
<p><hl></p>
<h2 id="using-open">Using <code>open</code></h2>
<p>Most Linux applications can simply be opened by typing their name into the terminal, optionally followed by a file to open and various rules, e.g. <code>zathura example.pdf</code>. In macOS however, typing the equivalent <code>preview example.pdf</code> just returns an error that <code>preview</code> has not been found. The same goes for <code>Preview</code>, <code>preview.app</code> and <code>Preview.app</code>.</p>
<p>Instead, on a Mac, I find the best way to call an application from the command line is to use <a href="https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/open.1.html"><code>open</code></a>.</p>
<p>The following lines all open Preview.app:</p>
<pre class="shell-script"><code>open -a preview
open -a preview.app
open -a Preview
open -a Preview.app</code></pre>
<p>And to open <code>example.pdf</code> just add the file name:</p>
<pre class="shell-script"><code>open -a preview example.pdf</code></pre>
<p>This same syntax can be applied to <code>rifle.conf</code> to open specific apps for specific file types with Ranger.</p>
<p>The simplest condition to use is the file extension (<code>ext</code>):</p>
<pre class="shell-script"><code>ext pdf = open -a preview "$@"</code></pre>
<p>Where <code>"$@"</code> is the file path of the file highlighted in Ranger The above there says, if the file extension is <code>.pdf</code>, open the file with Preview.app.</p>
<p><hl></p>
<h2 id="regex">Regex</h2>
<p>Regular expressions can be used to make more complex conditions, e.g. using <code>|</code> to include multiple file extensions in one line, or <code>[]</code> and <code>?</code> to list similar extensions without repetition. Try to work out what the conditions below mean. If you need a refresher on Regex, I often refer to <a href="LINK%20TO%20REGEX">this website</a>:</p>
<pre class="shell-script"><code>ext jpe?g|png|gif = open -a preview "$@"
ext od[dfgpst] = open -a libreoffice "$@"</code></pre>
<p><hl></p>
<h2 id="mime-types">MIME types</h2>
<p>MIME types are a good last resort for catching any files with unusual extensions, I normally put them at the bottom of a list of preferred applications, to open with a program I know will be installed, for example:</p>
<pre class="shell-script"><code>ext xml|json|py|pl|rb|js|sh|php|m[ark]d[own]|txt = vim "$@"
ext tex = open -a texworks "$@"
mime ^text = nano "$@"</code></pre>
<p><hl></p>
<h2 id="the-has-condition">The <code>has</code> condition</h2>
<p>For the majority of GUI applications on a Mac, the <code>has</code> condition does not work, for the same reason that calling applications directly from the terminal by typing their name doesn't work, because Ranger can't find them. As far as I know, there isn't any way to get around this. Maybe if you could check for the presence of the named directory in <code>~/Applications</code>, but I haven't worked that out yet.</p>
<p><hl></p>
<h2 id="taking-advantage-of-launchservices-defaults">Taking advantage of LaunchServices defaults</h2>
<p>Finally, a good line to keep at the end of <code>rifle.conf</code> is:</p>
<pre class="shell-script"><code>flag f = open "$@"</code></pre>
<p>This takes advantage of <code>open</code> using macOS <code>LaunchServices</code> to decide the default application to open certain filetypes. This should catch everything that you haven't listed specifically. I suppose you could just have a <code>rifle.conf</code> with only this line and everything would still work, files would be opened with the default Mac GUIapplication as if you had double clicked on it in <code>Finder</code>.</p>
<p><hl></p>
<h2 id="formatting">Formatting</h2>
<p>To increase readability I try to split my <code>rifle.conf</code> into sections, so I don't get mixed up with my program heirarchies, for example:</p>
<pre class="shell-script"><code>#-------------------------------------------
# Web material
#-------------------------------------------
ext x?html?, X, flag f = open -a Safari "$@"
ext x?html?, has w3m, terminal = w3m "$@"
#-------------------------------------------
# Text files
#-------------------------------------------
ext xml|json|tex|py|pl|rb|js|sh|php|m[ark]d[own]|txt = vim "$@"
mime ^text = vim "$@"
#--------------------------------------------
# Audio without X
#-------------------------------------------
ext mp3 = /Applications/VLC.app/Contents/MacOS/VLC --intf ncurses "$@"</code></pre>
<p>Now build your own <code>rifle.conf</code> using the tips above!</p>
<p><hl></p>
<h1 id="a-final-tip">A final tip</h1>
<p>If you find yourself switching between Finder and Ranger a lot, especially as you transition to the terminal, adding this keybinding to <code>~/.config/ranger/rc.conf</code> will allow you to open Finder on the highlighted file with <code>%</code>:</p>
<pre class="shell-script"><code>map % shell open -R %s</code></pre>
<p>It opens the shell prompt and uses <code>open</code> to call Finder (<code>-R</code>) on the highlighted file (<code>%s</code>). You could do similar if you wanted to quickly specify a non-default editor for a file:</p>
<pre class="shell-script"><code>map te shell open -a textedit %s</code></pre>
</body>
</html>