Hot answers tagged

99

Vim comes with an exhaustive and fully indexed documentation that contains the answers to most of the questions you may have on using Vim. But the documentation is huge and may look to the neophyte as an impenetrable maze. Here are a few guidelines to help you find what you need… The :help command is your gateway to Vim's documentation. Read the first ...


86

Quincy's answer is fine, but there's an exact way to do this which doesn't require editing the buffer: :%s/pattern//ng This will print a message like 3 matches on 2 lines, and no changes will be made to your buffer. The n flag makes the :substitute command print the number of matches instead of performing an actual substitution; the g flag enables ...


78

I actually have a pretty similar workflow to yours, (copying and pasting blocks that are similar, then using :s to change variable names) especially when I'm writing lots of lines that are similar except for which variable they use. But there are a couple things I do that you might find useful. General vim things The first thing that will help you is ...


40

As an alternative to :noh, I like to do :let @/="" mapped to a keyboard shortcut. The difference is that :noh leaves the search term in the search register, so n and N in normal mode resume the search by jumping to the next/previous match and re-highlighting. Using :let @/="", on the other hand, causes the message E25: No previous regular expression and ...


31

:vimgrep pattern % :cwin vimgrep will search for your pattern in the current file (%), or whatever files you specify. cwin will then open a buffer in your window that will only show the desired lines. You can use pretty much any navigating/search command within the cwin buffer. Press return to jump to the line under your cursor in the source file.


29

There are several ways to do this. Inside Current Directory If you want to perform the search/replace in a project tree, you can use Vim's argument list. Simply open Vim and then use the :args command to populate the argument list. You can pass in multiple filenames or even globs. For example, :args **/*.rb will recursively search the current directory ...


28

You can change the behaviour of n and N to search consistently downwards/upwards by useing the following: nnoremap <expr> n 'Nn'[v:searchforward] nnoremap <expr> N 'nN'[v:searchforward] Update: How does it work? This uses expression-maps, which maps the lhs of the map to an expression that will be evaluated, everytime the lhs is encountered. ...


27

Simply type :noh<cr> (Where <cr> symbolizes a carriage return, i.e. Enter.) The full non-abbreviated version of this command is :nohlsearch. For convenience, you can have a mapping such as nnoremap <Leader><space> :noh<cr> in your .vimrc. Since my leader is Space, this allows me to clear highlighting simply by tapping ...


22

You can use this command to print lines containing This and not red: :g/\(.*This\)\&\(.*red\)\@! \& separates the branches like a logical AND because we want the lines that satisfy the two conditions (branches), \(.*This\) is the first branch, it could simply be This but it is more generic that way, \(.*red\)\@! is the second branch, \@! means "...


21

Let's say we have a simple project structure like this: greeting.txt looks like and info/age.txt looks like Let's say we want to replace all occurences of Sam with Bob. Here's what we would do: Set working directory Make sure Vim's current working directory is the root of the project (won't show you how to do this here). Find files that contain 'Sam' ...


21

There is a much more convenient way. Rather than using :set nohlsearch which actually turns the hlsearch setting off, use :nohls This will only turn hlsearch off until you search again. From :help nohls *'hlsearch'* *'hls'* *'nohlsearch'* *'nohls'* 'hlsearch' 'hls' boolean (default off) global {not in Vi} ...


19

Here you go: :g/foo/t.|s//bar Decomposing: :g/foo/ " start a global command applied on all lines matching 'foo' t. " duplicate the current line (the cursor is now on the new line) | " chain a new command s//bar " substitute the last searched element with 'bar' Because the g command will update the search pattern, so you can ...


19

The c_CTRL-R family of maps can improve your workflow quite a bit: You need not type variable names when using :s/, just use CTRL-R CTRL-W to insert the word under the cursor into the command line (caveat: this does not escape special characters, like * does). After making a small change using ce, you can search for the old word using / CTRL-R -. Then use ....


18

You can do : wd4/x<Enter> If you start on the top left of your text Explanation w : move to beginning of next word d : delete 4/x<Enter> : until the 4th occurence of x If you don't know the number of times you would like to do it beforehand, you can also do : d/x<Enter> and then hit . to repeat


17

Disable search highlighting permanently Matches won't be highlighted whenever you do a search using / :set nohlsearch Clear highlight until next search :noh or :nohlsearch (clears until n or N is pressed or a fresh search is performed) Clear highlight on pressing ESC nnoremap <esc> :noh<return><esc> Clear highlight on pressing ...


17

You can use the :global and :normal commands for this. The :global command has the following syntax: :global/{pattern}/{command} This will run "command" on every occurrence of "pattern". You can then use the :normal command to run a series of keystrokes on each occurrence of a word using :global. Like so: :g/WORD/normal {keystrokes} If those ...


16

Performing a substitution on several consecutive lines is pretty easy: :2,11s/^/word / but a range can't cover non-consecutive lines. With a bit of creativity, though, it is entirely possible to work around that "limitation". Indeed, you can repeat the last substitution with :& or :&& (the former will not preserve the original flags, the ...


16

You can use the very-nomagic switch. See :h \V for details. This would give you %s/\V[ERROR] Login unsuccessful/something/g. There is also the nomagic switch \M, which is a lighter version of \V and does not seem to be widely used. Also note that you don't have to escape spaces as you do in your example, even when not using \V.


15

Here are a few ways to do it. 1 | $color = "#fff"; 2 | function PickColor () { 3 | $color = "#bbb"; 4 | $newColors = ["#000", "#fbf", $color]; 5 | foreach ($newColors as $c) { 6 | if ($c == "#fff") { 7 | break; 8 | } 9 | } 10 | } 11 | $differentColor = $color; I've modified your example to include line ...


15

:%s/pattern//n The n flag in the end tells :s command to report the number of matches and not actually substitute. Read :h :s_flags for more details.


15

You can use :vimgrep /pattern/ {files} to populate the quickfix list with matching patterns. The problem is that :vimgrep files option doesn't directly allow for buffers. You can use: % Is replaced with the current file name. *:_%* *c_%* # Is replaced with the alternate file name. *:_#* *c_#* #n (where n is a number) is replaced with *:...


15

From :h :nohlsearch :noh :nohlsearch :noh[lsearch] Stop the highlighting for the 'hlsearch' option. It is automatically turned back on when using a search command, or setting the 'hlsearch' option. This command doesn't ...


15

Type in normal mode /<ctr-v>u0303 / - start search <Ctr-v>u - init utf-8 code input 0303 - hex code combine character. :he unicode Also :he mbyte-combining and :he utf-8-char-arg the last one covered case with commands like f, F and so on.


15

You can just use T, and F, instead of t, and f,. The lowercase commands go forward; the uppercase ones go backwards. Example: foo, b|ar, baz With the cursor on the first a, t, would move forward to r (the character before the next comma). Now if you use T, you will go backwards to the character after the previous comma (i.e. the space after foo,). f and ...


15

As @muru mentioned in the comment, you could use an equivalence class (described in :help /[[) which seems to be a character class expression evaluated as a set of similar characters (i.e. are the same once you remove any accent/diacritic). For example, to look for kočička and kocicka with the same pattern, you could use this: ko[[=c=]]i[[=c=]]ka where [[=...


15

Yes, you have the history command: :history / Note that it can be used for /, :, =, >, ?, @, all, cmd, debug, expr, input and search. Alternatively you can use the q:, q/ and q? commands to see previously entered commands and searches on a sepearate buffer. You can then modify as you want and replay them by pressing <CR>. See :h :history, :h q:.


15

This looks like you indeed found an obscure bug. I have implemented the gn textobject back in 2012 for Vim 7.3 something. It basically works in the following way: 1) It searches backwards for the last match of the current regular expression. 2) It searches forward for the next match of the current regular expression. This should make clear, that the ...


14

This is not the more pager, this is Vim's internal and minimalistic pager which doesn't have search capabilities. But you can use the :redir command: :redir @a redirect output of following commands to register a :let list every current option and its value G<CR> go straight to the end of the listing and make it disappear :redir END ...


14

Use the gn motion to edit the current search match. The following example will change the current match to "FooBar": cgnFooBar The great part about this technique is that you can repeat the change via .. Assuming the next match will be changed the same way you can just use . to repeat. Or skip a few matches via n and then use .. Vimcasts has a nice ...


14

You can use the gn motion for that, it selects the next searched element. You can use it like so: /foo<CR> gn -> select the next "foo" sbar<Esc> -> (optional) substitute it with "bar" Bonus: To have . repeat the search and the change, use a c to do everything in one command: /foo<CR> cgnbar<Esc> ... -...


Only top voted, non community-wiki answers of a minimum length are eligible