Hot answers tagged

6

There's two different regex features that would be helpful. First off, you can use capturing groups. If you put something in parentheses, you can refer to whatever matched inside. So /\(.\)\1 Will match any character (\(.\)) followed by that same character (Group 1, \1). You can also refer to groups in the replacement part of a substitute command. The ...


5

The following enables fuzzy file search (i.e :find script.js) through every file in your project root while excluding the node_modules dir and its contents. set path+=** set wildignore+=**/node_modules/** Tested on: VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Sep 11 2017 13:57:34) ...


5

You can use \zs (and it's complement, \ze), e.g. /:q: \zs should work for your example (assuming there is exactly 1 space after the second :) It works by setting the start (or end in the case of \ze) of the matching text to a point somewhere in the middle of the regex. Check out :h \zs, it has a much better description than I can give, with additional ...


5

The sneak plugin provides that functionality. By default, it's bound to the s key, so you would need to use sts for your example. The documentation also gives example mappings to put sneak's functionality on the f family of keys or to enhance the standard 1-character searches with some of sneak's other improvements.


5

gn + <Escape> gn Search forward for the last used search pattern, like with n, and start Visual mode to select the match. If the cursor is on the match, visually selects it. If an operator is pending, operates on the match. E.g., "dgn" ...


4

To get the content of the register "o you can use: getreg('o') This will return the content of the register as a string. To match a string you can use match(): echo match(getreg('o'), 'image') You'll get -1 if the register doesn't contain image and a positive interger otherwise. See :h getreg() :h match()


4

That % in the %s command at the beginning of your command specifies a range and tells the command to be run for all lines (short for 1,$, where $ stands for the last line). Besides line numbers, you can also specify marks or even specify search items. For the search you can specify the full range of regular expressions that vim knows about and you can even ...


3

One way to do this is by using \zs to set the "start" of the match, so that everything before \zs is untouched by the replacement: :%s/RESULT=\zs.*/200/g Your original attempt using a lookbehind was also on the right track: you just put the .* part in the wrong place: :%s/\(RESULT=\)\@<=.*/200/g This replaces anything that comes after a RESULT=, ...


3

You can change the location in which the cursor is left after a search by using an offset: You can either an "end" or "start" offset for your specific example: /:q:/e+2 /:q:/s+4 See :help search-offset for more details. You can apply these offsets retroactively to an existing search by using an empty regular expression: /:q: //e+2


3

I don't fully understand your question, i know two ways to find all executable files inside a folder: readdir( require patch 8.1.1120) echo readdir('dir', {v-> executable('dir/' . v) && !isdirectory('dir/' . v)}) :h readdir() return a list of files and subdirectories, you can provide a :h lambda expression to filter it. The return name is not ...


3

As you state, your string contains / so presumably that is tripping you up since it must be escaped if you use / to delimit the sections of your substitution command. See the other answer here for solution with escaping. But we don't have to use / as our separator. We can use any non-alphanumeric, single-byte character except \, " or |. For example, with #......


3

vim-rails and vim-ruby are setting path, and they don't really care that path is limited to 1024 characters on some platforms throwing out what you set. Solved with autocmd: let g:project_find_path = '.,' . system("git ls-tree -d HEAD --name-only | tr '\n' : | sed 's/:/**,/g'") autocmd VimEnter let &path = g:project_find_path autocmd BufReadPost * let &...


3

Besides the plugins mentions, there are several other plugins available, that extend the f and t motions. Among there are clever-f, vim-fanfingtastic and my own ft-improved. At least for my plugin I can tell you, that you can configure it, to check for several following characters. You have to set :let g:ft_improved_multichars = 1 in your vimrc for that. ...


3

The easymotion plugin also provides functionality like that. Easymotion is considered to be larger with more features, both come highly recommended. Maybe someone needs to write a review of both, while checking them out...


2

You can add various parameters or commands for the offset of where the cursor lands. /test/e on the last t of "test More info here: http://vimdoc.sourceforge.net/htmldoc/pattern.html#search-offset


2

You can use the search() command for this. Something like: nnoremap ) :call search(')')<CR> "Next nnoremap ( :call search('(', 'b')<CR> "Previous Edit: Origial response before edit: nnoremap ) yl:call search('<C-r>"\C')<CR> " Next instance nnoremap ( yl:call search('<C-r>"\C', 'b')<CR> " Previous instance ...


2

A basic version of this is actually in the reference manual as an example of how to use the getchar() function: This example redefines "f" to ignore case: :nmap f :call FindChar()<CR> :function FindChar() : let c = nr2char(getchar()) : while col('.') < col('$') - 1 : normal l : if getline('.')[col('.') - 1] ==? c : break : endif :...


2

Here is another approach: nnoremap <silent><expr> \f MySearch('f') nnoremap <silent><expr> \t MySearch('t') func! MySearch(cmd) let arg=getchar() if arg !~ '^\d\+' " no real key, mouse click or something unusual, so abort return a:cmd."\<esc>" else let arg=nr2char(arg) endif let pat=...


2

Before the actual answer, a personnal opinion: I wrote the code to show that it can work but I don't think it is a good idea to use this code: having f and t going backward and F and T going forward might be pretty disturbing. I think it is better to use , and ;: if f; doesn't find a character then simply use , to replay the motion in the other direction: ...


2

This should do what you want: :%s/&DB2_PORT;\/&DB2_DATA;/\0:tcp=true/


2

For your particular case, you can use :vertical sfind, in which case you can Tab-complete the filename, same as you can do with :find. Short version is: :vert sf lo<TAB> Which will complete to: :vert sf long_name_module.py And will open your file in a vertical split as you requested. More generally, Vim doesn't really have a concept similar to "...


2

No, named capture groups are not available. In fact, some design decisions in Vim actually expose the limit of 9 (numbered) capture groups, such as the matchlist() function, which returns a list of 10 strings for each of \0 through \9. (That doesn't mean named groups would be impossible, it's just exposing some internals showing this is quite an ingrained ...


2

@Rich's solution is a perfect general solution, however maybe your question is a good example of an XY problem. For your specific use case Vim has :h CTRL-A and :h CTRL-X: CTRL-A Add [count] to the number or alphabetic character at or after the cursor. CTRL-X Subtract [count] from the number or alphabetic character ...


1

If you want to indent html, use the indent operator = with the inner-tag text object it: Place your cursor on the tag whose contents need indenting Press =it


1

Is sed an option? sed -r '/<blockquote>/,/<\/blockquote>/{s/^(\s\s\+)(<[^>].\{-}>)/\1 \2/g}'


1

Try: :g/lang="EN"/s%^\(.\{-}\)\(<span .\{-}</span>\)%\1\2\2%|s%lang="EN"%lang="FR"% Breakdown: g/lang="EN"/ for all lines that contain lang="EN" s%^\(.\{-}\)\(<span .\{-}</span>\)%\1\2\2% ^\(.\{-}\) match the shortest possible prefix \(<span .\{-}</span>\) match from first <span to first </span> \1\2\2 replace with ...


1

Try it like this: :%s/\(\w\+\),\s\(\w\+\)/\2 \1/g Or for simpler syntax without all the escapes use \v for very-magic ( thanks to @D.BenKnoble ) :%s/\v(\w+),\s(\w+)/\2 \1/g


1

Please note that I changed your mapping to suit my keyboard. However this is what I got in terms of functionality: nnoremap <C-P> :MyFind0<CR> nnoremap <Leader><C-P> :MyFind1<CR> command! -bang -nargs=* MyFind0 call MyFind(0, <q-args>, <bang>0) command! -bang -nargs=* MyFind1 call MyFind(1, <q-args>, <bang&...


1

Here is a one-liner solution built on top of getchar()) nnoremap <silent> f :silent! exe "normal! ". substitute(matchstr(getline('.')[col('.') :], '\v\c.{-}'.nr2char(getchar())), '.', "l", "g")<cr> The idea is to extract all the characters from the current position till the character typed, and to convert them in as many <right> as ...


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