linux - Append text into a file while opened in an editor

07
2014-07
  • Arch

    Here is the scenario: A user creates a new file X.php let's say using nano. The user writes some content, saves the file (Stage S1) but does not exit the nano. He again make some changes and then exit the nano after saving the file again (Stage S2).

    Question: I've a piece of code(a trigger) that I need to inject in every php file in a directory. I'm able to do so. But in the upper scenario, as soon as I detect a new file(using inotifywait) is created (Stage S1), I inject my code into it. However, since nano was never closed,thus ,in the next save (Stage S2) my injected code gets overwritten by the new changes of user. How can I get rid of this issue?

    P.S.: The editor can be any text editor and I don't know the editor used by the user to create file.

  • Answers
  • mdpc

    I'm sorry but editors buffer the editing process in may different ways. Unless you generate your own editor with the desired architecture in mind and force everybody to use that particular editor, you're going to be sorely disappointed and go through a lot of complexities with the problem still remaining.

    Wrappers on the editors will not help this problem either. As long as a file is open logically (buffered mode) you will be doomed to failure.

  • Scott

    You need to modify your code injector process to act whenever a PHP file is modified or created, and make it smart enough to check to see whether your code is there, and insert it if it isn’t.  Otherwise you are vulnerable to the following scenario:

    1. User creates X.php, saves it, and exits the editor.
    2. Your process inserts your code.
    3. User edits the file, sees your code, says “WTF?” and deletes it (and saves the file).

  • Related Question

    osx - Text editor for Mac that will open any file
  • Dinah

    In Windows, every text editor I encountered allowed me to drag and drop any file type into it and it opened. Apparently, Mac app developers have a different philosophy because I can't find a simple free text editor for OS X that will do this. TextWrangler came highly recommended but it can't accomplish this simple feat. Can anyone suggest one?


    Edit apparently on at least some editors, there is a distinction between drag/drop in the document list area vs. the typing area. In addition to the accepted answer, this is one thing to be aware of. I still don't know why it doesn't work in TextEdit, but this is true for TextWrangler and Smultron.


  • Related Answers
  • 8088

    For Textwrangler - You just need to drag it to the Documents draw and a blah.hdl file opened perfectly well for me. It even will try to open binary files that way, but may ask you for an encoding to use...

    To change it asking what to do you can track down a setting in the Textwrangler Text Files preferences:

    prefs

    Also, works for me perfectly well (renamed a file blah.hdl and dragged) in Textmate. In fact I can drag anything into Textmate and it will "open" it (no snarky-ness intended ;-)

  • las3rjock

    Drag-and-drop works for MacVim. It also works in Aquamacs.

    Edit: Dragging-and-dropping a PDF into TextWrangler did not work. Apparently TextWrangler tries to figure out whether or not you should be opening that file in a text editor.

  • shuckster

    My vote is for jEdit.

    As a bonus it's Java-based, and therefore cross-platform for the times when you are forced to abandon your beloved Mac.

  • Benjamin Dobson

    Er, TextEdit. Drag any file onto the Dock icon and it will open it.

  • Diogo

    Try this: Vim (Vi IMproved) for Mac OS X

    Vim is a highly configurable text editor, freely available for many different platforms. For general information, advocacy, and the latest news on Vim in general, visit the Vim Home Page. This site is specifically devoted to Vim on the Macintosh. This page is mainly devoted to binary distributions for Mac OS X.