t0mmyw
t0mmyw

Reputation: 737

Can Intellij open files for editing at the top/start, not the last edit position

If you open a big java file, scroll to 'somewhere' and make a change. Then close the file and re-open it, Intellij will open it at the last point that you made the change. This is annoying, can it be changed so it always opens the file at the top like most file reading applications do

Upvotes: 2

Views: 225

Answers (2)

Ti Strga
Ti Strga

Reputation: 1390

This same misfeature annoyed me to the point of doing something about it, as I prefer to open files by double-clicking them in the Project tree (which doesn't present the option of typing in a line number). Also, it bothers me to be typing in line numbers on every single file option simply to get the text editor to not jump to wherever I was editing six months ago.

Sadly, there's still no option to toggle this behavior. (JetBrains really, really doesn't like it when users prefer simpler behavior than their flagship defaults.) But it's very easy to strip the "last edited position" history out of the saved workspace.

Optional first step: If you have more than one workspace, you need to find its configuration file. Wherever you designated the project root location should contain a .idea subdirectory with a workspace.xml file, for example $HOME/IdeaProjects/MyProjectName/.idea/workspace.xml. There will be a ProjectId key and some "nonsense looking" value, for example

<component name="ProjectId" id="wZadhKS8vnOD4GBBT2Pz93rDw" />

You'll need that unique ID.

Actual steps:

  1. Exit IDEA. You can't do this while the IDE is running.

  2. Go to your personal IDEA directory. This can vary based on version; for me it is currently %HOME%/.IdeaIC2019.3. It will have a config/workspace subdirectory containing an XML file for each of your workspaces, named after the ProjectId above. For example,

    $HOME/.IdeaIC2019.3/config/workspace/wZadhKS8vnOD4GBBT2Pz93rDw.xml

For 2020.1 and later, this location has moved; for me the default is now %APPDATA%/JetBrains/IdeaIC2020.1 and there is no config subdirectory, so: C:/Users/me/AppData/Roaming/JetBrains/IdeaIC20201./workspace instead.

That XML file contains the saved last-edit positions in a node like this:

<component name="editorHistoryManager">
this is all the stuff that causes annoyances
</component>
  1. Make a backup copy of the wZa...3rDw.xml file, whatever it's called for you.

  2. Use your favorite programmatic XML editor tool to remove that node. For example:

    xmlstarlet ed --omit-decl -d '//component[@name="editorHistoryManager"]' wZa...3rDw.xml > tmp

    mv tmp wZa...3rDw.xml

  3. The next time IDEA is launched, all files will open at their beginning, the way God and nature intended.

  4. For bonus points, automate the above in a script that runs behind the scenes as appropriate. :-)

IDEA does something a little unusual with its XML involving whitespace, and tools like XMLStarlet often do something else. As XML is whitespace agnostic this makes no difference at runtime, but it does mean that if you want to compare for correctness or you're keeping the IDEA configuration in revision control, there will be a lot of extraneous "churn" in the diffs. If this causes too much noise, you can augment step (4) to something like

xmlstarlet ....  |  sed -e 's@"/>$@" />@'  > tmp

to insert extra whitespace back, in most of the places where IDEA had originally put some. (I didn't test this heavily, as the lack of whitespace isn't important to me or to the IDEA runtime. It would have been nice to have a cleaner diff, but whatever.)

Caveats: As IDEA can save its config in either a directory-based layout, or in a all-in-one-file layout, the steps to find the workspace config file may vary wildly. What I wrote above works for the default directory-based layout.

Upvotes: 0

Morfic
Morfic

Reputation: 15518

I don't really remember seeing such an option, but you can work around with some small tricks.

1) include line number of the file, eg to open MyUI at line 10: CTRL + N & type myui:10

navigate to line

2) navigate to symbol, et to navigate to method init of MyUI: CTRL + SHIFT + ALT + N & type myui.init

enter image description here

Upvotes: 3

Related Questions