如何转到文件的第 n 个字符,而不是字节?

dot*_*hen 8 vim utf-8

vim一个可以得到用下面的命令文件的第5个字节:

:goto 5
Run Code Online (Sandbox Code Playgroud)

但是,在 UTF-8 文本中,这可能是文件中的第 5 个、第 4 个甚至第 2 个字符。如何转到文件的第 5 个字符,而不是字节?

Ing*_*kat 3

您可以从缓冲区的开头开始,并使用它search()来匹配 N 个字符。唯一的陷阱是也要考虑换行符。这是gco执行此操作的自定义映射:

function! s:GoToCharacter( count )
    let l:save_view = winsaveview()
    " We need to include the newline position in the searches, too. The
    " newline is a character, too, and should be counted.
    let l:save_virtualedit = &virtualedit
    try
        let [l:fixPointMotion, l:searchExpr, l:searchFlags] = ['gg0', '\%#\_.\{' . (a:count + 1) . '}', 'ceW']
        silent! execute 'normal!' l:fixPointMotion

        if search(l:searchExpr, l:searchFlags) == 0
            " We couldn't reach the final destination.
            execute "normal! \<C-\>\<C-n>\<Esc>" | " Beep.
            call winrestview(l:save_view)
            return 0
        else
            return 1
        endif
    finally
        let &virtualedit = l:save_virtualedit
    endtry
endfunction
" We start at the beginning, on character number 1.
nnoremap <silent> gco :<C-u>if ! <SID>GoToCharacter(v:count1 - 1)<Bar>echoerr 'No such position'<Bar>endif<Bar><CR>
Run Code Online (Sandbox Code Playgroud)

'fileformat'请注意,对于已设置为 的缓冲区中的 CR-LF 组合,这仅计算一个字符dos