1

How can I have emacs start and be in the middle of a command input? Particularly, I want emacs to start in the middle of a command input find-file with a message in the small buffer saying:

Find file: ~/

and the cursor at the last character of it so that I can continue typing the remaining path to open the file I want.

dkim
  • 3,930
  • 1
  • 33
  • 37
sawa
  • 165,429
  • 45
  • 277
  • 381
  • Currently, it starts in the working directory of the current file, or $HOME if you're not in any file doesn't it? – Squidly Aug 23 '12 at 13:08

2 Answers2

3

You can execute one of the following commands on the command prompt or make a shell script containing it appropriately:

$ emacs -f find-file         # if you want to start Emacs in the current direcoty
$ (cd ~; emacs -f find-file) # if you want to start Emacs in your home diretory

From the emacs(1) man page:

-f function, --funcall function

Excute the lisp function function

dkim
  • 3,930
  • 1
  • 33
  • 37
1

I have to admit that my lisp is a bit rusty, but this works for me. Drop it in your ~/.emacs file (or whatever init file you are using):

(add-hook 'emacs-startup-hook
          (lambda ()
            (if (= (length command-line-args) 1)
                  (call-interactively 'find-file))))

If you call emacs with no arguments, like this:

sawa@localhost:~$ emacs

It will invoke find-file for you. If, on the other hand, you invoke emacs with an argument, such as a filename, like this:

sawa@localhost:~$ emacs somefile.txt

It will default to just visiting somefile.txt

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • `emacs --eval '(dired ".")' -f find-file` will do the same thing although I'm not sure about why to want to run both `dired` and `find-file`. – dkim Aug 23 '12 at 14:25
  • @DeokhwanKim It was my mistake to mention dired. It actually wanted find-file. – sawa Aug 23 '12 at 14:29
  • Thanks, I like this answer because I can write that in the init file rather than doing shell scripting outside of emacs. – sawa Aug 23 '12 at 14:35