If I for example create a mark inside the file ~/.vimrc with mV.

How do I convert the mark 'V to the path of the file the mark is in?

When I type :marks I can see a list of marks and the corresponding file paths.

Perhaps I can put this list into a string somehow in order to retrieve the file path, but how?

I want to make a function that jumps to the file of the mark at the position where my cursor was last and not at the mark position.


You probably want to look at ":help getmarklist()" which can give you a list of global marks or, if you prefer, local marks of a specified buffer.

However, this sounds like you just want to restore the cursor when you enter a buffer. Have you looked at ":help restore-cursor"? It has this autocmd as an example:

autocmd BufReadPost *
  \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit'
  \ |   exe "normal! g`\""
  \ | endif

This just causes your cursor to jump to the last known position within a file whenever you open it for the first time in a Vim session.


Found a way to store the :marks output into a string without using the getmarklist() function

fun! Marklist() abort
  redir => str
  :silent marks
  redir END
  " do something with str here ...

But in the end i used a different way to achieve my goal:

let fileMarkKeys = [ "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z" ]
let fileMarkKeysLen = len(fileMarkKeys)
let i = 0 
while i < fileMarkKeysLen
    exe "nnoremap <leader>'" . fileMarkKeys[i] . " '" . fileMarkKeys[i] . "g`\""
    let i += 1

I put the above code into my .vimrc
Now when i use a capital mark like <leader>'V it will jump to my vimrc file in the buffer and automatically go the latest cursor position. (and not to the mark position)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.