Vim Tips Wiki
Register
Advertisement
Tip 258 Printable Monobook Previous Next

created 2002 · complexity intermediate · author colin dearing · version 6.0


Ever wondered how long the current word is? This can be quite useful when editing data files. Add the following to your vimrc:

nnoremap <C-_> :echo 'word' expand('<cword>') 'has length' strlen(expand('<cword>'))<CR>

Then press Ctrl-_ to display the word under the cursor, and its length. The length is the number of bytes in the word, which is not the same as the number of characters if a multi-byte encoding is used. To get the number of characters, use:

nnoremap <C-_> :echo 'word' expand('<cword>') 'has length' strlen(substitute(expand('<cword>'), '.', 'x', 'g'))<CR>

For strings that aren't words, use the following to show the number of bytes in visually selected text when Ctrl-_ is pressed:

vnoremap <C-_> "-y:echo 'text' @- 'has length' strlen(@-)<CR>

It would be better to use :set showcmd to display the length (in bytes and characters) of any visually selected text. Then type viw to select the current word and show its length (v starts a character-wise visual selection and iw selects the inner word).

Comments[]

Advertisement