diff --git a/ranger/commands.py b/ranger/commands.py index df3fc1b..c2ec362 100644 --- a/ranger/commands.py +++ b/ranger/commands.py @@ -1,5 +1,36 @@ from ranger.api.commands import Command +class mkcd(Command): + """ + :mkcd + + Creates a directory with the name and enters it. + """ + + def execute(self): + from os.path import join, expanduser, lexists + from os import makedirs + import re + + dirname = join(self.fm.thisdir.path, expanduser(self.rest(1))) + if not lexists(dirname): + makedirs(dirname) + + match = re.search('^/|^~[^/]*/', dirname) + if match: + self.fm.cd(match.group(0)) + dirname = dirname[match.end(0):] + + for m in re.finditer('[^/]+', dirname): + s = m.group(0) + if s == '..' or (s.startswith('.') and not self.fm.settings['show_hidden']): + self.fm.cd(s) + else: + ## We force ranger to load content before calling `scout`. + self.fm.thisdir.load_content(schedule=False) + self.fm.execute_console('scout -ae ^{}$'.format(s)) + else: + self.fm.notify("file/directory exists!", bad=True) class fzf_select(Command): """ diff --git a/ranger/rc.conf b/ranger/rc.conf index 26b64e4..a2c6b50 100644 --- a/ranger/rc.conf +++ b/ranger/rc.conf @@ -304,7 +304,7 @@ map V console shell vim%space map mark_files toggle=True -map M console mkdir%space +map M console mkcd%space # Jumping around map H history_go -1