# Delete / Yank lines from current line up to and including line containing search pattern

Suppose I have the following text (cursor is in middle of first word 'begin.'

blah blah blah blah stuff stuff stuff
\\\begin{tabular}
{
|p{0.25\textwidth}
|p{0.2\textwidth}
|p{0.5\textwidth}
|}
\hline
Item & Type & Notes\\\hline
Text text text text text text text text &
Text text text text text &
Text text text text text text text text text text text text text text text text text
text text text text text text\\\hline

Installation instructions &
.txt / .html / TBD &
Installation help should be conspicuous but not the central item\\\hline
\end{tabular}
blah blah blah more stuff more stuff


I want to cut the table out and move it somewhere else. The lines are actually much longer so they wrap several times and I can't easily determine how many lines I want to cut.

I type d 2 / a r } / e to delete until the second match of "ar}" stop at the 'e'nd of the pattern.

But this grabs from the middle of the word 'begin' to the end of where I want to cut. I'd like to grab complete lines and I feel like there should be a shortcut for this in under 6 keystrokes. I'm not using Latex-VIM because it doesn't like my standard Windows installation of GVIM so no, I can't (as far as I know) easily grab from the \begin to the \end. I'd love to be proven wrong on that.

Yes, this question is potentially a crossover of StackExchange sites including Tex, StackOverflow, and Unix. Feel free to suggest a better site for asking this.

-

## migrated from tex.stackexchange.comFeb 10 at 20:04

This question came from our site for users of TeX, LaTeX, ConTeXt, and related typesetting systems.

Well, you seem to know that this is off-topic here. We generally seem to accept editor-related questions only when they have a direct connection to LaTeX in any sense. This is not your case, since, if I understand it correctly, you want to grab whole lines starting where you are and ending at the second match for some string. Anyways, V/ar}<CR>nx should do the job. –  tohecz Feb 10 at 19:56
Ok, how do I migrate it? Delete and repost there or..? –  Okuma.Tony Feb 10 at 19:59
@cmhughes Yes, that is the simple answer, but the sequence was already rather long. I was looking for the short easy way. –  Okuma.Tony Feb 10 at 20:00

With an Ex command:

:,/\\e/d


Breakdown:

• Structure of an Ex command: :{range}command{address}.
• One can use line numbers, marks or search patterns to delimit the range, here we start with the current line, usually a . but it can be skipped like here, and we finish with a pattern matching \end.
• We delete the lines in that range.

With a combination of search and contextual marks:

/\\e<CR>
d''


Breakdown:

• Search for \e.
• delete from here to the line of the previous cursor position.

With visual mode:

v/\\e<CR>
V
d


Breakdown:

• Visually select from here to \e.
• Switch to visual line block.
• delete the lines in the selection.

Variation of the method above with an Ex command instead of a normal mode command:

v/\\e<CR>
:'<,'>d


Breakdown:

• Visually select from here to \e.
• Enter command-line mode with :, the '<,'> range is inserted automatically.
• delete the lines in the selection.
-
Simple. Eloquent. Amazed. If only verbal languages could be so optimized... I'm having trouble getting it to search for second occurrence of end when there's a nested \begin\end inside. –  Okuma.Tony Feb 11 at 15:33
@Okuma.Tony, you can do a second search like this: /\\e/;/\\e so the 2nd solution becomes /\\e/;/\\e<CR>d'', the 3rd solution becomes v/\\e/;/\\e<CR>Vd and so on. The ; offset sadly doesn't work in a range. You should try benjifisher's suggestion, it's really great. –  romainl Feb 11 at 15:52
@Okuma.Tony, correction, you can use more than one search in a range but the syntax is a little bit different: :,/\\e//\\e/d. –  romainl Feb 11 at 15:59
@romainl: Thanks. –  benjifisher Feb 11 at 22:35

This is one of the features you get with the matchit plugin, part of the standard vim distribution. For testing purposes, do this interactively; if you like it, then you can do this from your vimrc file:

:runtime macros/matchit.vim
:filetype plugin on


After those two commands (either interactively or during startup as part of your vimrc file)

:e foo.tex


and then, with the cursor anywhere on \begin{tabular}, use V% to select the entire environment. I am not sure whether you will consider it a bug or a feature, but this will include entire lines, meaning that you will get the "\\" at the beginning.

This should also handle nested environments, and commented-out lines, correctly.

Coincidentally, the maintainer of matchit.vim is the same as the maintainer of the standard ftplugin for tex.

:help matchit-install
:help :filetype-plugin-on

-
This only selects from cursor position to the end of the first encapsulation block. In if it's on the \begin{tabular} line, it selects to the }. If cursor is on a line like \node [block] (start) ... in the middle of the word node, it selects from cursor to ].. –  Okuma.Tony Feb 11 at 16:01
@Okuma.Tony, matchit relies on the filetype of your buffer being correctly set. With :setf tex, pressing % with the cursor on \\\begin{tabular} jumps to the correct \end{tabular} –  romainl Feb 11 at 16:07
@romainl Does this depend on latex-vim being installed? I have standard install of VIM 7.4 on Windows 7 and .tex files are recognized well enough to handle automatic indenting. –  Okuma.Tony Feb 11 at 16:31
@Okuma.Tony: No, it does not depend on latex-vim. The matchit plugin is part of the standard vim distribution, in \$VIMRUNTIME/macros/. When I refer to the "standard" ftplugin for tex, I mean the one that comes in the standard vim distribution. –  benjifisher Feb 11 at 22:32
@Okuma.Tony: In response to your first comment, you are describing the default behavior of %, which is overridden by the matchit plugin. Please pay attention to the order of the commands in my answer. –  benjifisher Feb 11 at 22:34