Loading ...
Sorry, an error occurred while loading the content.

Re: Incorrect working directory in gvim launched from Konqueror

Expand Messages
  • Jean Johner
    Hello, Thank you for your answers. In summary: - Using autochdir is better than nothing, with the complications described by Marco - Using au VimEnter * if
    Message 1 of 19 , Mar 1, 2011
    • 0 Attachment
      Hello,
      Thank you for your answers.
      In summary:
      - Using "autochdir" is better than nothing, with the complications
      described by Marco

      - Using
      au VimEnter * if expand('%') != ""' | cd %:h | endif
      (note that the double quote must be doubled)
      solves the problem (don't ask me why). Thank you Jürgen.

      - I see no fatality why gvim is unable to have this behaviour by
      default. Launching Nedit or emacs in the same conditions results in
      the correct working directory. I will submit the problem to vim_dev
      (did you eNG1Ne ?)

      Best regards.

      Jean Johner


      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php
    • Jürgen Krämer
      Hi, ... actually, there were no double quotes in the original command, just two strings with single quotes: % and the empty string. ... The VimEnter event is
      Message 2 of 19 , Mar 1, 2011
      • 0 Attachment
        Hi,

        Jean Johner wrote:
        >
        > Thank you for your answers.
        > In summary:
        > - Using "autochdir" is better than nothing, with the complications
        > described by Marco
        >
        > - Using
        > au VimEnter * if expand('%') != ""' | cd %:h | endif
        > (note that the double quote must be doubled)

        actually, there were no double quotes in the original command, just
        two strings with single quotes: '%' and the empty string.

        > solves the problem (don't ask me why). Thank you Jürgen.

        The VimEnter event is triggered after all configurations file have been
        read and the first file, if any, has been loaded. If there is at least
        one file the % register contains its name. %:h gives the path to this
        file. %:p:h might actually be a better solution, because this takes care
        of file names without a path. On non-Unix-system this is no problem
        because :cd only prints the current directory if called without a path,
        but on Unix-systems :cd without a path changes the current directory to
        the user's home directory.

        > - I see no fatality why gvim is unable to have this behaviour by
        > default. Launching Nedit or emacs in the same conditions results in
        > the correct working directory. I will submit the problem to vim_dev
        > (did you eNG1Ne ?)

        I like the current behaviour. It's easier to change to the directory of
        a loaded file once you are inside Vim than to go back to the directory
        Vim was started from once the current directory has changed. I have
        defined two commands for this task:

        :command -bar -nargs=0 CD cd %:p:h
        :command -bar -nargs=0 LCD lcd %:p:h

        Also the current directory is much more predictable for this use-case

        vim **/files-in-subdirectories.txt

        ;-) It's still the directory from where you have issued the command and
        not some semi-random subdirectory.

        Regards,
        Jürgen

        --
        Sometimes I think the surest sign that intelligent life exists elsewhere
        in the universe is that none of it has tried to contact us. (Calvin)

        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php
      • Jean Johner
        Hi Jurgen, Thank you for your explanations. I can understand your preferences but consider the following facts: - launching gvim from the command line in a
        Message 3 of 19 , Mar 1, 2011
        • 0 Attachment
          Hi Jurgen,

          Thank you for your explanations.

          I can understand your preferences but consider the following facts:

          - launching gvim from the command line in a subdirectory always
          results in the subdirectory to be the working directory.
          I see no reason why double-clicking in Konqueror should behave
          differently. In most situations, people launching gvim in a
          subdirectory want to open other files in the same directory. By the
          way, other people in this thread seem to suffer from the present
          situation.

          - The behaviour would be consistent with the Windows behaviour

          - The behaviour would be compatible with the behaviour of almost all
          other text editors in KDE (Emacs, XEmacs, Nedit, Kwrite, Kate). An
          exception is Kedit which behaves like gvim.

          Perhaps we could vote.

          Best regards

          Jean Johner









          On Mar 1, 3:09 pm, Jürgen Krämer <jottka...@...> wrote:
          > Hi,
          >
          > Jean Johner wrote:
          >
          > > Thank you for your answers.
          > > In summary:
          > > - Using "autochdir" is better than nothing, with the complications
          > > described by Marco
          >
          > > - Using
          > > au VimEnter * if expand('%') != ""' | cd %:h | endif
          > > (note that the double quote must be doubled)
          >
          > actually, there were no double quotes in the original command, just
          > two strings with single quotes: '%' and the empty string.
          >
          > > solves the problem (don't ask me why). Thank you J�rgen.
          >
          > The VimEnter event is triggered after all configurations file have been
          > read and the first file, if any, has been loaded. If there is at least
          > one file the % register contains its name. %:h gives the path to this
          > file. %:p:h might actually be a better solution, because this takes care
          > of file names without a path. On non-Unix-system this is no problem
          > because :cd only prints the current directory if called without a path,
          > but on Unix-systems :cd without a path changes the current directory to
          > the user's home directory.
          >
          > > - I see no fatality why gvim is unable to have this behaviour by
          > > default. Launching Nedit or emacs in the same conditions results in
          > > the correct working directory. I will submit the problem to vim_dev
          > > (did you eNG1Ne ?)
          >
          > I like the current behaviour. It's easier to change to the directory of
          > a loaded file once you are inside Vim than to go back to the directory
          > Vim was started from once the current directory has changed. I have
          > defined two commands for this task:
          >
          >   :command -bar -nargs=0 CD    cd  %:p:h
          >   :command -bar -nargs=0 LCD   lcd %:p:h
          >
          > Also the current directory is much more predictable for this use-case
          >
          >   vim **/files-in-subdirectories.txt
          >
          > ;-) It's still the directory from where you have issued the command and
          > not some semi-random subdirectory.
          >
          > Regards,
          > J�rgen
          >
          > --
          > Sometimes I think the surest sign that intelligent life exists elsewhere
          > in the universe is that none of it has tried to contact us.     (Calvin)

          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php
        • Jürgen Krämer
          Hi, ... sorry, I seem to have misinterpreted you -- I thought you always wanted to set the working directory to the directory of the loaded file, even when
          Message 4 of 19 , Mar 1, 2011
          • 0 Attachment
            Hi,

            Jean Johner wrote:
            >
            > Thank you for your explanations.
            >
            > I can understand your preferences but consider the following facts:
            >
            > - launching gvim from the command line in a subdirectory always
            > results in the subdirectory to be the working directory.
            > I see no reason why double-clicking in Konqueror should behave
            > differently. In most situations, people launching gvim in a
            > subdirectory want to open other files in the same directory. By the
            > way, other people in this thread seem to suffer from the present
            > situation.

            sorry, I seem to have misinterpreted you -- I thought you always wanted
            to set the working directory to the directory of the loaded file, even
            when calling Vim from the command line.

            > - The behaviour would be consistent with the Windows behaviour
            >
            > - The behaviour would be compatible with the behaviour of almost all
            > other text editors in KDE (Emacs, XEmacs, Nedit, Kwrite, Kate). An
            > exception is Kedit which behaves like gvim.

            I don't have a Unix/Linux box here so I can't really say much. Can Vim
            determine whether it was started from Konqueror (or some other GUI file
            manager) or from the command line? In Windows it just *assumes* it was
            started from Explorer when it was passed a full path including a drive
            letter. What's the path to the file inside Vim, is it absolute or
            relative? You can check this with

            :echo expand('%')

            Does it change for files below the user's home directory and for files
            outside (like in /etc)? If it's always absolute you can simulate the
            Windows behaviour of Vim with the following modified version of the
            autocommand:

            au VimEnter * if strpart(expand('%'), 0, 1) == '/' | cd %:h | endif

            Regards,
            Jürgen

            --
            Sometimes I think the surest sign that intelligent life exists elsewhere
            in the universe is that none of it has tried to contact us. (Calvin)

            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php
          • Marco
            ... This line is still wrong. A single quote is missing. au VimEnter * if expand( % ) != | cd %:h | endif This line solves the problem for me. Thank you.
            Message 5 of 19 , Mar 2, 2011
            • 0 Attachment
              On 2011-03-01 Jürgen Krämer <jottkaerr@...> wrote:

              > > - Using
              > > au VimEnter * if expand('%') != ""' | cd %:h | endif
              > > (note that the double quote must be doubled)
              >
              > actually, there were no double quotes in the original command, just
              > two strings with single quotes: '%' and the empty string.

              This line is still wrong. A single quote is missing.

              au VimEnter * if expand('%') != '""' | cd %:h | endif

              This line solves the problem for me. Thank you. I would like to see this as
              default behaviour, too.


              Marco


              --
              You received this message from the "vim_use" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php
            • Marco
              ... I totally agree. ... ACK ... +1 Marco -- You received this message from the vim_use maillist. Do not top-post! Type your reply below the text you are
              Message 6 of 19 , Mar 2, 2011
              • 0 Attachment
                On 2011-03-01 Jean Johner <jean.johner@...> wrote:

                > Hi Jurgen,
                >
                > Thank you for your explanations.
                >
                > I can understand your preferences but consider the following facts:
                >
                > - launching gvim from the command line in a subdirectory always
                > results in the subdirectory to be the working directory.
                > I see no reason why double-clicking in Konqueror should behave
                > differently. In most situations, people launching gvim in a
                > subdirectory want to open other files in the same directory.

                I totally agree.

                > - The behaviour would be compatible with the behaviour of almost all
                > other text editors [...]

                ACK

                > Perhaps we could vote.

                +1


                Marco


                --
                You received this message from the "vim_use" maillist.
                Do not top-post! Type your reply below the text you are replying to.
                For more information, visit http://www.vim.org/maillist.php
              • Jürgen Krämer
                Hi, ... please read my mail again; the original line was correct regarding the single quotes. Your line checks if the current file is not one with a name
                Message 7 of 19 , Mar 2, 2011
                • 0 Attachment
                  Hi,

                  Marco schrieb:
                  > On 2011-03-01 Jürgen Krämer <jottkaerr@...> wrote:
                  >
                  >>> - Using
                  >>> au VimEnter * if expand('%') != ""' | cd %:h | endif
                  >>> (note that the double quote must be doubled)
                  >>
                  >> actually, there were no double quotes in the original command, just
                  >> two strings with single quotes: '%' and the empty string.
                  >
                  > This line is still wrong. A single quote is missing.
                  >
                  > au VimEnter * if expand('%') != '""' | cd %:h | endif
                  >
                  > This line solves the problem for me. Thank you. I would like to see this as
                  > default behaviour, too.

                  please read my mail again; the original line was correct regarding the
                  single quotes.

                  Your line checks if the current file is not one with a name consisting
                  of two double quotes. However unlikely such a name is, the test is wrong.
                  And it does not take care of the case where Vim is started without a
                  filename.

                  Regards,
                  Jürgen

                  --
                  Sometimes I think the surest sign that intelligent life exists elsewhere
                  in the universe is that none of it has tried to contact us. (Calvin)

                  --
                  You received this message from the "vim_use" maillist.
                  Do not top-post! Type your reply below the text you are replying to.
                  For more information, visit http://www.vim.org/maillist.php
                • Ben Schmidt
                  ... This is still not what was intended, though syntactically correct. There should not be any double quotes at all: au VimEnter * if expand( % ) != | cd
                  Message 8 of 19 , Mar 2, 2011
                  • 0 Attachment
                    > This line is still wrong. A single quote is missing.
                    >
                    > au VimEnter * if expand('%') != '""' | cd %:h | endif

                    This is still not what was intended, though syntactically correct. There
                    should not be any double quotes at all:

                    au VimEnter * if expand('%') != '' | cd %:h | endif

                    Ben.



                    --
                    You received this message from the "vim_use" maillist.
                    Do not top-post! Type your reply below the text you are replying to.
                    For more information, visit http://www.vim.org/maillist.php
                  • Marco
                    ... Sorry, you re right. My fault. Marco -- You received this message from the vim_use maillist. Do not top-post! Type your reply below the text you are
                    Message 9 of 19 , Mar 2, 2011
                    • 0 Attachment
                      On 2011-03-02 Jürgen Krämer <jottkaerr@...> wrote:

                      >
                      > Hi,
                      >
                      > Marco schrieb:
                      > > On 2011-03-01 Jürgen Krämer <jottkaerr@...> wrote:
                      > >
                      > >>> - Using
                      > >>> au VimEnter * if expand('%') != ""' | cd %:h | endif
                      > >>> (note that the double quote must be doubled)
                      > >>
                      > >> actually, there were no double quotes in the original command, just
                      > >> two strings with single quotes: '%' and the empty string.
                      > >
                      > > This line is still wrong. A single quote is missing.
                      > >
                      > > au VimEnter * if expand('%') != '""' | cd %:h | endif
                      > >
                      > > This line solves the problem for me. Thank you. I would like to see this
                      > > as default behaviour, too.
                      >
                      > please read my mail again; the original line was correct regarding the
                      > single quotes.
                      >
                      > Your line checks if the current file is not one with a name consisting
                      > of two double quotes. However unlikely such a name is, the test is wrong.
                      > And it does not take care of the case where Vim is started without a
                      > filename.

                      Sorry, you're right. My fault.


                      Marco


                      --
                      You received this message from the "vim_use" maillist.
                      Do not top-post! Type your reply below the text you are replying to.
                      For more information, visit http://www.vim.org/maillist.php
                    • Christian Brabandt
                      Hi Jean! ... The behaviour is already built into vim. But currently it only works when dragging a file onto gvim and holding down the shift key. I don t know,
                      Message 10 of 19 , Mar 2, 2011
                      • 0 Attachment
                        Hi Jean!

                        On Di, 01 Mär 2011, Jean Johner wrote:

                        > I can understand your preferences but consider the following facts:
                        >
                        > - launching gvim from the command line in a subdirectory always
                        > results in the subdirectory to be the working directory.
                        > I see no reason why double-clicking in Konqueror should behave
                        > differently. In most situations, people launching gvim in a
                        > subdirectory want to open other files in the same directory. By the
                        > way, other people in this thread seem to suffer from the present
                        > situation.
                        >
                        > - The behaviour would be consistent with the Windows behaviour
                        >
                        > - The behaviour would be compatible with the behaviour of almost all
                        > other text editors in KDE (Emacs, XEmacs, Nedit, Kwrite, Kate). An
                        > exception is Kedit which behaves like gvim.

                        The behaviour is already built into vim. But currently it only works
                        when dragging a file onto gvim and holding down the shift key. I don't
                        know, whether vim can distinguish between being called from a file
                        manager and from the commandline.

                        Hm, but wait, you should be able to script something? Using the solution
                        from Jürgen, that can be slightly improved to the following:

                        (untested)
                        au VimEnter * if empty($TERM) | sil cd %:h | endif

                        I think, it doesn't even matter, if the filename is given as relative or
                        absolute path, because even when given just the filename without a path
                        or no filename at all, ':h' will expand to '.' which is save in that
                        case. In any case, the check for an empty $TERM variable should prevent
                        changing the directory, when called from the command line.

                        regards,
                        Christian

                        --
                        You received this message from the "vim_use" maillist.
                        Do not top-post! Type your reply below the text you are replying to.
                        For more information, visit http://www.vim.org/maillist.php
                      • John Little
                        ... I ve got KDE 4, but in principle this stuff is controlled by desktop entry files (.desktop), which are used by gnome too so I don t think it s different
                        Message 11 of 19 , Mar 2, 2011
                        • 0 Attachment
                          On Feb 28, 9:34 am, Jean Johner <jean.joh...@...> wrote:
                          > Doing the same in KDE 3.5 Konqueror file manager opens file1.f but the
                          > working directory is always the user's home directory.

                          I've got KDE 4, but in principle this stuff is controlled by desktop
                          entry files (.desktop), which are used by gnome too so I don't think
                          it's different back in 3.5.

                          My KDE (in the absence of set autochdir and the like) starts gvim in
                          my $HOME/Documents; I suppose that's from system settings->about me-
                          >paths.

                          I suggest you check out

                          ~/.local/share/applications/gvim.desktop

                          If it's not there, copy it from /usr/share/applications. (I had to
                          customize mine because KDE didn't not honour my $PATH when running
                          gvim, and insisted on running /usr/bin/gvim when I wanted /usr/local/
                          bin/gvim.) Or, use the system settings to edit the gvim action, I
                          can't remember what it was like for KDE 3.5.

                          If there's a Path= entry, that will be used, but that's no good.

                          Changing the exec key to

                          f=%f;cd ${f%%/*};/usr/local/bin/gvim -f %F

                          worked, but looks dodgy in that it assumes bash is being used to run
                          it; the Desktop Entry specification (http://standards.freedesktop.org/
                          desktop-entry-spec/desktop-entry-spec-latest.html) doesn't say
                          anything about that. and I imagine that desktop environments are free
                          to exec the command directly.

                          Telling vim to do it seems to me to be supported; it's working for me
                          with:

                          /usr/local/bin/gvim -c "%%:h" -f %F

                          That sets the current directory to the path of the first file. The %
                          is doubled to escape it from the desktop mechanism.

                          NGN, you might like to try that too. I think the other editors (kate,
                          gedit, ...) are doing the equivalent of vim's set autochdir. IMO this
                          is not a vim issue, it's a desktop entry specification problem, in
                          that there's no obvious way to specify the path of the invoked file.

                          Regards, John

                          --
                          You received this message from the "vim_use" maillist.
                          Do not top-post! Type your reply below the text you are replying to.
                          For more information, visit http://www.vim.org/maillist.php
                        • Jean Johner
                          ... In KDE 3.5, to launch gvim by clicking a .f file and have the good working directory (without adding anything in the .vimrc), you must pull the Konqueror s
                          Message 12 of 19 , Mar 6, 2011
                          • 0 Attachment
                            John Little wrote:
                            >
                            > Telling vim to do it seems to me to be supported; it's working for me
                            > with:
                            >
                            > /usr/local/bin/gvim -c "%%:h" -f %F
                            >
                            > That sets the current directory to the path of the first file. The %
                            > is doubled to escape it from the desktop mechanism.

                            In KDE 3.5, to launch gvim by clicking a .f file and have the good
                            working directory (without adding anything in the .vimrc), you must
                            pull the Konqueror's "Settings" menu, choose "Configure Konqueror"
                            then "File Associations", choose x_fortran in "text", click gvim then
                            "edit" then "Application". In "Command", I typed
                            $HOME/bin/gvim -c "cd %:h" %f

                            (note that the % is not doubled, even %f is optional. Also note that
                            you have forgotten cd in your command).

                            Alternatively, you may edit the
                            $HOME/.kde/share/applnk/.hidden/gvim.desktop
                            file.

                            I keep thinking that this command should be done by default by gvim.

                            Thank you John,

                            Best regards,

                            Jean Johner

                            --
                            You received this message from the "vim_use" maillist.
                            Do not top-post! Type your reply below the text you are replying to.
                            For more information, visit http://www.vim.org/maillist.php
                          • John Little
                            ... Sometimes, one wants to edit a file somewhere else but remain where you are . IMO vim isn t about telling you how to work just because other editors do
                            Message 13 of 19 , Mar 6, 2011
                            • 0 Attachment
                              On Mar 7, 10:58 am, Jean Johner <jean.joh...@...> wrote:

                              > I keep thinking that this command should be done by default by gvim.

                              Sometimes, one wants to edit a file "somewhere else" but remain "where
                              you are". IMO vim isn't about telling you how to work just because
                              other editors do it that way.

                              Regards, John

                              --
                              You received this message from the "vim_use" maillist.
                              Do not top-post! Type your reply below the text you are replying to.
                              For more information, visit http://www.vim.org/maillist.php
                            • Jean Johner
                              ... OK. I appreciate your art of abbreviations, what is NGN for ? (in a previous mail). Regards, Jean -- You received this message from the vim_use maillist.
                              Message 14 of 19 , Mar 7, 2011
                              • 0 Attachment
                                On Mar 7, 1:30 am, John Little <john.b.lit...@...> wrote:
                                > On Mar 7, 10:58 am, Jean Johner <jean.joh...@...> wrote:
                                >
                                > > I keep thinking that this command should be done by default by gvim.
                                >
                                > Sometimes, one wants to edit a file "somewhere else" but remain "where
                                > you are".  IMO vim isn't about telling you how to work just because
                                > other editors do it that way.
                                >
                                > Regards, John

                                OK.

                                I appreciate your art of abbreviations, what is NGN for ? (in a
                                previous mail).

                                Regards, Jean

                                --
                                You received this message from the "vim_use" maillist.
                                Do not top-post! Type your reply below the text you are replying to.
                                For more information, visit http://www.vim.org/maillist.php
                              Your message has been successfully submitted and would be delivered to recipients shortly.