Translations of this page:

Редактор VIM: Советы и секреты

GVimPortable - носим Vim на флешке

К вопросу о выборе правильного редактора: пользователи Windows, обратите внимание на знаменитый редактор Vim, а точнее — на его “переносную” версию под Windows: GVim Portable.

И не забудьте посетить сайт vim online, содержащий хорошую подборку документации по Vim.

Поиск и замена текста

Tip #31: Find and Replace

To find and replace one or more occurences of a given text pattern with a new text string, use the s[ubstitute] command.

There are a variety of options, but these are what you most probably want:

:%s/foo/bar/g find each occurance of 'foo' and replace it with 'bar' without asking for confirmation
:%s/foo/bar/gc find each occurance of 'foo' and replace it with 'bar' asking for confirmation first
:%s/<foo>/bar/gc find (match exact word only) and replace each occurance of 'foo' with 'bar'
:%s/foo/bar/gci find (case insensitive) and replace each occurance of 'foo' with 'bar'
:%s/foo/bar/gcI find (case sensitive) and replace each occurance of 'foo' with 'bar'
Without the 'g' flag, replacement occurs only for the first occurrence in each line.

For a full description and some more interesting examples of the substitute command refer to

:help substitute

See also:

:help cmdline-ranges
:help pattern
:help gdefault

Tip #1501 - substitute last search : vim online

Замена результатов предыдущего поиска:

/ищем-что-то
:%s//меняем-на-другое/g
В итоге будет произведена замена ищем-что-то на строку меняем-на-другое. Плюс в том, что не надо дважды вводить выражение для поиска, и исключается возможность ошибки в написании.

Copy and paste text

Copy and paste text with vi or vim

The command 'Y' or 'yy' copies (yanks) one or more lines. To copy one line, two lines, 10 lines, and all lines to the end of the file, respectively:

Y
2Y
10Y
yG

To paste the text contained in the buffer above (uppercase P) or below the current cursor position (lowercase p), respectively:

P
p

It is also possible to yank text within a line. The following commands yank text from the current cursor position to the end of the word and the end of the line, respectively:

yw
y$

The same commands paste the text within a line. Lower case p pastes after the cursor position and upper case P pastes before.

Paste will also work with deleted text, either lines or parts of lines. Be careful not to execute any other commands prior to pasting as this will empty the buffer.

Show/hide line numbers

Tip #19 - line numbers...

:set number
or shorter:
:set nu

An even better way, if you like maps: Put this in your .vimrc file:
:nmap <C-N><C-N> :set invnumber <CR>
By pressing Ctrl-N twice (or some other shortcut you fancy) in normal mode, you can toggle between showing and hiding line numbers.

 
linux/vim-tips.txt · Последние изменения: 04.08.2023 02:32 (внешнее изменение)
 
Recent changes RSS feed Creative Commons License Driven by DokuWiki