Computer tools > Vim

Vim

Summary

Replacing regexp

:%s/original/replaced/g

Shortcuts to add to ~/.vimrc

(Requires run_file)
map <F2> :w<CR>:!cygstart "%"<CR> "save and lauch program. Change cygstart to xdg-open, gnome-open, etc. depending on Linux OS
map <F3> :w<CR>:!run_file "%"<CR> "runs current file. Requires run_file.

"Comment any selection (v) of html doc. with the relevant brackets: <!-- --> in the body, /* */ when being between the <style> tags, // when being between the <script> tags.
map <F8> v:s/^//g<CR>?<\(\\|\/\)script>\\|<\(\\|\/\)style>\\|<\(\\|\/\)html>\\|<\!DOCTYPE html><CR>yi<<C-O><C-O>:if "<C-r>0" == "script"<CR>:'<,'>s#\(.\+\)#//\1<CR>elseif "<C-r>0" == "style"<CR>:'<,'>s#\(.\+\)#/*\1*/<CR>else<CR>:'<,'>s/\(.\+\)/<!--\1--><CR>endif<CR>:nohls<CR><Esc>
map <C-F8> :s/<!--\(.*\)-->/\1/<CR>:nohls<CR> "remove <!-- --> comments, in a html doc

"Simpler versions
"map <F8> ?<\(\\|\/\)script>\\|<\(\\|\/\)style>\\|<\(\\|\/\)html>\\|<\!DOCTYPE html><CR>yi<<C-O><C-O>:if "<C-r>0" == "script"<CR>s#\(.\+\)#//\1<CR>elseif "<C-r>0" == "style"<CR>s#\(.\+\)#/*\1*/<CR>else<CR>s/\(.\+\)/<!--\1--><CR>endif<CR>:nohls<CR><Esc>
"map <F8> :s/\(..*$\)/<!--\1-->/<CR>:nohls<CR> "put <!-- --> comments, in a html doc

"To swap distant selections:
" 1) delete current selection (v) and create m mark (mm)
map <F9> dmm
" 2) swap new current selection (v) with last m mark (`m)
map <C-F9> P`mP