How to store the current buffer filename with cursor position to register using neovim Lua API?

Viewed 12

In vim I can use getcurpos() and expand('%:t'), but how does this work in lua? The solution should ideally only use the neovim api.

1 Answers

Without the neovim api:

function Fcolumn_noplenary()
  local fname = vim.fn.expand('%:t')
  local line_col_pair = vim.api.nvim_win_get_cursor(0) -- row is 1, column is 0 indexed
  local fnamecol = fname .. ':' .. tostring(line_col_pair[1]) .. ':' .. tostring(line_col_pair[2])
  vim.fn.setreg('+', fnamecol) -- register + has filename:row:column
end

And with plenary:

function Fcolumn_plenary()
  local Path = require "plenary.path"
  local path = Path.path
  local fileAbs = vim.api.nvim_buf_get_name(0)
  local p = Path:new fileAbs
  local fname = p.filename 
  local line_col_pair = vim.api.nvim_win_get_cursor(0) -- row is 1, column is 0 indexed
  local fnamecol = fname .. ':' .. tostring(line_col_pair[1]) .. ':' .. tostring(line_col_pair[2])
  vim.fn.setreg('+', fnamecol) -- register + has filename:row:column
end
Related