简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM