Reputation: 459
In Vim there is a command ``.` to return exactly to where last edited text.
But my question is: How to make it automatic? What I mean is, every time I exit and reopen the same file again, it brings me to the point where I left.
I saw my friend's Vim has that behavior but he doesn't know how to do it.
Upvotes: 15
Views: 2805
Reputation: 11800
Put this lines on your ~/.vimrc file
" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal g`\"" |
\ endif
This only will help you next time you open vim to restore your cursor position.
Another idea is placing this on your ~/.bashrc
lvim='vim -c "normal '\''0"'
It will allow you to open the last edited file.
alias lvim="vim -c':e#<1'"
The above alias will open vim in command mode -c
to edit an alternative file #
in this case the first one <1
which is the last one.
For neovim I have this:
In your init.lua
require('autocmds')
In your lua directory create a file called autocmds.lua
:
local augroups = {}
augroups.restore_position {
restore_cursor_position = {
event = "BufRead",
pattern = "*",
command = [[call setpos(".", getpos("'\""))]],
},
}
for group, commands in pairs(augroups) do
local augroup = vim.api.nvim_create_augroup("AU_"..group, {clear = true})
for _, opts in pairs(commands) do
local event = opts.event
opts.event = nil
opts.group = augroup
vim.api.nvim_create_autocmd(event, opts)
end
end
Upvotes: 2
Reputation: 161674
I use these commands a lot:
CTRL-O Go to [count] Older cursor position in jump list (not a motion command).
CTRL-I Go to [count] newer cursor position in jump list (not a motion command).
ma Set mark a
at cursor position (does not move
the cursor, this is not a motion command).
'a Jump to the mark a
in the current buffer.
gi Insert text in the same position as where Insert mode was stopped last time in the current buffer.
Upvotes: 11