Search code examples
csvvimvi

gg in sourced file goes to line 2 in vim


I'm experimenting with running a sequence of commands on a csv file; doing some replacements, changing some stuff. I open the file in vim and use so! filewithcommands. But gg will never go to the top of the file, always to the second position.

contents of samplefile.csv:

sometext;morechars
nextline;withsomemoredata
areyou;gettingthepoint

contents of filewithcommands:

gg

When I open vim --clean and run :so! filewithcommands the cursor will jump to the second line for some reason. I've tried :0 but the same thing happens. Running Ubuntu with Vim v 9.0.2121.


Solution

  • The commands in your filewithcommands are supposed to be Ex commands so gg, a normal mode command, has nothing to do there. Actually, that is what Vim is trying to tell you with:

    Error detected while processing /path/to/filewithcommands:
    line    1:
    E492: Not an editor command: gg
    

    More information is provided by :help 492:

      Not an editor command ~
    
    You tried to execute a command that is neither an Ex command nor
    a user-defined command.
    

    Error messages are a good thing and skipping them is never a good idea. Case in point: gg is wrong and won't do what you expect no matter what. Adding a ! to skip the error message won't make gg magically do what it can't do. All it will do is deprive you of an opportunity to understand the problem and fix it.

    The Ex command for moving the cursor to the top of the buffer is:

    :1
    

    so that is what you should put in your filewithcommands:

    1
    

    (the : is not necessary in that context)

    ex

    Alternatively, you can use :help :normal to execute normal mode commands in an Ex context. Using the following in your filewithcommands will have the same effect as 1:

    normal! gg