View Full Version : [ubuntu] Emacs question

August 16th, 2010, 04:58 AM
Hi I am using EMacs 23 on ubuntu 10.04. Is there any way to open any text file in emacs in the READ ONLY mode through the terminal.

I know that after opening a text file in emacs I can use the key combination C-x C-q to put it in read only mode. But I want the read only mode to be 'on' the moment my emacs window opens. Is there any terminal command for that?

August 16th, 2010, 05:05 AM
emacs --funcall toggle-read-only

August 16th, 2010, 05:17 AM
That works thanks....but it is too long a command....could there be any shorter command.... This is the lazy side of me speaking :)

August 16th, 2010, 07:01 AM
You can make your own command. In your root directory, open a file called .bashrc in an editor. Below is an example for a command called "emacsread", simply add this line to your bashrc

alias emacsread="emacs --funcall toggle-read-only"

save it, and restart your terminal. You should be good to go. Now you can just type emacsread and it will automatically enter the full command for you.

August 16th, 2010, 07:13 AM
Oh thats nice! But I had a small difficulty

Say I have a file named foo.txt and I want to open it in read only mode.

Then I found I need to sandwich the name 'foo.txt' between emacs and

--funcall toggle-read-only for the command to work.

Note that placing foo.txt at the end of the string emacs --funcall toggle-read-only does not work and opens foo.txt in normal mode.

How should I modify what you just posted and put it in my .bashrc file?

August 16th, 2010, 07:55 AM
I was bored, so I solved your "problem." :D

Make a file called emacs-ro, make it executable (chmod +x path/to/emacs-ro), and put it somewhere in your $PATH, e.g. /usr/bin

The file should have these contents:

while [[ $argnumber > 0 ]]
argnumber=$(( argnumber - 1 ))
arglist=$arglist\ `echo ${BASH_ARGV[argnumber]} | sed 's/ /\\ /g'`
arglist="$arglist --funcall toggle-read-only"

All this script does is take the list of arguments it receives and call emacs with that list of arguments followed by --funcall toggle-read-only. It does *not* treat arguments with spaces in them correctly, for reasons that I cannot fathom. Has something to do with the inconsistent way bash passes arguments when doing parameter expansions.

But for example, "emacs-ro -nw" should work fine.

Hope that works for you!

August 16th, 2010, 09:23 AM
Hats off bro!!!, this worked perfectly! Thanks a lot!

August 17th, 2010, 01:17 AM
A better and just slightly shorter script:

emacs "$@" --funcall toggle-read-only


Courtesy of Programming Talk post about arguments with spaces. This one will handle those correctly, and is obviously shorter.