Fandom

Vim Tips Wiki

Show the length of the current word

1,624pages on
this wiki
Add New Page
Talk0 Share
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).

CommentsEdit

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.