5

Is there a way to use different folders views each folder ? Some folders i like to see as icons and others is better to see as list.

enter image description here

  • 3
    The exact same question passed by a short time ago. My comment was: not in a clean way, only in a (very) dirty way. What is possible is to run a background script, acting when a nautilus window of a certain (folder-) name comes to front, making xdotool press a single Ctrl 1 (to set list view), or the other way around. But again, it is a dirty solution in this case – Jacob Vlijm Apr 12 '15 at 16:05
4

Since nautilus does not have a command line option to switch between list- and icon view, nor has the option to set view prferences per folder, there is no clean way to do that.

Started as an experiment, the option below was to see if it could be done anyway in a reasonably functional way.

As mentioned in a comment, the solution is as dirty as it gets, but it turned out to be fully functional, and in the hours that I tested it, I did not run into issues. The decision to use it or not is yours.

The scripts and what they do

The solution consists of two scripts; one to be run from a shortcut key to add the active (nautilus) window to a list. Listed windows will be automatically set to list view, other windows are in icon view by default. Of course you can set it vice versa, depending on what view type you'd like to set as default

The switch from / to list view is done by simulating either Ctrl+1 or Ctrl+2, which are the shortcuts in nautilus to set list- or icon view.

How it works in practice

Running the first script in the background, the default view for nautilus windows is icon view.

If you want to set list view for a specific folder, you navigate to the folder (open it), with the folder's window opened and frontmost, press (e.g. Ctrl+Alt+A)

This will add the window's name to a list (kept in a hidden file in your home directory). The next time you open the folder, it will automatically switch to list view, and back to icon view when you navigate tyo another folder.

To remove the folder from the list, press Ctrl+Alt+R with the folder in question in front.

How to set up

  1. The scripts need both wmctrl and xdotool:

    sudo apt-get install wmctrl
    sudo apt-get install xdotool
    
  2. Copy the script below into an empty file, save it as add_folder.py This is the script to add or remove the window to/from list view.

    #!/usr/bin/env python3
    import subprocess
    import os
    import sys
    
    add = sys.argv[1]
    
    wlist = os.environ["HOME"]+"/.window_list.txt"
    
    get = lambda cmd: subprocess.check_output(cmd).decode("utf-8")
    window = get(["xdotool", "getactivewindow", "getwindowname"]).strip()+"\n"
    
    def add_window():
        if os.path.exists(wlist):
            current = open(wlist).readlines()
            if not window in current:
                new = current+[window]
                open(wlist, "w").writelines(new)
        else:
            open(wlist, "w").write(window)
    
    def remove_window():
        if os.path.exists(wlist):
            current = open(wlist).readlines()
            if window in current:
                current.remove(window)
                open(wlist, "w").writelines(current)
    
    if add == "+":
        add_window()
    elif add == "-":
        remove_window()
    
    • Test run the script by the command:

      python3 /path/to/add_folder.py +
      

    A hidden file should be created in ~/home, named .window_list.txt. You will need to press Ctrl+H to make it visible (files with a name, starting wit a '.' are invisible by default)

    If it works fine, add both commands:

       python3 /path/to/add_folder.py +
       python3 /path/to/add_folder.py -
    

    to a shortcut key combination (I used Ctrl+Alt+R and Ctrl+Alt+A): choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command(s)

  3. Copy the script below into an empty file, save it as set_view.py This is the background script to change the window's view to either list view or icon view.

    #!/usr/bin/env python3
    import subprocess
    import time
    import os
    wlist = os.environ["HOME"]+"/.window_list.txt"
    
    def get(cmd):
        try:
            return subprocess.check_output(cmd).decode("utf-8")
        except subprocess.CalledProcessError:
            return ""
    
    def check_window():
        pid = subprocess.check_output(["pidof", "nautilus"]).decode("utf-8").strip()
        wlist = get(["wmctrl", "-lp"]).splitlines()
        front = get(["xdotool", "getactivewindow", "getwindowname"]).strip()
        return (front, [w for w in wlist if all([pid in w, front in w])])
    
    match1 = check_window()
    
    while True:
        time.sleep(1)
        match2 = check_window()
        if all([match2 != match1, match2[1] != []]):
            w = match2[0]
            try:
                if w in open(wlist).read().splitlines():
                    cmd = "xdotool key Ctrl+1"
                    subprocess.Popen(["/bin/bash", "-c", cmd])
                else:
                    cmd = "xdotool key Ctrl+2"
                    subprocess.Popen(["/bin/bash", "-c", cmd])
            except FileNotFoundError:
                pass
        match1 = match2
    

    Test run it by running from a terminal window:

    python3 /path/to/set_view.py
    

    Navigate through your folders and add / remove folders to the list-view list as described in 2. (remember, the list view will be activated the next time you open the folder)

That's it!

Note

Since the script does not see if the listed folders still exist in your directories, the file ~/.window_list.txt might include some outdated entries over time if you delete listed folders.

You can prevent dat by either use the shortcut you set to do so, or simply walk through the file every once and a while.

  • From a linux novice - could you explain what is being done in the script? and if it will slow down nautilus when opening directories with large number of files (like the stereotypical Downloads folder) – 3l4ng Feb 4 '16 at 17:00
  • @3l4ng what happens is exactly what happens if you press Ctrl + 1 or 2 (the nautilus keyboard shortcut to set either list- or icon view), simply because that is what the script simulates, depending on the window title. You would have yo switch anyway, so the speed is not something the script determines. Let me know if you need any help. – Jacob Vlijm Feb 4 '16 at 17:12
  • @3l4ng added a few sections to the answer. – Jacob Vlijm Feb 4 '16 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.