Search code examples
vimputtyswapfile

Why is vim not working properly after a file recover?


I went to open a log file in vim, but was warned that a swap file had been found. One of the warnings suggested that I might want to open the file using vim -r filename.log. I did that, but now when I 'vim' around in a file, I have to type "GG" (instead of "G") to get it to take me to the end, and "gg" (instead of "g") to get it to take me to the beginning. And when I use j and k to move up / down one line, it highlights part of the line (see blue and green cursors in screenshot below) whereas before there was no highlight. Any clues what could be going on?

enter image description here

Thanks!


Solution

  • I have to type "GG" (instead of "G") to get it to take me to the end,

    That's odd... Is it possible that the first G is being consumed by a pending keystroke? For example, if you type gG, that's an invalid command and gets ignored. So you'd need gGG to actually go to the end of the file, since the first G gets ignored... Is it possible that this is what's happening?

    Otherwise, is it possible that you have a mapping for G or something using G as a prefix? Does :map G show any mappings defined?

    and "gg" (instead of "g") to get it to take me to the beginning.

    This is normal. gg is the command in Vim (and in the original vi too) to go to the top of the file.

    Are you perhaps thinking of less which goes to the top with a single g?

    And when I use j and k to move up / down one line, it highlights part of the line (see blue and green cursors in screenshot below)

    This is the effect of the matchit plug-in, which is shipped with Vim (on recent versions of Vim, but I believe it's been shipped with Vim for quite some time.) It's enabled by default, loaded as part of startup.

    The matchit plug-in will highlight a matching paren, brace or bracket whenever you have the cursor on its pair. So since you have the cursor on a [, Vim will highlight the corresponding ].

    whereas before there was no highlight. Any clues what could be going on?

    You might not have noticed the effect of matchit even if it was enabled, since it only gets triggered when your cursor is on top of a bracket. Perhaps this log file is different in that all lines begin with a bracket and that's why it was so salient?

    Or you might have different versions or flavors of Vim in your machine and some of them will not load matchit (perhaps you have a "minimal" Vim that doesn't support plug-ins?) Perhaps you have commands vim, vi, view and some of them will load a different version of Vim that behaves differently. In most cases, you tend to use that other version, but this time since you got a message regarding recovering the file and using vim -r explicitly, you ended up using this particular version that does load it?

    You can use the :version command to check which version of Vim you're using and which features are available. (In particular, look for "minimal", or check whether the +eval is enabled, if it's listed as -eval it means support for external plug-in files will be for the most part disabled.)

    You can also check :scriptnames to see the list of script files loaded by Vim, you can check whether matchit.vim is included in that list.