'How to match git conflict markers with %
In vi and vim, % can be used to find a matching symbol e.g.:
/** <- With the cursor on / ...
* Some comment
*
...
*
*/ <- % moves the cursor to the / on this line
This works with matching {}, () [] pairs, and also works with c-conditionals like #ifdef.
Is there a setting to make this work with git conflict markers?
#! /usr/bin/env ruby
def hello
<<<<<<< HEAD <- With the cursor on this line
puts 'hola world'
=======
puts 'hello mundo'
>>>>>>> mundo <- % moves the cursor to this line
end
Can (and how) vi and/or vim be configured to do that?
Solution 1:[1]
In your startup files (eg $HOME/.vimrc) you can add:
packadd! matchit
let b:match_words = '<<<<<<<:=======:>>>>>>>'
packadd! enables the optional plugin, and match_words specifies the match pattern. Note that this will override any previous assignment of match_words, and this sort of thing is typically better suited to store in a filetype plugin. Adding those two lines directly in the main startup file should work, but may not be the best solution. (eg, if you're merging a file whose type is recognized, the filetype plugins may override the setting of match_words)
Solution 2:[2]
Assuming you are actually talking about Vim (which makes this configurable) and not vi (which doesn't).
The functionality is documented under :help %:
% Find the next item in this line after or under the
cursor and jump to its match. |inclusive| motion.
Items can be:
([{}]) parenthesis or (curly/square) brackets
(this can be changed with the
'matchpairs' option)
/* */ start or end of C-style comment
#if, #ifdef, #else, #elif, #endif
C preprocessor conditionals (when the
cursor is on the # or no ([{
is following)
For other items the matchit plugin can be used, see
|matchit-install|. This plugin also helps to skip
matches in comments.
From there, you can follow the 'matchpairs' tag to :help 'matchpairs', which won't help you because you can only add pairs of single characters like <:>.
You can then follow the promising matchit-install tag, which should put you on the right path.
Try harder.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | William Pursell |
| Solution 2 |
