ranger

提供: ArchWiki
2015年4月8日 (水) 17:19時点におけるKusakata (トーク | 投稿記録)による版
ナビゲーションに移動 検索に移動

関連記事

ranger は Python で書かれているテキストベースのファイルマネージャです。ディレクトリは3つのカラムに分けて表示されます。キーストロークやブックマーク、マウスやコマンド履歴を使ってディレクトリを移動することができます。選択したファイルは自動的にプレビューが表示されディレクトリは中身が表示されます。

ranger の特徴: vi スタイルのキーバインド、ブックマーク、選択、タグ、タブ、コマンド履歴、シンボリックリンクを作成可能、複数あるコンソールモード、タスク表示。ranger のコマンドやキーバインドはカスタマイズすることができ、外部スクリプトを呼び出すこともできます。よく似たファイルマネージャに Vifm があり、2つのペインと vi スタイルのキーバインドを使用しますが、機能の数はやや劣ります。

インストール

ranger公式リポジトリからインストールできます。開発版を使いたい場合は AUR から ranger-gitAUR をインストールしてください。scope.sh によるファイルプレビューの使用など、任意の依存パッケージは ranger-git の PKGBUILD に説明があります。

使用方法

ranger を起動するには、まずターミナルを起動して、そこから ranger を実行してください。

キー コマンド
? マニュアルを開く
1? キーバインドを表示
2? コマンドを表示
3? 設定を表示
l, Enter ファイルを起動

設定

起動後、ranger~/.config/ranger ディレクトリを作成します。デフォルト設定をこのディレクトリにコピーしたいときは次のコマンドを実行してください:

ranger --copy-config=all
  • rc.conf - スタートアップコマンドとキーバインド
  • commands.py - : で起動するコマンド
  • rifle.conf - 特定のファイルを起動した時に使われるアプリケーション

rc.conf only needs to include changes from the default file as both are loaded. For commands.py, if you do not include the whole file, put this line at the top:

from ranger.api.commands import *

To add a keybind that moves files to a created directory ~/.Trash/ with DD, add to ~/.config/ranger/rc.conf:

map DD shell mv -t /home/user/.Trash %s

See man ranger for general configuration.

コマンドの定義

Continuing the above example, add the following entry to ~/.config/ranger/commands.py to empty the trash directory ~/.Trash.

class empty(Command):
    """:empty

    Empties the trash directory ~/.Trash
    """

    def execute(self):
        self.fm.run("rm -rf /home/myname/.Trash/{*,.[^.]*}")

To use it, type :empty and Enter with tab completion as desired.

警告: [^.] is an essential part of the above command. Without it, all files and directories of the form ..* will be deleted, wiping out everything in your home directory.

Colorscheme

.config/ranger/colorschemescolorschemes サブフォルダを作成してください:

mkdir .config/ranger/colorschemes

then copy your new newscheme.py into that folder. Alter the default color scheme in the .config/ranger/rc.conf file:

set colorscheme newscheme

ファイルの関連付け

Modify ~/.config/ranger/rifle.conf. As the beginning lines are executed first, you should put modifications at the top of the file. For example, the following entry will open a tex file with kile:

ext tex = kile "$@"

To open all files with xdg-utils:

has xdg-open, flag f = xdg-open "$1"

Tips and tricks

圧縮ファイル

These commands use atool to perform archive operations.

Archive extraction

The following command implements archive extraction by copying (yy) one or more archive files and then executing :extracthere on the desired directory.

import os
from ranger.core.loader import CommandLoader

class extracthere(Command):
    def execute(self):
        """ Extract copied files to current directory """
        copied_files = tuple(self.fm.env.copy)

        if not copied_files:
            return

        def refresh(_):
            cwd = self.fm.env.get_directory(original_path)
            cwd.load_content()

        one_file = copied_files[0]
        cwd = self.fm.env.cwd
        original_path = cwd.path
        au_flags = ['-X', cwd.path]
        au_flags += self.line.split()[1:]
        au_flags += ['-e']

        self.fm.env.copy.clear()
        self.fm.env.cut = False
        if len(copied_files) == 1:
            descr = "extracting: " + os.path.basename(one_file.path)
        else:
            descr = "extracting files from: " + os.path.basename(one_file.dirname)
        obj = CommandLoader(args=['aunpack'] + au_flags \
                + [f.path for f in copied_files], descr=descr)

        obj.signal_bind('after', refresh)
        self.fm.loader.add(obj)

圧縮

The following command allows the user to compress several files on the current directory by marking them and then calling :compress package name. It supports name suggestions by getting the basename of the current directory and appending several possibilities for the extension. You need to have atool installed. Otherwise you will see an error message when you create the archive.

import os
from ranger.core.loader import CommandLoader

class compress(Command):
    def execute(self):
        """ Compress marked files to current directory """
        cwd = self.fm.env.cwd
        marked_files = cwd.get_selection()

        if not marked_files:
            return

        def refresh(_):
            cwd = self.fm.env.get_directory(original_path)
            cwd.load_content()

        original_path = cwd.path
        parts = self.line.split()
        au_flags = parts[1:]

        descr = "compressing files in: " + os.path.basename(parts[1])
        obj = CommandLoader(args=['apack'] + au_flags + \
                [os.path.relpath(f.path, cwd.path) for f in marked_files], descr=descr)

        obj.signal_bind('after', refresh)
        self.fm.loader.add(obj)

    def tab(self):
        """ Complete with current folder name """

        extension = ['.zip', '.tar.gz', '.rar', '.7z']
        return ['compress ' + os.path.basename(self.fm.env.cwd.path) + ext for ext in extension]

外付けドライブ

External drives can be automatically mounted with udev or udisks. Drives mounted under /media can be easily accessed by pressing gm (go, media).

画像のマウント

The following command assumes you are using cdemu as your image mounter and some kind of system like autofs which mounts the virtual drive to a specified location ('/media/virtualrom' in this case). Do not forget to change mountpath to reflect your system settings.

To mount an image (or images) to a cdemud virtual drive from ranger you select the image files and then type ':mount' on the console. The mounting may actually take some time depending on your setup (in mine it may take as long as one minute) so the command uses a custom loader that waits until the mount directory is mounted and then opens it on the background in tab 9.

import os, time
from ranger.core.loader import Loadable
from ranger.ext.signals import SignalDispatcher
from ranger.ext.shell_escape import *

class MountLoader(Loadable, SignalDispatcher):
    """
    Wait until a directory is mounted
    """
    def __init__(self, path):
        SignalDispatcher.__init__(self)
        descr = "Waiting for dir '" + path + "' to be mounted"
        Loadable.__init__(self, self.generate(), descr)
        self.path = path

    def generate(self):
        available = False
        while not available:
            try:
                if os.path.ismount(self.path):
                    available = True
            except:
                pass
            yield
            time.sleep(0.03)
        self.signal_emit('after')

class mount(Command):
    def execute(self):
        selected_files = self.fm.env.cwd.get_selection()

        if not selected_files:
            return

        space = ' '
        self.fm.execute_command("cdemu -b system unload 0")
        self.fm.execute_command("cdemu -b system load 0 " + \
                space.join([shell_escape(f.path) for f in selected_files]))
 
        mountpath = "/media/virtualrom/"

        def mount_finished(path):
            currenttab = self.fm.current_tab
            self.fm.tab_open(9, mountpath)
            self.fm.tab_open(currenttab)

        obj = MountLoader(mountpath)
        obj.signal_bind('after', mount_finished)
        self.fm.loader.add(obj)

New tab in current folder

You may have noticed there are two shortcuts for opening a new tab in home (gn and Ctrl+n). Let us rebind Ctrl+n:

rc.conf
map <c-n>  eval fm.tab_new('%d')

Shell tips

Synchronize path

ranger provides a shell function /usr/share/doc/ranger/examples/bash_automatic_cd.sh. Running ranger-cd instead of ranger will automatically cd to the last browsed folder.

If you launch ranger from a graphical launcher (such as $TERMCMD -e ranger, where TERMCMD is an X terminal), you cannot use ranger-cd. Create an executable script:

ranger-launcher.sh
#!/bin/sh
export RANGERCD=true
$TERMCMD

and add at the very end of your shell configuration:

.shellrc
$RANGERCD && unset RANGERCD && ranger-cd

This will launch ranger-cd only if the RANGERCD variable is set. It is important to unset this variable again, otherwise launching a subshell from this terminal will automatically relaunch ranger.

ranger からシェルを起動

With the previous method you can switch to a shell in last browsed path simply by leaving ranger. However you may not want to quit ranger for several reasons (numerous opened tabs, copy in progress, etc.). You can start a shell from ranger (S by default) without losing your ranger session. Unfortunately, the shell will not switch to the current folder automatically. Again, this can be solved with an executable script:

shellcd
#!/bin/sh
export AUTOCD="$(realpath "$1")"

$SHELL

and - as before - add this to at the very end of your shell configuration:

shellrc
cd "$AUTOCD"

Now you can change your shell binding for ranger:

rc.conf
map S shell shellcd %d

Alternatively, you can make use of your shell history file if it has any. For instance, you could do this for zsh:

shellcd
## Prepend argument to zsh dirstack.
BUF="$(realpath "$1")
$(grep -v "$(realpath "$1")" "$ZDIRS")"
echo "$BUF" > "$ZDIRS"

zsh

Change ZDIRS for your dirstack.

Start new ranger instance only if it's not running in current shell

Put this in your shell's startup file:

rg() {
    if [ -z "$RANGER_LEVEL" ]
    then
        ranger
    else
        exit
    fi
}

Execute rg to start or restore ranger.

トラブルシューティング

画像のプレビューでブロックノイズ

カラムのボーダーをなくすと画像プレビューでストライプが見えることがあります [1]~/.config/ranger/rc.conf に以下を設定:

set draw_borders true

参照