Custom formatting of LaTeX (beamer) frames in Vim using the gq command - vim

I want to be able to use the gq key combination in Vim to format the text in my Beamer LaTeX slides.
For example, I'd like gqap to transform this:
\begin{frame}{Some title}
\begin{itemize}\item first point
\item second point
\item etc. point \end{itemize} \end{frame}
into this:
\begin{frame}{Some title}
\begin{itemize}
\item first point
\item second point
\item etc. point
\end{itemize}
\end{frame}
Specifically, I'd like to observe the following rules:
\item always begins on a new line
\begin and \end always begin on a new line
\begin{frame} and \end{frame} always start in column 1
environments within environments are indented 2 spaces
\item is indented 2 spaces within its environment
Thus, my core question relates to how the above could be achieved.
Alternatively, any suggestions on starting points or useful tutorials would be most helpful.
I'd also be interested in any comments about the relative merits of formatexpr, formatprg, or internal formatting for performing such formatting.

After a little searching I found this useful answer on Tex.SE to a similar question. It provides a dedicated function for formatting Context source, which could be adapted to LaTeX.
I think I'd prefer something integrated with the built-in gq functionality in Vim. And the answer still requires a little bit of work to get configured with LaTeX, and I'm not sure what indenting it provides. Anyway, I thought I'd share it in case it was useful to others.

why don't try snipmate plugin
begin<tab>
to
begin{<cursor>env}
%text
\end{env}

Related

Tab number, instead of number of windows

Above is default tabline, I want that instead of number of windows I ll see there index of tab, in the above example '1 quickfix.txt | 2 tabpage.txt' to simply call [index]gt to go to target page. Is it possible to acquire default string for tab, for example '2 quicklix.txt' then swap first digit with tabnumber to get '1 quickfix.txt'?
P.s.
I don't want to write scripts the way vim is suggesting, see :help tagline, in the sense, that there many features like cropping and showing if file is modified, which I like and don't want to write them from scratch. What I want is to get default string for tab and modify it the way I explained earlier.
P.s. I don't want to write scripts the way vim is suggesting, see :help tagline, in the sense, that there many features like cropping and showing if file is modified, which I like and don't want to write them from scratch.
As discussed earlier, your goal is impossible to achieve without at least a little bit of scripting.
You don't have to write anything "from scratch", though. The snippets generously provided to you under :help setting-tabline and :help setting-guitablabel cover a lot of ground so you only have to copy them to your vimrc and modify them to suit your needs.

VIM Delete Standard Scripting Word Groups

How would you use VIM to delete a word group, which includes white space characters, but is a standard grouping you would want to access when scripting? Specifically, when you have your cursor over some part of the following text, how would delete help="initialize, lines, h2, derivs, tt, history", from below. Maybe one would need to create specific mappings. But on the other hand, it seems pretty natural to want to access text like this if you are using VIM to edit scripting programs.
parser = argparse.ArgumentParser()
parser.add_argument("task", help="initialize, lines, h2, derivs, tt, history", default='yes')
Vim has a variety of text objects built-in, e.g. da" deletes quoted text (including the quotes; di" keeps the quotes). See :help text-objects for more information.
There are some plugins, e.g. textobj-user - Support for user-defined text objects and my own CountJump plugin that make it easy to define your own, "special" text objects. Also, you'll find many such text objects on vim.org. Based on your example, argtextobj.vim - Text-object like motion for arguments may be exactly what you need here.
If you are inside the " you want to delete, I would use:
di"diW
If you were above help=, I would use something like:
d/defEnter
to remove everything until you encounter default, followed by a few x, and left-wise motion, to remove the remaining characters.
I don't really think a new mapping is needed, but your experience may vary.
What makes sense from Vim's perspective and according to its design goals is to provide small and generic elements and a few rules to combine them in order to achieve higher level tasks. It does quite a good job, I'd say, with its numerous text-objects and motions but we always have to repeat domain-specific tasks and that's exactly where Vim's extensibility comes into play. It is where users and plugin authors fill the gap with custom mappings/object/functions and… plugins.
It is fairly easy, for example, to record a macro and map it for later reuse. Or create a quick and dirty custom text-object…
The following snippet should work with your sample.
xnoremap aa /\v["'][,)]/e<CR>o?\v\s+\w+\=<CR>
onoremap aa :normal vaa<CR>
With it, you can do daa, caa, yaa and vaa from anywhere within that argument.
Obviously, this solution is extremely specific and making it more generic would most certainly involve a bit more thought but there are already relatively smart solutions floating around, as in Ingo's answer.

Plugin name for showing indentation guide in gVim

Anybody, any idea??
which plugin is showing the indentation guide in the image below. Downloaded from http://leetless.de/images/vim/pyte.png
.png
Thanks
A similar effect could be achieved with:
set list
set listchars+=tab:\│┈
Maybe with another filler character.
See :help 'list' and :help 'listchars'.
But…
As I was writing that answer it appeared to me that the answer was probably in the colorscheme's author's ~/.vimrc.
I think that you should really work on sharpening your deduction skills. It takes about 30 seconds to 1 minute to find that information by yourself:
Go to the site where you get that pic from: http://leetless.de
Look around for something Vim-related. The navigation is generally the first place to go and what do you find? "Vim themes" at http://leetless.de/vim.html
That image illustrates the first "theme" featured, that's a good sign. But let's read the introduction text (emphasis mine, typos his):
If you are curious what some other things are (like the indetation markers) and how they work, take a look at my .vimrc. Note that the encoding is broken with that file so the "set lcs" part is probably not copy-pasteable, follow the instructions in the comments above that line in order to find out how you can make your own unicode-lcs.
Wow! It looks like you are getting closer to the truth. Beware of the Cigarette Man!
Follow the link and do a search for lcs.
Done.
Not sure what you mean by "indentation guide", but I don't think any plugin is involved.
The first thing I see that you might be referring to is the characters at the beginning of lines indicating where there are tabs. That can be done by doing setting the listchars option to an appropriate value, turning the list option on, and selecting a color for the SpecialKey highlight group.
The other thing you might be referring to is highlighting of the column that currently contains the cursor. That can be done by turning on the cursorcolumn option. The color used for that can be set with the CursorColumn highlight group.
There's also a plugin, vim-indent-guides
Here's a nice plugin for vim: https://github.com/Yggdroot/indentLine

Book translation data format

I'm thinking of translating a book from English to my native language. I can translate just fine, and I'm happy with vim as a text editor. My problem is that I'd like to somehow preserve the semantics, i.e. which parts of my translation correspond to the original.
I could basically create a simple XML-based markup language, that'd look something like
<book>
<chapter>
<paragraph>
<sentence>
<original>This is an example sentence.</original>
<translation lang="fi">Tämä on esimerkkilause.</translation>
</sentence>
</paragraph>
</chapter>
</book>
Now, that would probably have its benefits but I don't think editing that would be very fun.
Another possibility that I can think of would be to keep the original and translation in separate files. If I add a newline after each translation chunk and keep line numbering consistent, editing would be easy and I'd be able to programmatically match the original and translation.
original.txt:
This is an example sentence.
In this format editing is easy.
translation-fi.txt:
Tämä on esimerkkilause.
Tässä muodossa muokkaaminen on helppoa.
However, this doesn't seem very robust. It would be easy to mess up. Probably someone has better ideas. Thus the question:
What would be the best data format for making a book translation with a text editor?
EDIT: added tag vim, since I'd prefer to do this with vim and believe that some vim guru might have ideas.
EDIT2: started a bounty on this. I'm currently leaning to the second idea I describe, but I hope to get something about as easy to edit (and quite easy to implement) but more robust.
One thought: if you keep each translatable chunk (one or more sentences) in its own line, vim's option scrollbind, cursorbind and a simple vertical split would help you keeping the chunks "synchronized". It looks very much like to what vimdiff does by default. The files should then have the same amount of lines and you don't even need to switch windows!
But, this isn't quite perfect because wrapped lines tend to mess up a little bit. If your translation wraps over two or three more virtual lines than the original text, the visual correlation fades as the lines aren't one-on-one anymore. I couldn't find a solution or a script for fixing that behavior.
Other suggestion I would propose is to interlace the translation into the original. This approaches the diff method of Benoit's suggestion. After the original is split up into chunks (one chunk per line), I would prepend a >> or similar on every line. A translation of one chunk would begin by o. The file would look like this:
>> This is an example sentence.
Tämä on esimerkkilause.
>> In this format editing is easy.
Tässä muodossa muokkaaminen on helppoa.
And I would enhance the readability by doing a :match Comment /^>>.*$/ or similar, whatever looks nice with your colorscheme. Probably it would be worthwhile to write a :syn region that disables spell checking for the original text. Finally, as a detail, I'd bind <C-j> to do 2j and <C-k> to 2k to allow easy jumping between the parts that matter.
Pros for this latter approach also include that you could wrap things in 80 columns if you feel like I do :) It would still be trivial to write <C-j/k> to jump between translations.
Cons: buffer-completion suffers as now it completes both original and translated words. English words don't hopefully occur in the translations that often! :) But this is as robust as it gets. A simple grep will peel the original text off after you are done.
Why not use a simplified diff format?
it is linewise which is suitable for whole sentences.
The first character is significant (space, special, + or -)
It will be quite compact
Maybe you needn't those ## parts
Vim will support it and color the English sentence and the Finnish sentence in distinct colors.
Assuming you want to keep the 1 - 1 relationship between the original text and the translated text, a database table makes the most sense.
You'd have one table with the following columns:
id - Integer - Autonum
original_text - Text - Not null
translated_text - Text - Nullable
You'd need a process to load the original text, and a process to show you one line of the original text and allow you to type the translated text. Perhaps the second process could show you 5 lines (2 before, the line you want to translate, and 2 after) to give you context.

Does Vim have an auto-comment feature based on the file's syntax?

I'm not sure this is possible, but I'm interesting in making this happen.
Ideally, I would like to map this feature to SHIFT+CTRL+3.
I'm looking for a way to have Vim enter a comment (single line) which corresponds to the syntax of the file I'm editing. If there are multiple single-line comment styles, Vim could either automatically pick one, or give me the choice. If the single-line comment has two parts (e.g. /* and */), then pressing SHIFT+CTRL+3 the first time will start the comment, and the second time will close the comment.
Examples:
Python: #
JavaScript: //
C, C++: /* with */ or //
I know there are scripts which will insert comments for you, but I haven't seen any that will do this based on the syntax of the file.
I highly recommend NERD Commenter.
Sort of! I don't believe vim will do this out of the box, but you can install plugins that will do fairly intelligent commenting (using movement keys, visual line highlighting, etc) that are specific to the filetype being edited. You can get these plugins off of vim.org, and you should be able to make your own key mappings in your .vimrc file if you don't like the ones they come with.
tComment is pretty well regarded, and has worked for me.
I've heard that EnhCommentify might be better, but I haven't used it myself.
Seems like a similar question to this:
How to comment in vim while respecting the indent?
Use the nerd commenter plugin:
http://www.vim.org/scripts/script.php?script_id=1218
See: this script which provides a function to commented a highlighted area in visual mode.
You want to start a comment in insert mode so your function would look more like:
fun CommentLines()
exe ":s#^#".g:Comment."#g"
endfun
Not quite what you're looking for, but efficient, and I suppose you know which comment to use.
(all this in command mode)
Put your cursor to the first line you want to comment. We willl then set a marker called a (valid names are a-z, single character) by typing
ma
put the cursor to the last line, then set a marker called b by typing
mb
Then comment the whole block (by searching for a newline and inserting the comment character (note the use of "#" as search delimiter because otherwise wee have to escape the "/")
:'a,'bs#^#//#
or for Python:
:'a,'bs/^/#/
To uncomment:
:'a,'bs#^//##
As we do line comments, it doesn't matter if we have other comments already in the file, they will be preserved.

Resources