Vim Tips

Frequently Asked Questions

How do I change the file format of a file in Vim?

:set ff=dos<html>|</html>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 <F10> :Hexmode<CR>
inoremap <F10> <Esc>:Hexmode<CR>
vnoremap <F10> :<C-U>Hexmode<CR>

" 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 <CR> :noh<CR>
map <F12> :set number!<CR>
map <F11> :set wrap!<CR>
nnoremap <F10> :Hexmode<CR>
inoremap <F10> <Esc>:Hexmode<CR>
vnoremap <F10> :<C-U>Hexmode<CR>

" 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
 
reference/vim.txt · Last modified: 2011/09/30 15:04 by simon
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki