user2879704
user2879704

Reputation:

Open file hook to move the cursor to the buffer/file end

How can i write a hook function that will place the cursor at the end of the file on opening a file.

In elisp, it looks approximately like this,

(add-hook 'open-buffer-hook
      (lambda () (end-of-buffer)))

In vim, I can open a file and press :$ to go to the end but i am keen on doing it via a hook.

Upvotes: 1

Views: 213

Answers (3)

romainl
romainl

Reputation: 196556

An autocommand seems too much to me.

In your shell,

$ vim file +$

opens file in Vim and jumps to the last line.

In Vim,

:edit +$ file

opens file and jumps to the last line.

This works with other related commands like :vsplit or :tabedit.

Upvotes: 1

Volker Siegel
Volker Siegel

Reputation: 3585

It you just want to open files from the command line, you can use

vim +':norm G$' file.txt

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270617

Using autocmd you may hook into BufReadPost and execute something like G$ in normal mode to advance to the last line and the last character.

autocmd BufReadPost * :normal G$

I used BufReadPost to cause this command to run after the file is fully read into the buffer. The * applies this rule to all buffer types, but you could limit it by FileType or by filename pattern as well. See :help autocmd for more details.

Upvotes: 1

Related Questions