Wikia

Vim Tips Wiki

Changes: Show the length of the current word

Edit

Back to page

(Change <tt> to <code>, perhaps also minor tweak.)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 26: Line 26:
 
</pre>
 
</pre>
   
It would be better to use {{tt|:set showcmd}} to display the length (in bytes and characters) of any visually selected text. Then type <code>viw</code> to select the current word and show its length (<code>v</code> starts a character-wise visual selection and <code>iw</code> selects the inner word).
+
It would be better to use <code>:set showcmd</code> to display the length (in bytes and characters) of any visually selected text. Then type <code>viw</code> to select the current word and show its length (<code>v</code> starts a character-wise visual selection and <code>iw</code> selects the inner word).
   
 
==Comments==
 
==Comments==

Latest revision as of 12:30, July 15, 2012

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

Around Wikia's network

Random Wiki