Thursday, January 4, 2018

VIM - Tips

MAPPING

# mapping ESC key to F5
:imap <f5> <esc>

SEARCH

# search white space 15 or more
/\s\{15,}


# searches for trailing white space and deletes it everywhere in the buffer
:%s/\s\+$//e
:%s/\s\+$ or :%s/\s\+$//
The "e" flag tells ":substitute" that not finding a match is not an error.

# selected ares, search _eng, add # in the beginning of the line
:'<,'>g/\%V_eng/s/^/#/

# Visual Select
:'<,'>s/\%V1/0/g
selected area, replace 1 to 0

# replace selected lines
:'<,'>s/eqr/pattern":"fsfpb0ph","

:'<,'>s/\%V-/ /g
#replace "-" with " " in selected area

#replace space with new line
:%s/ /\r/g

# To change to the directory of the currently open file for all windows
:cd %:p:h

# To change to the directory only for the current window
:lcd %:p:h
     % gives the name of the current file,
     %:p gives its full path,
     %:p:h gives its directory (the "head" of the full path).

# replace " " with a new line
:%s/ /\r/g

# combine multiple lins to one line
%j

# Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.
:%s/foo/bar/g

# Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'.
:s/foo/bar/g

# search [00:49:33.151] with any number
/[..:..:..\....\]

zz - move current line to the middle of the screen
zt - move current line to the top of the screen
zb - move current line to the bottom of the screen
z<enter> - move current line to the bottom of the screen

# Show all tabs:
/\t

# Show spaces before a tab:
/ \+\ze\t

# Convert tabs to space
:retab

# Show trailing whitespace:
/\s\+$

# Show trailing whitespace only after some text (ignores blank lines):
/\S\zs\s\+$

# ignore case
:set ignorecase
:set ic

# consider case
se noic

# search a pattern and add a character at the beginning of the line
:%g/_eng/substitute/^/#/

Recording macro

Each register is identified by a letter a to z.

# To enter a macro, type:
q<letter><commands>q

# To execute the macro
 <number> times (once by default), type:

<number>@<letter>

# So, the complete process looks like:
qd  start recording to register d
... your complex series of commands
q   stop recording
@d  execute your macro
@@  execute your macro again
# end of recording macro #

# to join all lines in a file into a single line
# "ggVG" visually selects all lines, and "J" joins them.
ggVGJ

# Align Colmun
:%!column -t #Align Colmun

# search square bracket of "A" char
/\["A", *[0-9-e.]*\]                                                                                                                                          2230,461      44%

# search square bracket of "A" char and replace
:%s/\["A", *[0-9-e.]*\]/["A", "auto"]/

# align column for selected column
:'<,'>%!column -t

# select columbn and override it
???

# split window horizontal
:sp filename

# split window vertical
:vs filename

# grep file names and open it
gvim `grep tmLatchTrim patterns/* -l`

# Switching case of characters
Toggle case "HellO" to "hELLo" with g~ then a movement.
Uppercase "HellO" to "HELLO" with gU then a movement.
Lowercase "HellO" to "hello" with gu then a movement.

No comments:

Post a Comment