Replace
:%s/https\?.*/[&](&)/g
%
– set the range to the entire files
– substitution/https\?.*/
– regex to matchhttp
orhttps
and anything else after it[&](&)
– The&
is the magic here and inserts the matched text. In this case, the URL. The rest of the characters are interpreted literally, giving us the linked URL in the resulting markdown./g
changes all the matches on a line- use any regex delimiter in your pattern substitution. No need to use
/
at all, try#
instead::s#/usr/local/bin#/usr/sbin#g
to avoid escaping slashes. - Limit a search and replace operation between lines matching 2 regex patterns using
/pattern1/,/pattern2/s/search/replace/
Search and replace | Vim Tips Wiki | Fandom
Repeat the last substitution
Developing efficient workflows in Vim is all about repetition, first and foremost by using .
to repeat the last command. But Vim can also repeat your last substitution. Here’s a few options with subtle differences:
:&
– Repeats last substitution but resets the flags. Also works with just :s.:&&
– Repeat last substitution with the same flags.:%&
– Repeat last substitution on entire file, reset flags.:%&&
– Repeat last substitution on entire file, keep flags.
:%s/something/newthing/g
:%&g
Search
- Append
/e
to the end of a search to place the cursor at the end of the next match. I.e/patten/e
- Stay in search mode
/pattern<C-g>
or/pattern<C-t>
- Append
\C
to the end of a search to enable a case-sensitive search. Examples:/copyright
- case insensitive/Copyright
- case sensitive/copyright\C
- case sensitive/Copyright\c
- case insensitive
Power of g
The global command :g
is very useful - multiple repeats
:[range]g[lobal]/{pattern}/[cmd]
For example:
:g/pattern/d
– Remove lines matching pattern:3,4g/pattern/d
– Remove lines matching pattern between 3 and 4 line:g/pattern/y A
- Yank all lines matching ‘pattern’ into the register.:g!/pattern/d
– Remove lines that do NOT match the pattern:v/pattern/d
– Also removes lines that do not match the pattern:cdo g/function/norm! ciw func<cr>
gQ
g0
g$
g <ctrl-g>
g <ctrl-a>
g#
g&
g-
g+
g??
gI
gU
gt
gT
g_
gf
gd
gg
Copying and pasting lines
The slow way is to navigate to the line I want, yank it, go back and paste it.
The most efficient way I can think to do that is to jump by searching with
/
and pressing<CR>
. Then yank the line withyy
. Then use the jump list,<ctrl-o>
, to bounce back. And pressp
to paste the line below orP
to paste the line above.Use the ex
:yank
command:<line number>yank
– copies the line number specified to your default register.Use the ex
:copy
command:<line number>copy.
– copies the line number specified and pastes it to the line belowThe ex
:copy
command has a short versiont
:281t.
– Copy line 281 and paste it below the current line:-10t.
– Copy the line 10 lines above the current line and paste it below the current line:+8t.
– Copy the line 8 lines after the current line and paste it below:10,20t.
– Copy lines 10 to 20 and paste them below:t20
– Copy the current line and paste it below line 20
Pasting into Vim from @StackOverflow? Avoid indent fail by using set
:paste
or use the system clipboard with"*p
To select a register from which to paste, one can use
"{register}p
to paste from the register{register}
. See pasting registers.
The shortcut keyboards in insert mode
<c-w>
- delete a word<c-x><C-f>
- autocomplete filenames in vim.<c-n>
- 自动提示<c-p>
- 自动提示<c-r>=
- From insert mode, enters Vim’s expression register<c-f>
- switch from Command-Line mode to the command-line window. Or During the/
portion of a search, open a search history window.<c-a>
or<C-x>
- increment or decrement hex, binary, and octal numbers in normal mode.<c-k>
+ 2 letters - add special characters in insert mode. see mode:help dig
. Examples:<c-k>oo
• bullet<c-k>Db
◆ diamond bullet<c-k>Pd
£ pound<c-k>Eu
€ euro<c-k>-N
– en dash<c-k>-M
— em
Quickfix
what a quick fix list is effectively what a quick fix list is a series of entries in which point to a specific file and location.
A lot of times they’re associated with errors links or search results.
- quickfix
:cw
错误信息分屏显示:cp
跳到上一个错误:cl
列出所有错误:cc
显示错误详细信息:cdo
- location-list
:ldo
DOS 与 Linux 的换行符
字符转换命令
dos2unix [-kn] file [newfile]
unix2dos [-kn] file [newfile]
The undo tree usr_32.txt
- If you make changes, undo, then make a different change, then undo, then make a different change, you create undo branches.
- To view the change tree, run
:undolist
, to navigate the undo branches, useg-
,g+
- Go back to an earlier text state with the
:earlier
command. - Go to newer text state with the
:later
command. This command accepts the following time units:s
(seconds),h
(hours),d
(days), andf
(number of saves).
Change the case of characters with ~
, u
and U
.
gUw
- Uppercase to end of wordgUiw
- Uppercase entire wordguap
- Lowercase paragraph
Sort lines in Vim:
sort-motion plugin - The primary interface to this plugin is via the gs
mapping, for sorting based on a text object or motion.
Examples:
gs2j
=> Sort down two lines (current + 2 below)gsip
=> Sort the current paragraphgsii
=> Sort the current indentation level (requires text-obj-indent plugin)gsi(
=> Sort within parenthesis.(b, c, a)
would become(a, b, c)
Vim has some built in options
:sort
- sort all lines:sort!
- sort in reverse:sort u
- remove dupes and sort:sort i
- ignore case:sort n
- sort numerically
There are ways to sort elements of a single line in vanilla vim as well, as detailed in this StackExchange response, but they will involve some regex.
jump between changes
:changes
- Show list of changesg;
- Jump to previousg,
- Jump to next
:norm
The command allows you to execute normal mode operations from the command line. By combining with %
, we can run a sequence of operations on an entire file.
:%norm
- Run a normal mode command on the entire file.ysiw
- surround plugin:%norm ysiw"A: ""
` character
`[
- Navigate to the beginning of your most recently yanked or changed text`]
- Navigate to the end of your most recently yanked or changed text
Ex Command-line
:set noignorecase
- make searches case sensitive (the default):set hlsearch
- highlight the remaining matches with the search highlight group.:set splitright
- open splits in a right direction.:set splitbelow
- open splits in a below direction.:set laststatus=3
- show only one activeted single status bar:edit!
- revert (go back or return to) all changes to the current buffer.:1,5j
- Use the ex commandj
to join some lines on a range. Use visual selection, and J to join or 'gJ' to join without spacing- Use
%:h
to get the path part of the current file. i.e.,cd %:h
to set the working directory to the directory of the current file. - Use
:reg
to view the contents of all registers, or:reg{register}
to view the contents of one. :Man
command - Open that command's man page:dig
- open a list of all digraphs available on your system (complication options can affect the list):reg
- open the vim registers- Toggle Vim boolean options
- adding a bang
!
at the end.:set number!
,:set cusorcolumn!
:set number
will turn the feature on:set nonumber
will turn the feature off
- adding a bang
- Check the current state of any option by adding a question mark
?
to the end::set number?
will return eithernumber
ornonumber
depending on if the option is on or off. - Repeat the history command
- Enter command mode
:
and then press<C-p>
to cycle back through your history, finding a command and invoking it again. - if the command was the last colon command you ran, simply press
@:
to repeat the last command.j
- Enter command mode
- Delete using the
"_d{motion}
command to delete without overwriting your default register. - Non-printable Characters
^M
is a single character inserted by usingCTRL-v
followed byCTRL-m
. UTF-8 C1 Controls and Latin1 Supplement
Others
You can edit your visual selection by using
o
to bounce your cursor to the opposite end of the selection. Adjust the top bound as needed, then presso
to return to the bottom.Use
gv
in Vim to reselect the last visual selectionfilename-modifiers。关于Unix:在Vim中打开与当前文件相同的文件夹中的文件
:p
Make file name a full path,:h expand
,:wildcards
Use
ea
to append to the end of the current word.Swap two characters in Vim with
xp
Put from the
"%p
register to put the name of the current file.To make it easier to navigate conflict markers, you could tweak the built-in matchit plugin: stackoverflow.com/a/71676129/853…
Multiple cursor support
Open the quickfix window with
:cwin
and see the results. Next we do another search. To get back to our previous, older quickfix window, we use:colder
. Then, we can return to the most recent search results with:cnewer
.To find out what’s stored in each register, simply use Vim’s
:registers
command, or:reg
for short.Use
m{capital letter}
to make a global mark. Close and reopen VIm, and press'{capital letter}
to open the file w/ the global mark.使用命令
:mksession ~/.mysession.vim
(:mks
is ok) 保存已经打开的多窗口文件。 如果文件重复,vim 默认会报错,使用:mksession! ~/.mysession.vim
代替。在终端输入命令vim -S ~/.mysession.vim
打开保存的会话。.
小数点可以重复上一次的命令N<command>
重复某个命令 N 次args
:args /path/*
:wn
https://vimtricks.com/p/vimtrick-edit-files-sequentially/ - Edit files sequentially
Helps
:help wildoptions
- command-line completion allows fuzzy-matching in some cases:help registers
-"0p
will paste from the0
register, which automatically contains the last yanked text.:help global
:help no-greedy
- because.*
is greedy. It matches the maximum amount of text it can..\{-}
will match the fewest characters possible to make a match.:help jumplist
- jumps are cursor movements stored in a list called the jumplist. movements which modify the jump list are:/pattern
searches and?pattern
searches (forward and backward pattern matching)*
and#
(forward and backward search for the word under the cursor.%
(jump to a matching enclosing character like paren, brace, bracket, etc)- Any inter-file navigation like
gf
:help scroll-cursor
:help option-list
- all vim options:help usr<tab>
:help buffer
:help window
:help <tab>
:help split
:help motion
:help options
:help <tabcomplate or ctrl-d>
:help <specific option name>
:help quickfix
:help macros
:help motions
:help substitude
:help g