VimTrick: Non-printable Characters
Easily access non-printable and other characters in Vim with CTRL-v.
At times we might want to use unprintable or other in characters not accessible on our keyboards. For example, if you’d like a search and replace expression to use a carriage return. To enter characters like this in Vim, type CTRL-v
while in insert-mode. Then enter the decimal code. You can also use CTRL-m
for newlines.
While in insert mode press CTRL-v
and then CTRL-m
to insert a newline. Or try CTRL-V
and then 162
to get ¢
. The screencast below captures the following substitution to drop each item onto an individual line:
%s/, /,^M /g
Note that the ^M
is a single character inserted by using CTRL-v
followed by CTRL-m
.
Other characters can be inserted using their encoding number, CTRL-V 13
in the case of carriage return or CTRL-v 9
can be used to insert a horizontal tab. Here are some other characters you can insert, by typing their number after you’ve pressed CTRL-v
in insert mode:
¢ 162
£ 163
© 169
® 174
Here’s a handy list for your reference. Just remember to go into Insert mode, then type CTRL-v
, then type the decimal number.
Was this useful? Help us improve!
With your feedback, we can improve VimTricks. Click a link to vote:
Great tip!
I have a function called "Preserve" wich I can use to perform some tasks and keeping the cursor position:
<pre>
" preserve function
if !exists('*Preserve')
function! Preserve(command)
try
let l:win_view = winsaveview()
"silent! keepjumps keeppatterns execute a:command
silent! execute 'keeppatterns keepjumps ' . a:command
finally
call winrestview(l:win_view)
endtry
endfunction
endif
</pre>
So to remove the DOS end of line i use another function called Dos2Unix
<pre>
" dos2unix ^M
if !exists('*Dos2unixFunction')
fun! Dos2unixFunction() abort
"call Preserve('%s/ $//ge')
call Preserve(":%s/\x0D$//e")
set ff=unix
set bomb
set encoding=utf-8
set fileencoding=utf-8
endfun
endif
com! Dos2Unix :call Dos2unixFunction()
</pre>
And the "Preserve function" could be used to other stuff like reindenting the whole file:
<pre>
command! -nargs=0 Reindent :call Preserve('exec "normal! gg=G"')
</pre>
Great info! I've been looking for something like this for a while.