How do I reveal a file as "selected" in *nix from the command line? - linux

Is there a semi-universal mechanism by which to reveal files as selected in various *nix window managers via the command line? For example, in Windows I can say the following:
explorer.exe /select,C:\TestDir\TestFile.txt
…and Explorer will reveal the file and select it for you. In OS X I can say the following:
osascript -e 'Tell application "Finder" to reveal "MacHD:Users:myaccount:Desktop:filename.txt"'
…and it will do the same. My question is, is there any way to do the exact same thing (somewhat universally) in any of the various popular *nix flavors across window managers? Obviously "Open Containing Folder" is simple enough, but I want to go the extra step of actually opening it with the specific file selected. Any assistance is appreciated.
Best.

I don't about other file managers (other answers can add that) but for nautilus it's been recently fixed
This allows to call nautilus
uri:///path/to/file from the command
line to open uri:///path/to with file
pre-selected.

For OSX AppleScript works for all versions, but if you know you'll be dealing with 10.6 or later you'd be better served by using the -R option for "open". It's around 30 times faster.
open -R "/Volumes/Users/Desktop/file-to-open.txt"
For Linux Nautilus allows for direct calling of the file, a generic solution for GNOME (you won't find one for "Linux") is the "gnome-open" command, which currently could open the directory but won't highlight the file:
"gnome-open /tmp/file.txt"

Related

How does File Explorer open files?

It's maybe stupid question, but what happens behind the scenes when i double click the word app, or .exe app?
For 'Non-Developer' it just opens the file in right environment after doubleclicking. But I would like to know, how is it done, how can the file manager know what to open? (.docx in word, .txt in texteditor,etc...)
+ I would like to know how can I do that in Node.js, is it the best way to use child_process and if statements for every suffixes?
There is a file association to tell Windows how to treat a certain file extension. You can type assoc in a Windows Command Prompt to see them. As an example:
C:\test>assoc .txt
.txt=txtfile
So Windows knows now, that the file with the extension .txt is a txtfile.
ftype defines, how that filetype is to be handled:
C:\test>ftype txtfile
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
So whenever you doubleclick on a file, Windows checks if it is associated with any filetype. Then it looks up, how to handle that filetype and executes that command (in the example above, it opens Notepad with the filename as a parameter).
Both assoc and ftype are able to change the settings (for example to open .txt files with another editor). But if you try that, do yourself a favor and note the original settings, so you are able to revert your changes when needed.
The changes can also be done directly in the registry (not recommended, when you are not experienced in handling the registry)
For Node.js, you can use child_process to shell out to the start command which does similar things as the File Explorer.
E.g. start some/path/to/file.docx will open that file in the default program associated with the format.

How to open multiple files in PyCharm with one click?

I have a directory with multiple files. There is one main file and other ones in subdirectories (including in a virtual environment directory) all of which I want to open with one click in the same instance of PyCharm. Is there a way to do this? Is there a native PyCharm way? Is there a way to do it using a Bash script. For the latter I have managed using pycharm.sh (see Open files from the command line). However, the different files are opened in different PyCharm instances which is not what I want.
I just tried this on Windows running from CMD. Simply listing the paths to the files separated by a space opens them in a single editor instance. (Although the documentation doesn't clearly mention this possibility, suggesting only 1 single file/project as argument).
For example executing the following:
pycharm64.exe C:\test_file1.txt C:\test_file2.txt
Opens like this:
This means PyCharm does accept a list of individual files as command line arguments to open them in a single instance.
all of which I want to open with one click in the same instance of PyCharm.
If the above example works on your shell you should be able to create a shell script that can be clicked.
However, the different files are opened in different PyCharm instances which is not what I want.
I don't know if depending on the shell any special rule applies that might cause each file to be opened in a different PyCharm instance/window, but if that's the case there's also likely to be a shell specific syntax rule to launch a single instance of the application passing multiple arguments.
Is there a native PyCharm way?
It seems PyCharm is naturally geared to working with projects. Looking closely at some functionalities like open/close/search in the PyCharm IDE their logic is entirely "project oriented" not like the usual concept of opening a set of unrelated files as in some other editors. (This is actually really smart, it doesn't clutter the UI with marginal functionalities and it forces users to set up a project - see this thread for a similar example about using search with individual unrelated files in PyCharm).
If you look at the screenshot, it's noteworthy that only 1 file is listed in the Project Tool Window although several files are opened in the editor.

How to avoid .pyc file listing in open File Dialog sublime text

i am trying to find if any possible configuration is available in sublime-text editor that can ignore the listing of *.pyc files in OPEN FILE DIALOG because,
its problematic and slow when Press Ctrl+O and type file name and its select file with *.pyc format. and we require to select next file to to open actual *.py file.
I tried file names in ascending order in Open file Dialog that lists *.py files first & *.pyc second. but i work with files mostly starts with a,x,y,w and that's why i require solution.
Sublime uses the underlying Open dialog of whatever operating system you're running it on, which means that it's technically up to the operating system in general to provide the capability to provide that kind of filtering.
If you're using Windows, you can change the file filter to Python, which will exclude *.pyc files. You can also enter *.py in the file name box and hit enter to get the dialog to show you only files with that extension.
On MacOS you can enter *.py in the search box in the top right of the open dialog to find files of that type. As far as I'm aware that's always going to search your whole Mac for files of that type and not just the folder that you're currently looking in, though (I don't use this particular feature of MacOS).
On Linux, well, that's going to depend on your Linux distribution in general. On my particular Linux machine, the dialog has a Search button that works similar to the one on MacOS, finding all files everywhere that match instead of just filtering the current location. Other distributions may have something similar to this or Windows.

Is it possible to call an application selection window (Right click->Open With->Other) from the linux console?

On Gnome/KDE you can select in which application you want to open file (Right click on file -> Open With -> Other). Is it possible open file that way, but from console?
For example: you print " file.ext" and instead of opening in concrete application, there are that application selection window forced and then users chooses - starts selected program.
I tried to figure out that myself, but not found anything like that.
"edit file.ext" doesn't fits my needs, because it starts preferred application and you cannot choose which. And also on my desktop it says:
"Error: no "edit" mailcap rules found for type "image/jpeg"
So, am I able to forse that "open with" window from console? If yes, can you say how?
Both on windows and mac you can do such things.
//edit at 2009-02-10 14:17
Thank you very much for answers. Command will be used in program code, so unfortunately probably I would not be able to make some extra bash scripts.
For GNOME:
gnome-open <file>
For KDE:
kfmclient exec <file>
These commands should open up the <file> in the preferred application in GNOME or KDE respectively, although I don't have an installation of either to test on.
Take a look at man run-mailcap, you can change or add selected applications for each mimetype modifying the /etc/mailcap, ~/.mailcap files and some others.
Traditionally, on Unix systens (and therefore Linux, too), you start applications from the console (and not from a UI). The command line (or console) expects you to enter the name of the application and then the filename (plus some options).
This allows to use applications (or commands) in shell scripts.
On Windows, there is no real console (the DOS box is just a reminiscence of the dark ages of MS DOS). So the MS developers came up with the idea to have the OS treat anything as a command. If it's not a real command or application, the OS will determine the file type (by extension on Windows and by some header information on Mac). For each file type, there will be an associated application in a look up table.
This is why on Windows, it appears that you can enter the name of a file on the console and you will get the application to edit that file.
If you want a quick way to fix this in the Unix console, create a script called "open" or "o" and use the file command with the option --mime to identify the file type. You can then use a case statement to launch your favorite editor.
As for the error about "mailcap rules": There is a file called "mailcap" on Unix where you can define abstract "commands" (open, edit, view, print) for file types. See the mailcap man page.

Replace in multiple files - graphical tool for Linux

It needs to be graphical. No sed, awk, grep, perl, whatever. I know how to use those and I do use them now, but I need to cherry-pick each replace in 300+ files.
I want a tool where I can:
type a search string
type a replace string
select a directory and file extension
and it would recursively go into each file in that directory and its sub-directories, open it and scroll to the place where search string is and offer two options:
replace (and find next)
find next
Nothing more. Reg.exp. support is a plus, but not required.
SOLVED: Regexxer is exactly what I needed. In case someone needs it on Slackware, here's what you need to download and how to compile it (choosing correct version of each dependency can be a PITA)
I think regexxer is exactly what you're looking for:
Regexxer
regexxer is a nifty GUI search/replace tool featuring Perl-style regular
expressions. If you need project-wide substitution and you’re tired of
hacking sed command lines together, then you should definitely give it a try.
See also the screenshot, looks a lot like what you're describing:
Emacs + dired + query-replace-regexp
For complete recipe follow this link (it's rather long, covering all possible alternatives),
jEdit does exactly what you need. It is written in Java and works well in Linux, Windows and OS X (probably other operating systems also).
Lately Kate (if you use KDE) can do it, but in a very tricky way. Go to "Edit>Search in Files", and choose the folder within which your files exist.
The trick is that only after the search results appear, you will find a text box and a button called "Replace checked". This button will do what you want.
I use gVim for this task all the time. I open up all the files at once, then use the commands to perform a subsitution on each file, asking for confirmation. Generally I use < 20 files, so I open them as tabs and use this:
:tabdo %s/foo/bar/gc
gVim works fine on Windows :) My coworkers often use Textpad to do this same thing, but I'd say gVim is much more efficient at it.
If you are a KDE user there's also kfilereplace.

Resources