3

I want to set an alias to start nedit together with command line option -noautosave (due to text files up to 500MB). What seemed to be easy:

alias nn="nedit -noautosave $1 &"

just raises some error "Permission denied" of a different file and another error about unexpected EOF while looking for matching "'" and "unexpected end of file".

One solution I found after a Google Search would be to check the quotes, but I can't see any possible errors with them.

I also tried declaring a function:

function nn () { nedit -noautosave $1 &}

which also failed with same errors.

  • Does nedit -noautosave work just from the shells? The function does not seem to be the problem to me. – Bernhard Sep 5 '13 at 11:18
  • Well, thanks, I forgot to mention that. I tried to do it that way (aka manually), and there is no problem at all, autosave is turned off and nedit is working as desired. – Soum Vhon Sep 5 '13 at 12:06
2

If you only want to run nedit with -noautosave when one of the files to be opened is larger than a given size, try this (I am using 100M but you can set your own size limit):

function nn() { 
    big=0;
    let big+=$(find "$@" -size +100M|wc -l)
    if [ $big -gt 0 ]; then
     nedit -noautosave -- "$@" &
    else
     nedit -- "$@" &
    fi
}

From man nedit:

   --  Treats all subsequent arguments as file names,
       even if they start with a dash.  This is so NEdit
       can access files that begin with the dash
       character.
  • Would also work with let big+=$(find "$@" -size +100 M|wc -l) (no need for the for loop). – Joseph R. Sep 5 '13 at 13:05
  • You can even do if [ ! -z $(find "$@" -size +100M) ];then – Joseph R. Sep 5 '13 at 13:07
  • 1
    @JosephR. yes the for was there cause I had started writing it using a different approach (I was trying to use && and find's exit codes but they don't work that way) and the for stayed. Answer edited, thanks. – terdon Sep 5 '13 at 13:12
  • @terdon I don't know exactly why, but your solution works for me, although the syntax is the same as in slm's answer. Weird... Anyway thanks for your answer, especially because it is very handy to automatically switch the parameters depending on the size of the file. I would love to upvote, but my reputation is too low, yet ;) – Soum Vhon Sep 5 '13 at 14:16
  • @SoumVhon you're welcome. Please remember to accept one of the answers (either mine or slm's from whose answer I shamelessly copied) so the question can be marked as answered. – terdon Sep 5 '13 at 14:18
2

You can't pass arguments into an alias. You'll have to use a function, so you were on the right track. You just had a typo in your use of the function command. Either of these will do, however:

$ nn () { nedit -noautosave -- "$@" & }

-or-

$ function nn() { nedit -noautosave -- "$@" & }

If you need to remove it, use the unset command, i.e. unset -f nn.

Also I'd use "$@" instead of $1, on the off chance that you want to open a series of files in this manner.

excerpt from bash man page

@      Expands to the positional parameters, starting from one.  When the 
       expansion occurs within double quotes, each parameter expands to a 
       separate word.  That is, "$@" is equivalent  to  "$1" "$2" ...  If 
       the double-quoted expansion occurs within a word, the expansion of 
       the first parameter is joined with the beginning part of the original 
       word, and the expansion of the last parameter is joined with the last
       part of the original word.  When there are no positional parameters,
       "$@" and $@ expand to nothing (i.e., they are removed).

Example

$ function nn() { nedit -noautosave -- "$@" & }

$ nn ~/.bashrc 
[3] 19830
  • Thanks for your answer, but this won't work for me. The same errors appear. What do the separate '--' stand for? Perhaps the error lies in some settings of my bash environment... – Soum Vhon Sep 5 '13 at 9:49
  • Nedit is installed. I wrote a similar function that just opens nedit with the specified file (just to have the '&'), and this works pretty well. But when opening a 500MB text file and nedit saves after every 80 or so strokes, editing can become very annoying. A workaround would be to increase the amount of strokes after nedit autosaves, but this would be a global setting. – Soum Vhon Sep 5 '13 at 10:17
  • 1
    @SoumVhon - This is sounding more like an XY Problem. What is your real issue, nedit slows down when -autosave is enabled after some period of time? – slm Sep 5 '13 at 10:41
  • I see what you try pointing to. But there is no problem with nedit's autosave capability nor it's speed (which is, when it comes to saving, only limitated by network and/or hard drive speed), I just want to create a program shortcut/alias that uses the -noautosave parameter to disable autosave completely (enabled by default). Only when there is now way of creating such a shortcut (and that seems to be the case on my system, even when I try your suggested solution), I either have to alter nedits configuration to get the behavior I am looking for, or just do it manually every time I need it. – Soum Vhon Sep 5 '13 at 12:02
  • Oh, just in case, I don't want to switch off autosave completely, just sometimes when I have to handle big files. If there is a smarter/easier way to achieve this goal, I would be happy to hear. – Soum Vhon Sep 5 '13 at 12:11
1

If the whole point of the alias is to apply the -noautosave option to any file you edit, then

alias nn='nedit -noautosave'

should be enough. You can then use it as

nn your_file &
  • Thanks for your answer, this one is working, although is was hoping for a solution that uses the '&' so that I don't have to type it by myself :) – Soum Vhon Sep 5 '13 at 12:17
  • @SoumVhon In that case, an alias is definitely not the answer and you should go with slm's solution after figuring out why it doesn't work. – Joseph R. Sep 5 '13 at 12:44
1
alias nn="nedit -noautosave $1 &"

The variable expansion is interpreted inside double quotes. If you ran that from your .bashrc or at your shell's command line, there were no positional parameters (they would be the parameters passed to the shell), so $1 expanded to the empty string, and the alias value is nnedit -noautosave &. Therefore, when you ran nn /path/to/file.txt, that was expanded to nnedit -noautosave & /path/to/file.txt. The error message “Permission denied” was because /path/to/file.txt is not executable.

You could have used alias nn='nedit -noautosave $1 &' to prevent the expansion and retain $1 inside the alias. However this wouldn't have worked any better, because aliases don't take arguments, they are substituted in place. So $1 would have been replaced by the first positional parameter at the time the alias was expanded, which is no better than before.

function nn () { nedit -noautosave $1 &}

That's basically correct. If you got the same errors as before, it's because the alias nn was still defined, and an alias takes precedence over a function of the same name. Use unalias nn to undefine the alias.

The function should be written this way:

function nn () { nedit -autosave "$@" & }

The double quotes are necessary in case there are spaces or wildcard characters in the file name. Using "$@" instead of "$1" lets you pass multiple arguments.

  • if you wanted to you could've morphed my answer into this one if you wanted to. I understand it's a bordeline call whether to do this but now we have 2 answers on this question that are very similar. Let me know your thoughts on this, not just for here but for others A's too in the future. – slm Sep 5 '13 at 21:49
  • @slm Uh? Your answer doesn't explain why his alias and his function (which was correct) failed. The intersection of our answers is about two sentences (“you can't use an alias” and “use "$@" instead of $1”). – Gilles Sep 5 '13 at 21:52
  • OK. Was just a thought. – slm Sep 5 '13 at 21:53
  • @Gilles I understand now that aliases are limited when it comes to arguments, parameters and variables, and therefore a function should be used in my case. What I don't get is why it fails when I define it the way you recommend. If I don't define an alias with the same name at all, I don't need to unalias it, correct? So there should be no interference from the very beginning. Most likely, the problem lies somewhere else, e.g. global bash settings (workplace computer, huge network). – Soum Vhon Sep 6 '13 at 8:14
  • @SoumVhon If you haven't defined an alias with the same name, the function should work. There may be some weird setting somewhere that causes strange behavior, but it really looks like the alias is still being invoked if you get the same errors. Run set -x first to see what commands bash runs. Try without the rest of your .bashrc. – Gilles Sep 6 '13 at 8:21

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.