See http://scriptogr.am/jayrajput for this blog.
Showing posts with label VIM. Show all posts
Showing posts with label VIM. Show all posts
Wednesday, March 13, 2013
Saturday, February 09, 2013
Vim: Sorting section titles in a file
Here is vim trick to sort section titles in a file. Inspired from a vim book which I read.
Imagine a input file where the sections needs to be sorted. The content of the section needs to be kept intact.
Section Z
Item1
Item2
SectionEnd
Section A
Item1
Item2
SectionEnd
Section D
Item1
Item2
SectionEnd
Here is the VIM exscript to do the job:
" Mark lines between each Section/End block
:g/^Section/,/^End/-1s/$/@@/
" Now join the blocks into one line
:g/^Section/,/^End/j
" Sort each block
:%!sort
" Restore the joined lines to original blocks.
:%s/@@ /^M/g
Output file after sourcing the exscript:
Section A
Item1
Item2
End
Section D
Item1
Item2
End
Section Z
Item1
Item2
End
Imagine a input file where the sections needs to be sorted. The content of the section needs to be kept intact.
Section Z
Item1
Item2
SectionEnd
Section A
Item1
Item2
SectionEnd
Section D
Item1
Item2
SectionEnd
Here is the VIM exscript to do the job:
" Mark lines between each Section/End block
:g/^Section/,/^End/-1s/$/@@/
" Now join the blocks into one line
:g/^Section/,/^End/j
" Sort each block
:%!sort
" Restore the joined lines to original blocks.
:%s/@@ /^M/g
Output file after sourcing the exscript:
Section A
Item1
Item2
End
Section D
Item1
Item2
End
Section Z
Item1
Item2
End
Saturday, August 18, 2012
VIM: Cdargs
Cdargs is a curses based directory bookmarking tool. See http://www.skamphausen.de/cgi-bin/ska/CDargs/
This works nice on bash but I wanted something like that to work with VIM. Tried NERDTree bookmarking but still wanted something like curses. Press a key a list is displayed where you can move around. Something like screen window list shown on pressing C-A+"
I found it that it was way too easy to integrate cdargs with vim using a vim function. The function is self-explanatory. Read the see links and you will be able to decipher the function.
There is also VIM plugin which uses cdargs but it does not show the curses window. See: https://github.com/vim-scripts/cdargs
This works nice on bash but I wanted something like that to work with VIM. Tried NERDTree bookmarking but still wanted something like curses. Press a key a list is displayed where you can move around. Something like screen window list shown on pressing C-A+"
I found it that it was way too easy to integrate cdargs with vim using a vim function. The function is self-explanatory. Read the see links and you will be able to decipher the function.
" See http://vim.wikia.com/wiki/Write_your_own_Vim_function
function! Cd()
exe "!cdargs"
let mydir = system("cat /home/jay/.cdargsresult")
" See http://stackoverflow.com/questions/4596932/vim-cd-to-path-stored-in-variable
cd `=mydir`
endfunction
" Alias to my function so you do not need to type call everytime.
command Cd call Cd()
There is also VIM plugin which uses cdargs but it does not show the curses window. See: https://github.com/vim-scripts/cdargs
Tuesday, May 19, 2009
VIM : Comments lines with a pattern in a file
The lines in a file containing a specific pattern can be commented in VIM using the :g command.
:g command is used in following form
:g/[patern]/[cmd]
Example:
:g/JR/normal I //
explanation
Pattern : JR
Cmd : normal I //
"I" is used in VIM to start insert mode at the start of line and "//" is the comment character used in PHP.
Enjoy VIM. Check out the help :g if you have more questions.
:g command is used in following form
:g/[patern]
Example:
:g/JR/normal I //
explanation
Pattern : JR
Cmd : normal I //
"I" is used in VIM to start insert mode at the start of line and "//" is the comment character used in PHP.
Enjoy VIM. Check out the help :g if you have more questions.
Monday, March 30, 2009
Toggle Comment in VIM in one line using perl
There are lot of scripts in VIM which can toggle comments for you. But interestingly there is not a one liner which I can execute on systems where I do not have my vimrc file.
try this mapping
:map F2 V:!perl -nle 'if (/^\s*\/\//) { s/\s*\/\/\s*(.*)/$1/g; print } else {print "//".$_}'^Mj
You can map Alt+C or Ctrl+T something which is faster than F2
This mapping will comment and uncomment the C-style comment. I have tried this on Ubuntu and VIM 7.1 and works fine for me
Note: The map command may not work on copying as the VIM may not recognize the Ctrl - M character. You have to Ctrl- V and then Enter to make it work
try this mapping
:map
You can map Alt+C or Ctrl+T something which is faster than F2
This mapping will comment and uncomment the C-style comment. I have tried this on Ubuntu and VIM 7.1 and works fine for me
Note: The map command may not work on copying as the VIM may not recognize the Ctrl - M character. You have to Ctrl- V and then Enter to make it work
Tuesday, March 10, 2009
Formatting XML in VIM - xmllint --format -
XML text can be formatted in VIM by running the xmllint command over the selected text.
!xmllint --format -
Note the hyphen at the last.
Tried searching for sorter options for --format but did not found any.
!xmllint --format -
Note the hyphen at the last.
Tried searching for sorter options for --format but did not found any.
Friday, April 06, 2007
Tuesday, December 26, 2006
VIM: vimrc function to set file name
This function replaces the existing file name with the name of the file stored in register %.
The file should have the file name after "Module" word for this function to work properly.
function! ReadFileName()
let l:linenum = search("^// *[Mm]odule","w")
let l:filename = getreg("%")
let l:newline = '// Module : '.filename
sil exe ":call setline( linenum, newline)"
endfunction
The file should have the file name after "Module" word for this function to work properly.
function! ReadFileName()
let l:linenum = search("^// *[Mm]odule","w")
let l:filename = getreg("%")
let l:newline = '// Module : '.filename
sil exe ":call setline( linenum, newline)"
endfunction
VIM: Getting names of file in the current directory
File names in the current directory can be obtained by key strokes: [control -x] +[control-f]
Sunday, December 24, 2006
VIM: File register %
Vim stores the name of the file in the register %
We can get the name of the file by keystrokes+%
We can get the name of the file by keystrokes
Subscribe to:
Posts (Atom)