satyavvd

Tuesday, April 18, 2017

Vim: Apply some operation with external command on each line



Say I have below list of numbers those are some file size in KBs and I want to
convert them to GB:
801466311
694975337
116332661
636558230
1776193937
2576588352
202358013
2041308660
1966184154
9249101106
7014864623
14710830460
2576588352

these numbers are in KBs say I want them in GBs
visually select the lines by pressing Shift+V

g/^/exe ".!sed -e 's/.*/&\\/(1024*1024)/' | bc -l"

Sunday, March 4, 2012

search n replace with multiple files in Vim

Suppose you have 100+ perl or C files and you want to replace 'xxx' with 'yyy' in all those files :
=================================
$vim *.pl
:bufdo! s/xxx/yyy/g
:wall
=================================


:wall -> save all.

same thing you can do with files opened in vim as in different windows.
:windo! s/xxx/yyy/g

other ways :
sed -i.bak -e 's/xxx/yyy/' *.pl ( with backup )
sed -i -e 's/xxx/yyy/' *.pl ( with no backup )
perl -p -i -e 's/xxx/yyy/g' *.pl ( careful no back up !! ).

Happy Vimming !!
@Satya

Sunday, February 26, 2012

Vim alphabet soup



aappend mode
Aappend to end of line
btraverse word wise back
Btraverse word wise back
cchange followed by motion command
cw - change a word
3cw - change 3 words right
c0 - that is c zero, change from current pos to begining of line
c$ - change from current pos to till end of line
ct - change till followed by char or pattern i.e motion forward dir.
cf - change till including followed forward dir.
cT - change till followed by char or pattern i.e motion backward dir.
cF - change till including followed backward dir.
cgg - change from cur.pos. to beg. of file
cG - change from cur.pos. to end of file
ciw - change a word, when you are in middle of word
caw - change a word including space after word
ci - could be quotes brackets or braces
CChange till end of line ( = c$ )
ddelete can be preceded count and followed by motion
dw - delete a word
3dw - delete 3 words right
dd - delete entire line, can precede count, use p to paste same line
d0 - that is d zero, delete from current pos to begining of line
d$ - delete from current pos to till end of line
dt - delete till followed by char or pattern i.e motion forward dir.
df - delete till including followed forward dir.
dT - delete till followed by char or pattern i.e motion backward dir.
dF - delete till including followed backward dir.
dgg - delete from cur.pos. to beg. of file
dG - delete from cur.pos. to end of file
diw - delete a word, when you are in middle of word
daw - delete a word including space after word
dwwhp - swap words
ddp - swap lines
DDelete till end of line ( = d$ )
emove to word wise, places cursor at end of each word
Emove to word wise, places cursor at end of each WORD
ff is a motion char it should followed by a char
To [count]'th occurrence of {char} to the right. The cursor is placed on {char}
FF is a motion char it should followed by a char
To [count]'th occurrence of {char} to the left. The cursor is placed on {char}
gg is a motion command it should be followed by some chars
g~w - change case of a word
10gg - to goto 10th line of file
gg - goto begining of file
gi - goto last insert position
gf - goto file on cursor pos.
gd - goto definition of cursor pos
Ggoto end of file
hmove left one char <-
Htop of screen
iinsert mode
IInsert mode , insert always starts at beginning of line
jdown one line
JJoin line
kup one line
Kdocumentation for word under cursor
lright one char ->
Lbottom of screen
mset mark followed by register char ( a-z A-Z 0-9)
MMiddle of screen
nnext find
NPrev find
oinsert blank next to current line
Oinserts blank above current line
ppaste
Ppaste at cursor pos.
rreplace one char
10r. - replaces 10 dots
RReplace mode
ssubstitute single char
Ssubstitue entire line
tTill before [count]'th occurrence of {char} to the right. The cursor is placed on the character left of{char
; is used to find next, comma is used for finding prev occurrence of {char}
TTill before [count]'th occurrence of {char} to the left. The cursor is placed on the character right of{char
; is used to find next, comma is used for finding prev occurrence of {char}
uundo
UUndo all changes on current line
vlinewise visual mode but from current pos
Vlinewise visual mode
wword wise , moves to beginning of word right
Wword wise , moves to beginning of word left
xdelete a char at current cur.pos
xp - swap next two characters around
Xdelete previous char at current cur.pos
yyank or copy
zz with motion is used for folds
~swap case
0zero, begin of line
^begin of line
$end of line
%to match corresponding brackets,braces or curlies
match brackets
matchit.vim : % now matches tags <tr><td><script> etc
#find and highlight all occurrences of current word backward
*find and highlight all occurrences of current word forward
-move prev. line
+move next line
.to repeat last insert
=(re)indent the text on the current line or on the area selected (SUPER)
>>shift line right, can preced count
>i} - shifts right whole content within curlies
>a} - shifts right whole content including curlies
<<shift line left
/forward search pattern
?backward search pattern

guulowercase line
gUUuppercase line
~invert case (upper->lower; lower->upper) of current character
gfopen file name under cursor (SUPER)
gadisplay hex, ascii value of character under cursor
g8display hex value of utf-8 character under cursor
ggg?Grot13 whole file
xpswap next two characters around
dwwhpswap words
ddpswap lines
CTRL-A,CTRL-Xincrement, decrement next number on same line as the cursor
CTRL-R=5*5insert 25 into text
=(re)indent the text on the current line or on the area selected (SUPER)
=%(re)indent the current braces { ... }
G=ggauto (re)indent entire document
!!filter through shell command
5!! - filter 5 lines though shell command equal to( :.,+4! )
CTRL-e, CTRL -y- in insert mode holding these key combinamtions copy contents from below, above lines.
*g* g# : find word under cursor (forwards/backwards)
CTRL -N: word completion in insert mode
CTRL -X CTRL -LLine complete SUPER USEFUL
/ CTRL -R CTRL -WPull onto search/command line
q:to traverse ex mode history

Wednesday, March 23, 2011

Extend your bash completion !!

I have downloaded zip file, I wanna extract it on command line.

$ unzip [tab][tab] -> if it displays only zip files instead of all files is that won't be good ?

Yes !!

here you go :

put below line(s) in your .bashrc file or in .bash_profile you are done with it !!
complete -A file -X '!*.zip' unzip

for other completions :

complete -A file -X '!*.php' php
complete -A file -X '!*.pl' perl
complete -A file -X '!*.c' gcc
complete -A file -X '!*.sh' bash
complete -A file -X '!*.gz' gunzip
complete -A file -X '!*.tgz' gunzip
complete -A file -X '!*.Z' gunzip
complete -A file -X '!*.gz' tar
complete -A file -X '!*.tgz' tar
complete -A file -X '!*.Z' tar
complete -A file -X '!*.awk' awk
complete -A file -X '!*.sed' sed
complete -A file -X '!*.zip' unzip

now you can get completion for php,perl or create for any of your favorite language.

Thanks,
Satya

Thursday, March 3, 2011

Persistent undo with Vim 7.3

Persistent Undo -> You can do undo after save and quit, reopen later on a file.

Create a directory 'undodir' under your Vim's runtime path i.e ~/.vim
$ mkdir ~/.vim/undodir

Put below settings in your .vimrc file :
"================================
au BufWritePre /tmp/* setlocal noundofile
set undodir=~/.vim/undodir
set undofile
set undolevels=1000 "maximum number of changes that can be undone
set undoreload=10000 "maximum number lines to save for undo on a buffer reload
"================================

Save .vimrc and quit.

Now open some text file do some changes in it, save and exit. Re-open the same file and press 'u' will undo your previous changes.

Happy Vimming !!

Thanks,
Satya

Monday, February 7, 2011

Perl Omni Completion with VIM ( aka intelli sense)


Download omniperl Vim pluginn :
http://www.vim.org/scripts/script.php?script_id=1924

goto your Vim's runtime directory :
Ex: ~/.vim

extract omniperl.zip there then open omniperl.vba with vim :
$cd ~/.vim
$unzip omniperl.zip
$vim omniperl.vba

they type :so %
(so that it will extracts required files from vba file
*Note: if you got any errors try to install vimball plug in before doing above step
http://www.vim.org/scripts/script.php?script_id=1502
follow instructions in 'install details section
)

open your .vimrc file and put below lines :
"===================================
filetype plugin on
autocmd FileType pl set omnifunc=perlcomplete#OmniPerl_Complete
function! SmartComplete ()
" Remember where we parked...
let cursorpos = getpos('.')
let cursorcol = cursorpos[2]
let curr_line = getline('.')

" Special subpattern to match only at cursor position...
let curr_pos_pat = '\%' . cursorcol . 'c'

" Tab as usual at the left margin...
if curr_line =~ '^\s*' . curr_pos_pat
return "\<TAB>"
endif

" If no contextual match and after an identifier, do keyword completion...
if curr_line =~ '\k' . curr_pos_pat
return "\<C-N>"

" Otherwise, just be a ...
endif
if curr_line =~ '\(\->\|::\)' . curr_pos_pat
return "\<C-X>\<C-O>"
else
return "\<TAB>"
endif
endfunction

" Remap for smart completion on various characters...
inoremap =SmartComplete()
"===================================





you are done with it !!

now you can test it , open a new perl file :

$vim test.pl

now type below :

use DBI;
DBI->

pressing tab after typing DBI-> will popup methods of that module.

Damn good isn't it???

Happy Vimming !!


Saturday, January 29, 2011

Generate CSV list in Vim

Put below mappings in your .vimrc file :

nnoremap \, :%s/$/,/:%g/^/-j
vnoremap \, y:$opmz:'z,$s/$/,/:'z,$g/^/-j


Example :

for example if you have numbers as :
1
2
3
4
5
6
7
8
9
10

inorder to make csv list out of that, after putting those mappings in .vimrc file just press \,
in command mode will generate :
1 2, 3, 4, 5, 6, 7, 8, 9, 10,

you can select particular lines or block using visual mode and can generate csv only for that values also.

Happy Vimming !!

Regards,
Satya

Followers