VSCodeNeovim で選択行のパスと行番号コピーする
tamago324.icon Neovimだとやっぱりなんでもできるのがいいね!
開いているプロジェクトからの相対パスで src/features/chat/hooks/usePrefilledInput.tsx:L37-L39 みたいな感じでコピーできるようになった
ノーマルモードは、ファイルパス
ビジュアルモードは、ファイルパスと行番号
WSL でのコピーをできるようにした
code:vim
" ノーマルモード: 行はコピーしない
nnoremap <A-c> <Cmd>call <SID>CopyFilepathLine()<CR>
function! s:CopyFilepathLine()
let l:line = line('.')
let l:result = luaeval("require('vscode-copy-path').get_filepath()")
call setreg('+', l:result)
echo 'Copied: ' . l:result
endfunction
" ビジュアルモード: :<C-u> でコマンドラインに抜けてから実行
xnoremap <A-c> :<C-u>call <SID>CopyFilepathRange()<CR>
function! s:CopyFilepathRange()
let l:start = line("'<")
let l:end = line("'>")
let l:filepath = luaeval("require('vscode-copy-path').get_filepath()")
let l:result = l:start == l:end
\ ? l:filepath . ':L' . l:start
\ : l:filepath . ':L' . l:start . '-L' . l:end
call setreg('+', l:result)
echo 'Copied: ' . l:result
endfunction
lua << EOF
local M = {}
function M.get_filepath()
local vscode = require('vscode')
local fullpath = vscode.eval("return vscode.window.activeTextEditor?.document.uri.fsPath ?? ''")
local root = vscode.eval("return vscode.workspace.workspaceFolders?.0?.uri.fsPath ?? ''")
-- \ を / に統一
fullpath = fullpath:gsub('\\', '/')
root = root:gsub('\\', '/')
if fullpath == '' then
return vim.fn.expand('%:p')
end
if root and root ~= '' then
if root:sub(-1) ~= '/' then
root = root .. '/'
end
if fullpath:sub(1, #root) == root then
return fullpath:sub(#root + 1)
end
end
return fullpath
end
package.loaded'vscode-copy-path' = M
EOF
設定で、Alt+C をNeovim側に転送する設定も入れる必要がある
ショートカットに追加
code:json
{
"command": "vscode-neovim.send",
"key": "alt+c",
"when": "editorTextFocus && neovim.init && neovim.mode != insert",
"args": "<A-c>"
},
これで送信できるんだ~