How can I code a function to open the same relative file (for example, server/logfile.txt) from a project? So, this should be relative to the project I'm in.
projectile-find-file list all of them, not only the one which we're interested to open...
You can use the projectile-project-root function to expand paths relative to your projectile project's base directory, eg.
(when-let ((root (projectile-project-root)))
(find-file-other-window (expand-file-name "server/logfile.txt" root)))
or (the when-let, if-let macros are from subr-x included in recent emacses)
(if-let ((root (projectile-project-root)))
(find-file-other-window (expand-file-name "server/logfilt.txt" root))
(user-error "not projectile project found"))
Perhaps a non projectile way would be to use bookmarks feature of emacs? For an already defined bookmark C-x r b would take you to the file and offset within it?
Related
I work on many different machines and VMs, each of which might have their own needs regarding options for .emacs. I have a Git repository / subdirectory .common-system with a second Emacs Lisp file .common-system/emacs containing common definitions used for all my systems.
What changes can I make to .emacs which will load and eval my secondary definition file automagically? Bonus points for keeping the secondary file out of my buffer list after startup.
Have you tried:
(load-file ".common-system/emacs")
It works better (as Stefan suggests) if you specify the full path to the file, something like
(load-file "~/.common-system/emacs")
If you stick with you current repository layout, using load-file (as suggested by others) is the way to go.
However, if you name your top-level directory .emacs.d and place the file init.el in it, Emacs will find it and run it automatically. That way, you don't have to hand-edit a .emacs file on each system you use.
You use a (cond), dispatching on variables such as user-login-name, system-type and system-name
(cond
((equal user-login-name
"myname")
;; Stuff
)
((equal system-type 'gnu/linux)
(require 'some-file)
;; some-file, could be any file in load-path, named some-file.el, and ending
;; with (provide 'some-file)
))
Emacs allows opening a directory and choosing files. After going one step up or down in the directory, the control moves to another split window. My problem how to make the control in same window?
Given this also how to open files in specific split windows?
If you are using dired, you can use ^ to include an upward directory and i to include a subdirectory. Jump between directory entries with < and >.
In dired-mode, you can press i (dired-maybe-insert-subdir) while point is over a directory to insert that directory's contents into the same buffer (rather than opening a new buffer).
In a buffer with multiple directories open, press C-M-p (dired-prev-subdir) while point is in an opened subdirectory to jump up to previous directories in the buffer.
You mentioned you wanted to insert subdirectories into the dired buffer with a right click. Here's some code to do that:
(add-hook 'dired-mode-hook
(lambda ()
(local-set-key [mouse-3]
(lambda (click)
(interactive "e")
(goto-char (posn-point (event-start click)))
(call-interactively #'dired-maybe-insert-subdir)))))
I know this is kind of minor, but it's been bugging me. I'm using Org-mode for a project and I tend to export to either PDF or HTML rather frequently and it leaves my directory littered with PDF, Tex, and HTML files. Is there a way to have Org-mode export to another location, perhaps a subdirectory called ./exports?
In addition to the use of publishing by modifying your org-publish-project-alist variable as #user1248256 suggested, you can directly specify the org-export-publishing-directory variable within your file:
#+bind: org-export-publishing-directory "./exports"
* This is a test headline
Some text here. This should be exported to the "./exports" directory.
Upon export it will be placed in the "exports" directory, but only if that directory exists. If it does not exist, you will get an error message in the console.
The original question referred to exporting of org-files, while most answers above actually have to do with publishing, which is a different concept.
I believe the best way to solve the problem posed by the OP is to add the following to your emacs initialization file (.emacs):
(defadvice org-export-output-file-name (before org-add-export-dir activate)
"Modifies org-export to place exported files in a different directory"
(when (not pub-dir)
(setq pub-dir "exported-org-files")
(when (not (file-directory-p pub-dir))
(make-directory pub-dir))))
PS:
I realize a 5 year old question might no longer be relevant to the OP, but hopefully people searching for similar stuff will benefit from this answer.
This is a slight modification of a code snippet found in http://rwx.io/posts/org-export-configurations/
The original solution found in the above blog allows for setting up different directories for each exported format. However, if the goal is to avoid having
one's directory "littered with PDF, Tex, and HTML files", I think it is best to have only one directory containing exported files of all formats, which is the essence of the modification I offered above.
Edit: The emacs manual (https://www.gnu.org/software/emacs/manual/html_node/elisp/Porting-old-advice.html#Porting-old-advice) states that the defadvice mechanism was made obsolete by the new advice-add. So here is a code snipet with the same effect, using the recommended advice-add:
(defun org-export-output-file-name-modified (orig-fun extension &optional subtreep pub-dir)
(unless pub-dir
(setq pub-dir "exported-org-files")
(unless (file-directory-p pub-dir)
(make-directory pub-dir)))
(apply orig-fun extension subtreep pub-dir nil))
(advice-add 'org-export-output-file-name :around #'org-export-output-file-name-modified)
As before, this should be placed in your .emacs file.
This probably wasn't possible when the question was first asked, but the simplest solution would be to add the directory to the :EXPORT_FILE_NAME: property:
:PROPERTIES:
:EXPORT_FILE_NAME: exports/<filename>
:END:
Just as in the accepted answer, the directory must exist in order for this to work.
You have to put the following line at the beginning of your org file :
#+EXPORT_FILE_NAME: PATH/filename
Where PATH is the path to the folder where you want your file to be exported (e.g. ~/exports) and filename the name you want to give to your exported file (e.g. tutorial.html).
I believe you can get that with org-publish.
Add to you emacs configuration file something like that:
(setq org-publish-project-alist
'(("html"
:base-directory "~/org/"
:base-extension "org"
:publishing-directory "~/org/exports"
:publishing-function org-publish-org-to-html)
("pdf"
:base-directory "~/org/"
:base-extension "org"
:publishing-directory "~/org/exports"
:publishing-function org-publish-org-to-pdf)
("all" :components ("html" "pdf"))))
Eval this expression (or restart emacs), press C-c C-e X at org-mode, then choose a project from a list.
You can see more information at http://orgmode.org/worg/org-tutorials/org-publish-html-tutorial.html and http://orgmode.org/manual/Publishing.html#Publishing
As stated in the section "Export settings", we can use the EXPORT_FILE_NAME within a file in order to set the output directory. The quote shown below is the relevant part of the documentation
‘EXPORT_FILE_NAME’
The name of the output file to be generated. Otherwise, Org generates the file name based on the buffer name and the extension based on the back-end format.
I work on an open source project where the creator sets his tab-indents to 2 spaces.
I'd like to just enable it on the fly for the one file I work on and not other files of the same type. There must be something like M-x set-tab-indent. It is a JavaScript file ending in .js.
I know I can use:
(setq-default tab-width int)
inside my .emacs file, but I rather just call an M-x command to set it and forget it during my duration of working on this file. I tried M-x apropos and Google but couldn't find the specific command.
Thanks.
You can make the variable js-indent-level local to the buffer using:
M-x make-variable-buffer-local <RET> js-indent-level <RET>
Then you can set that variable in the buffer using:
M-x set-variable <RET> js-indent-level <RET> 2
The easiest way to do this for a single buffer is to use M-x set-variable.
Type M-x set-variable and press enter
When prompted for the variable to set, set tab-width then press enter
You'll be prompted with the line Set tab-width (buffer-local) to value:.
Put the value you want, then hit enter
The buffer should instantly be updated with the new value.
You could also use file local variables to automate omrib's solution for that one file, by adding this to it:
// Local Variables:
// js-indent-level: 2
// indent-tabs-mode: nil
// End:
Create a file ".dir-locals.el" in the project's directory and fill it like this:
((nil . ((tab-width . 2))))
This will take care of setting tab-width automatically and you don't have to modify the actual file (which is likely version-controlled.)
See the manual for more information about the format. I believe this requires Emacs 23.
As indicated by others, one issue with the File Local Variables approach is that you need to modify the file, and that's not ideal if you need to keep those declarations out of version control.
If you want the variables to apply to all files under a given directory, then Directory Local Variables is obviously the way to go, and you can implement that with either a .dir-locals.el file, or by calling (dir-locals-set-directory-class):
http://www.emacswiki.org/emacs/DirectoryVariables
http://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html
I prefer the directory class approach myself, and I was thinking that it's a shame that there isn't an analogous approach for file local variables, but I found that the directory class code actually works perfectly with files, and the only issue is that dir-locals-set-directory-class calls file-name-as-directory on its argument, which prevents it from being matched, due to the trailing slash.
The following therefore is a way to configure directory local variables for a single file, without modifying the file itself, or affecting other files under the same parent directory.
(defun my-file-locals-set-directory-class (file class &optional mtime)
"Enable 'directory local' classes for individual files,
by allowing non-directories in `dir-locals-directory-cache'.
Adapted from `dir-locals-set-directory-class'."
(setq file (expand-file-name file))
(unless (assq class dir-locals-class-alist)
(error "No such class `%s'" (symbol-name class)))
(push (list file class mtime) dir-locals-directory-cache))
(dir-locals-set-class-variables
'my-javascript-class
'((nil . ((js-indent-level . 2)
(indent-tabs-mode . nil)))))
(my-file-locals-set-directory-class
"path/to/the/file.js" 'my-javascript-class)
I use a snippet of code in my init.el that tries to auto-detect files that use 2-space indents, and switch Emacs's indentation for that file to 2 spaces when it sees such files:
(add-hook 'js-mode-hook
(lambda ()
(when (string-match-p "^ [A-Za-z]" (buffer-string))
(make-variable-buffer-local 'js-indent-level)
(set-variable 'js-indent-level 2))))
When I start working on a project in emacs, I use M-x cd to get into the project root directory. But every time I use C-x C-f to open a file in one of the subdirectories (like app/model/Store.rb) emacs changes current directory to that of the file. Is there a way to make emacs stay at the root?
How about this? It replaces the regular find-file command with your own which always starts in some "root" directory (customize the find-file-root-dir variable):
(defvar find-file-root-dir "~/"
"Directory from which to start all find-file's")
(defun find-file-in-root ()
"Make find-file always start at some root directory."
(interactive)
(let ((default-directory find-file-root-dir))
(call-interactively 'find-file)))
(global-set-key (kbd "C-x C-f") 'find-file-in-root)
Assuming that you want the working directory of a file to be set to whatever the working directory was before you executed find-file, you could try the following:
(defmacro disallow-cd-in-function (fun)
"Prevent FUN (or any function that FUN calls) from changing directory."
`(defadvice ,fun (around dissallow-cd activate)
(let ((old-dir default-directory) ; Save old directory
(new-buf ad-do-it)) ; Capture new buffer
;; If FUN returns a buffer, operate in that buffer in addition
;; to current one.
(when (bufferp new-buf)
(set-buffer new-buf)
(setq default-directory old-dir))
;; Set default-directory in the current buffer
(setq default-directory old-dir))))
Armed with this macro, go search for operations that set the variable default-directory: M-x find-library files; M-x occur (setq default-directory. After some investigation, you discover that the desired function is called find-file-noselect-1. Also, it looks like set-visited-file-name is also a candidate. So:
(disallow-cd-in-function find-file-noselect-1)
(disallow-cd-in-function set-visited-file-name)
Note
Note that (disallow-cd-in-function find-file) would work just fine, but then if you switched to ido-mode, you'd be opening files with ido-find-file instead of find-file. Both of these functions ultimately use find-file-noselect-1, so hitting that with the macro is a more univeral solution.
Is there a way to make emacs stay at the root?
No, there isn't. C-x C-f always visits starting from the default directory of the buffer you are already vising. The default directory, by default, is the same directory as the file. You can change these (separately for every buffer) using M-x cd.
But that is not what you want. What you should do is C-x b to *scratch* (whose default directory is the same as where you launched Emacs from -- in your words "root"), and then visit a new file. And if you need to do this frequently, just open up a dired in there and work your way thru.
I appreciate I'm not answering your question directly, but I noticed you were more specific in your requirements in one of your comments: "I don't use compile or recompile, I just tend to close files I am not working on, since it takes fewer keystrokes to open a file again".
Have you got ido turned on for buffer switching? If you exclude the directory thing for a moment, switching files or buffers with ido is an identical number of keystrokes (C-x C-f vs C-x b, followed by a few characters in the file name). If you include the directory thing, switching files is more tricky for the precisely the reasons you mention. Sticking with buffers is much easier.
Going a step further, with the help of 'anything.el' it's quite easy to abstract away whether a given file is in a buffer or in a file using the file cache. For example, if you do the following:
(file-cache-add-directory-recursively "/my/ruby/project") ".*\\.rb$")
and run 'anything-for-files' (I have it bound to C-x f) all your open buffers are listed, along with all of the files you've just added to the file cache; isolating a given file usually only takes one or two more characters.
Any file in your project is thus 4 or 5 key presses away, and the directory they are in or whether or not they are in a buffer becomes irrelevant.
Hope that's helpful...
Sorry I haven't worked out the details, but you might be able to add a function to find-file-hook that resets the default directory to whatever you want.