====== Редактор VIM: Советы и секреты ====== К [[http://twitter.com/numberone/statuses/48276722|вопросу]] о выборе правильного редактора: пользователи Windows, обратите внимание на знаменитый [[http://iar.spb.ru/why/vim|редактор Vim]], а точнее — на его “переносную” версию под Windows: [[http://portablegvim.sourceforge.net/index.html|GVim Portable]]. И не забудьте посетить сайт [[http://www.vim.org/index.php|vim online]], содержащий хорошую подборку [[http://www.vim.org/docs.php|документации по Vim]]. * [[http://iar.spb.ru/why/vim|Немного о редакторе vim]] * [[http://iar.spb.ru/tips/vim-scripts|Расширения vim]] * [[http://lib.ru/unixhelp/vibegin.txt|Редактор VI. Список команд]] ===== Поиск и замена текста ===== 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//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'' Замена результатов предыдущего поиска: /ищем-что-то :%s//меняем-на-другое/g В итоге будет произведена замена ''ищем-что-то'' на строку ''меняем-на-другое''. Плюс в том, что не надо дважды вводить выражение для поиска, и исключается возможность ошибки в написании. ===== Copy and paste text ===== 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 ===== :set number or shorter: :set nu An even better way, if you like maps: Put this in your .vimrc file:\\ '':nmap :set invnumber ''\\ By pressing Ctrl-N twice (or some other shortcut you fancy) in normal mode, you can toggle between showing and hiding line numbers.