It may be located at:
/etc/vim/vimrc
Otherwise create it at:
~/.vimrc
[Linux] - 將vim打造成source insight
reset
export TERM=xterm
stty rows 40 cols 120
syntax enable
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
How to merge a swap (.swp) file to the original file?
vim .xxx.swp
# in the editor, :recover
rm .xxx.swp
:h <vim-command>
How to Turn On or Off Color Syntax Highlighting In vi or vim Editor
:syntax on
:syntax off
How To Show or Hide Line Numbers In vi / vim Text Editor
<:set number> or <:set nu>: show line numbers
<:set nonumber> or <:set nu!>: hide line numbers
How to find out which file is currently opened in vim?
<ctrl+g>: show opened' file's name and its total line number
<1> and then <ctrl+g>: additionally show full path
<2> and then <ctrl+g>: additionally show buffer number
<h>: move left
<j>: move down
<k>: move up
<l>: move right
<0>: go to head of line
<$>: go to end of line
<gg>: go to first line
<:$>: go to last line
<ctrl+f>: page down
<ctrl+d>: half page down
<ctrl+b>: page up
<ctrl+u>: half page up
<%>: move to matching braces
<f?>: find the character <?> and move the cursor onto it
<F?>: same as <f?>, but find backward
<t?>: same as <f?>, but stop at the character before <?>
<T?>: same as <t?>, but find backward
They can be preceded with a number <n>
, so the command will be repeated n times.
<;>: repeat the previous command
<,>: repeat the previous command in reverse direction
<:set ic> or <:set ignorecase>: case-insensitive mode search
<:set noic>: case-sensitive search
How to do case insensitive search in Vim
<:set ic> and then <:set smartcase>
If you search for something containing uppercase characters, it will do a case-sensitive search; if you search for something purely lowercase, it will do a case-insensitive search.
How to stop line breaking in vim Display long line as multiple lines:
<:set wrap>
Display long line as one line:
<:set nowrap>
Method 1:
/\<.*the-word-you-want-to-find.*\>
<n> to find next
<N> to find previous
Method 2:
move the cursor to the word you want to find
<SHIFT+*> to find the word the cursor at
<SHIFT+#> to find previous
<p>: paste after
<P>: paste before
Turning off auto indent when pasting text into vim
Before pasting:
:set paste
After pasting:
:set nopaste
<y> and then move left: copy the character the cursor move to
<y> and then move right: copy the character the cursor previously at
<x>: delete the current character under the cursor
<yaw>: copy the current word(not character) under the cursor
<daw>: delete the current word(not character) under the cursor
<caw>: delete the current word and put you into insert mode
<yy> or <Y>: copy a line
<dd>: cut a line
<:%y>: copy all the file
<:%d>: delete all the file
How to delete line(s) below current line in vim?
:+,$d
First install vim-gtk
:
sudo apt-get install vim-gtk
Make sure clipboard
and xterm_clipboard
are +
now.
vim --version | grep clipboard
And then:
"+y
<o>
<D>
<J>
<dh>: delete the previous character
<dl>: delete current character
<db>: delete all previous characters in this word
<dw>: delete all later characters in this word, also the following space
<cw>: change(i.e. delete and go to insert mode) all later characters in this word
<diw>: delete current word
<ciw>: change current word
<daw>: delete the current word(word here is the characters between these specific characters: .,-+()[]/\)
<daW>: delete the current token(token here is the characters between spaces, they may contain .,-+()[]/\)
<d0>: delete all previous characters
<d$>: delete from current character to the end of line
Delete All Characters After “.” In Each Line
:%norm f<?>lD #<?> is that specific character
or
:%s/\<?>.*/<?>/
Efficient way to delete line containing certain text in vim with prompt
:%s/.*<text>.*\n//g
How to automatically strip trailing spaces on save in Vi and Vim?
:%s/\s\+$//e
Find all 'foo's in the file and replace them with 'bar's.
:%s/foo/bar/g
Interactive search/replace regex in Vim?
To find and replace one by one with confirmation, use:
:%s/foo/bar/gc
And then use y
to confirm, n
to skip, Esc
to quit.
To escape / and \, add \ before them, they becomes: \/ and \\.
To replace trailing spaces:
:%s/\s\+$//c
<u> or <:undo>: undo
<Ctrl+R> or <:redo>: redo
<Shift+V> and then move up or down to select multiple lines
Method 1:
<:left 4> or <:le 4>: indent!!
<:right 4>: unindent!!
Method 2:
:set tabstop=4
:set shiftwidth=4
:set expandtab
and then >(to indent) or <(to unindent)
Method 3: to indent
:'<,'>s!^! !
To indent the whole file:
How do I fix the indentation of an entire file in Vi?
gg=G''
What's a quick way to comment/uncomment lines in Vim?
Ctrl + v to go into visual mode, select multiple lines, and then:
:'<,'>s/^/#/
Note that '<,'>
appears automatically after :
is entered.
VIM Disable Automatic Newline At End Of File
vim -b file.txt
:set noeol
:wq
or
vim file.txt
:set binary
:set noeol
:wq
To disable automatic newline forever, add the following into .vimrc
:
:set nofixendofline
<:q!>
<:wq>