2

I'm trying to do something fairly simple. I'm writing latex and I have all these blocks of code, that I'm trying to comment out (and disregard the words in the word count). So I have to write these lines a bazillion times:

%TC:ignore
\iffalse

and this as well:

\fi
%TC:endignore

Is it possible to setup a shortcut, so if I type something along the lines of:

:zlc[ENTER]

it then inserts one of the code-chunks?

Then I could setup my own kind of tailored standard clipboard in my VIMRC-file. So I shouldn't put it all into different registers every time I start Vim.

Edit1

Just as I saw the answers, I knew what I had forgotten to search for, which is 'abbreviations'. I would just add it here, so this post would come up, if someone else Googled it.

1

Sure, you've got a couple options.

Use an abbreviation:

iabbrev zlc %TC:ignore<CR>\iffalse
  • If you type zlcEnter in insert mode it will fill in the text.
  • iabbrev is an abbreviation that only works in insert mode

Use a function:

command! Zlc call Ignore()
function! Ignore()
   normal! i%TC:ignore
   normal! o\iffalse
endfunction
  • If you type :ZlcEnter in normal mode it will fill in the text.
  • Note the capital Z; user commands cannot start with a lowercase letter.

Place either of these in your vimrc to make them work.

See :h abbrev, :h command, and :h normal for more info.

  • 1
    I didn't get that whole thing about ^M. I'm afraid to edit your answer, since I may just be too dumb to see what you did there. But your answer combined with a tiny bit of Googling about Multi-line Abbreviations gave me the final answer that I was looking for, which is this: iabbrev zlc %TC:ignore\<CR>\iffalse ... Where \<CR> gives you a carriage return. By the way, to those who are thinking 'What the truck is that 'i' doing in front of abbrev (in iabbrev), then I can reveal to you that it is an abbreviation that is only valid in 'Insert mode' (I found that by reading a bit in :help abbrev ). – Zeth Sep 9 '16 at 16:09
  • 1
    The ^M is a raw carriage return. Your \<CR> should work just fine, but you can get ^M by going into insert mode and then pressing CTRL-V+Enter. It's one character but it is represented by ^M. – Tumbler41 Sep 9 '16 at 16:59
  • Edited to remove confusing ^Ms. – Tumbler41 Sep 9 '16 at 17:07
1

Not exactly what you were asking but as @Tumber41 already gave you a solution, here is a possible alternative just to show that you can achieve things in different ways with vim:

function! AddCommentLines() range
    call append(a:firstline-1, "%tc:ignore")
    call append(a:firstline, "\iffalse")


    call append(a:lastline+2, "\\fi")
    call append(a:lastline+3, "%tc:endignore")
endfunction

vnoremap <key> :call AddCommentLines()<cr>

This function will add the lines around a visual selection. So you can visually select the text you want to comment, press the key combination you'll have defined in the vnoremap line and your comments will appears around the selection.

You can also call it directly with a range: say you want to comment lines 100 to 110:

:100,110call AddCommentLines()

And if you don't provide a range only the current line will be surrounded by the comments.


Also I would recommend to try using some snippets see this question to choose a snippet engine. For example with Ultisnips you could add theses lines in the latex snippets file:

snippet com "Add opening comment block"
%TC:ignore
\iffalse
endsnippet

snippet ccom "Add closing comment block"
\fi
%TC:endignore
endsnippet

This way when you'll type com followed by the key you choose to trigger a comment the first comment block will be added. And when you type ecom followed by the snippet trigger key the second block will be added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.