1 # This is a sample commands.py. You can add your own commands here.
3 # Please refer to commands_full.py for all the default commands and a complete
4 # documentation. Do NOT add them all here, or you may end up with defunct
5 # commands when upgrading ranger.
7 # A simple command for demonstration purposes follows.
8 # -----------------------------------------------------------------------------
10 from __future__ import (absolute_import, division, print_function)
12 # You can import any python module as needed.
15 # You always need to import ranger.api.commands here to get the Command class:
16 from ranger.api.commands import Command
19 # Any class that is a subclass of "Command" will be integrated into ranger as a
20 # command. Try typing ":my_edit<ENTER>" in ranger!
21 class my_edit(Command):
22 # The so-called doc-string of the class will be visible in the built-in
23 # help that is accessible by typing "?c" inside ranger.
24 """:my_edit <filename>
26 A sample command for demonstration purposes that opens a file in an editor.
29 # The execute method is called when you run this command in ranger.
31 # self.arg(1) is the first (space-separated) argument to the function.
32 # This way you can write ":my_edit somefilename<ENTER>".
34 # self.rest(1) contains self.arg(1) and everything that follows
35 target_filename = self.rest(1)
37 # self.fm is a ranger.core.filemanager.FileManager object and gives
38 # you access to internals of ranger.
39 # self.fm.thisfile is a ranger.container.file.File object and is a
40 # reference to the currently selected file.
41 target_filename = self.fm.thisfile.path
43 # This is a generic function to print text in ranger.
44 self.fm.notify("Let's edit the file " + target_filename + "!")
46 # Using bad=True in fm.notify allows you to print error messages:
47 if not os.path.exists(target_filename):
48 self.fm.notify("The given file does not exist!", bad=True)
51 # This executes a function from ranger.core.acitons, a module with a
52 # variety of subroutines that can help you construct commands.
53 # Check out the source, or run "pydoc ranger.core.actions" for a list.
54 self.fm.edit_file(target_filename)
56 # The tab method is called when you press tab, and should return a list of
57 # suggestions that the user will tab through.
58 # tabnum is 1 for <TAB> and -1 for <S-TAB> by default
59 def tab(self, tabnum):
60 # This is a generic tab-completion function that iterates through the
61 # content of the current directory.
62 return self._tab_directory_content()