====== Vim Tips ====== ===== Frequently Asked Questions ===== ** How do I change the file format of a file in Vim? ** :set ff=dos|unix ** How do I put Vim in HEX mode ** :%!xxd :%!xxd -r ** How to quickly switch to/from HEX editing mode? ** Add the following to your .vimrc, and map to the key of your choice to allow toggeling: nnoremap :Hexmode inoremap :Hexmode vnoremap :Hexmode " ex command for toggling hex mode - define mapping if desired command -bar Hexmode call ToggleHex() " helper function to toggle hex mode function ToggleHex() " hex mode should be considered a read-only operation " save values for modified and read-only for restoration later, " and clear the read-only flag for now let l:modified=&mod let l:oldreadonly=&readonly let &readonly=0 let l:oldmodifiable=&modifiable let &modifiable=1 if !exists("b:editHex") || !b:editHex " save old options let b:oldft=&ft let b:oldbin=&bin " set new options setlocal binary " make sure it overrides any textwidth, etc. let &ft="xxd" " set status let b:editHex=1 " switch to hex editor %!xxd else " restore old options let &ft=b:oldft if !b:oldbin setlocal nobinary endif " set status let b:editHex=0 " return to normal editing %!xxd -r endif " restore values for modified and read only state let &mod=l:modified let &readonly=l:oldreadonly let &modifiable=l:oldmodifiable endfunction ** Useful settings when editing readonly large files in Vim? ** set undolevels=-1 set nomodifiable set readonly set noswapfile set bufhidden=unload set buftype=nowrite ** How do I remove all duplicated lines from a text file in VIM? ** :g/\(^.*$\)\_.*\1/d or 1G !Gsort 1G !Guniq ** How do I set up auto indenting space-filled tabs in VIM? ** Add the following to your configuration: set tabstop=4 set shiftwidth=4 set expandtab set smartindent ===== Sample .vimrc ===== set nobackup set tabstop=3 set smartindent set autoindent colorscheme desert set cindent set expandtab set shiftwidth=3 set incsearch highlight String ctermfg=darkcyan highlight Comment cterm=bold ctermfg=magenta highlight Constant ctermfg=3 highlight Search ctermfg=white ctermbg=blue highlight SpecialKey ctermfg=1 nnoremap :noh map :set number! map :set wrap! nnoremap :Hexmode inoremap :Hexmode vnoremap :Hexmode " ex command for toggling hex mode - define mapping if desired command -bar Hexmode call ToggleHex() " helper function to toggle hex mode function ToggleHex() " hex mode should be considered a read-only operation " save values for modified and read-only for restoration later, " and clear the read-only flag for now let l:modified=&mod let l:oldreadonly=&readonly let &readonly=0 let l:oldmodifiable=&modifiable let &modifiable=1 if !exists("b:editHex") || !b:editHex " save old options let b:oldft=&ft let b:oldbin=&bin " set new options setlocal binary " make sure it overrides any textwidth, etc. let &ft="xxd" " set status let b:editHex=1 " switch to hex editor %!xxd else " restore old options let &ft=b:oldft if !b:oldbin setlocal nobinary endif " set status let b:editHex=0 " return to normal editing %!xxd -r endif " restore values for modified and read only state let &mod=l:modified let &readonly=l:oldreadonly let &modifiable=l:oldmodifiable endfunction