Hot answers tagged

15

Yes, you can do that by using an expression. For this particular case, you can use Vim arithmetic and calculate 1-x, which will turn 0 into 1 and 1 into 0. :s/\d/\=1-submatch(0)/g For a more general replacement case, you can use a Dict to store the mapping of match to replacement string. For example, to turn the digits into written numbers: :let repl = {"...


14

There's a bunch of excellent answers here already, but for the sake of completeness I feel like I should point out that, in most practical respects, if you concatenate multiple ex commands with the bar character |, they act like a single operation. In particular, they will lead to a single item in each of the undo tree and changelist. As such, the ...


10

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) ...


9

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 ...


7

In your specific use case, i think what you can do is this handy oneline: %s/0\|1/\=!submatch(0)/g That basically inverts the match.


7

Sure: %s/[01]/\="10"[submatch(0)]/g Matches either a 0 or 1 and replaces it with either "10"[0] = 1 or "10"[1] = 0


7

This answer is longer than others, it didn't take the specific data value advantage, it's a general solution to replace n different values with n different replacements: let d={0:1,1:0} | %s/\v(0|1)/\=d[submatch(0)]/g | unlet d So if you want to change Hello vim 8 to Oh god 9 in a single replacement: let d={'Hello':'Oh','vim':'god',8:9} | %s/\v(Hello|vim|...


6

I'm actually surprised there is no native tr in vi/vim, but if your platform is UNIXy enough, there's an external command for just this: :%!tr 01 10 This is what I would personally use, but it may not be portable enough for you.


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" ...


5

I'm quite certain I've already gave an answer to the general case either here on vi.SE, or on stackoverflow, but impossible to find the Q/A... In that answer I provide the command CycleSubstitute that could be used in your case in the following way :[range]CycleSubstitute/0/1 I've packaged it here. And it's implemented in the following way: :command! -...


5

Similar to @LucHermitte's answer, the SwapStrings plugin lets you do :%SwapStrings 0 1 My own PatternsOnText plugin provides (among many others) a :SubstituteMultiple command that can do swaps as a corner case of doing multiple substitutions atomically (that is, without introducing a temp replacement): :%SubstituteMultiple /0/1/ /1/0/ g


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 ...


4

The difference is that :find will actually search for a file by that name in the list of directories specified in 'path'. The default 'path' contains . (which is the path of the current file), then /usr/include (on Unix platforms, like Linux and Mac OS) and then an "empty" entry, corresponding to the current directory. Which means if you're editing src/...


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

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

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

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...


3

Ralf’s answer is the correct one, but I’ll add some more generically useful commands: Prepend :vertical to any command which splits and it will use a vertical split Similarly, prepend :tab to any command which splits and it will use a tab These more general commands work for most things (so :tab help does what you thing), so they are good to have in your ...


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

Try https://github.com/ggvgc/vim-fuzzysearch Then simply... nnoremap / :FuzzySearch<CR> let g:fuzzysearch_prompt = '/' Then / will take you to a prompt where you enter your pattern and it all looks just like normal Vim. I believe From there n and N will work without modification. If they aren't already enabled then set incsearch and, possibly, ...


2

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


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 : ...


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