Unicode printing in vim - vim

I am working with text files that contain a lot of unicode characters (≼, ⊓, ⊔, ...). Vim displays them fine, but when I print they are replaced by a generic character. Gedit prints them without problem, but it's a bit of a pain to launch another editor just to print.
Is there a way to get vim (on Linux/Gnome) to print properly? I tried using vim-gnome, in hope that it would use the same infrastructure as gedit, but it does not.

Vim is only able to use 8-bit encoding for printing. If there is encoding that includes all those characters all you need is to use
set printencoding={encoding}
If there is not then you can’t print it from vim directly. You can use :TOhtml command suggested by #DaoWen, do
:TOhtml
:w /tmp/print.html
:!command-that-makes-browser-print-a-file(I-do-not-know-one) /tmp/print.html
:!rm /tmp/print.html
. You can also use my formatvim plugin to print this to pdf through latex (don’t forget to file bug reports: latex-xcolor output is untested):
:Format format latex-xcolor to /tmp/print.tex
:!pdflatex /tmp/print.tex && lp /tmp/print.pdf && rm /tmp/print.*
(you can use html output as well, but that will not make me know a command to print it). Of course, you can map these to a single key.

Try using the :TOhtml command to convert your document to output your buffer in HTML format. You should be able to print the resulting file from your browser.

Related

vim/nvim line spacing when printing

I'm trying to use nvim for everything, including writing, and all is great but the text is too crammed when I print it out.
Is it possible to adjust the line spacing when printing with :hardcopy?
My idea of a kludge fix would be to insert a second newline character for every carriage return, including those automatically inserted by line wrapping. Is this possible?
One of the least frustrating ways to do it would be to use a tool like pandoc to convert the text (for example Markdown) to whatever format you need — preferably PDF. However, pandoc uses LaTeX to create the resulting PDF, so regarding the styling you would have to tinker with supplying it a template or other options.
You can do it even easier with (for example) a Node.js tool called mdpdf. After installation, just run
mdpdf file.md --style styles.css
to supply it with a CSS stylesheet in which you can modify the resulting text output with every feature CSS permits. Using larger line spacing would be something like this:
body { line-spacing: 150%; }
This results in a 1.5x line spacing for everything in the document.
Of course, you can also set up a custom Vim command to automate this for you, putting something like the following into your .vimrc:
command MdToPDF !mdpdf %:t --style /full/path/to/styles.css
Calling :MdToPDF in Vim will then run that command for you.
Finally, if you're happy with the output, just print the PDF.

(VIM) Is vimgrep capable of searching unicode string

Is vimgrep capable of searching unicode strings?
For example:
a.txt contains wide string "hello", vimgrep hello *.txt found nothing, and of course it's in the right path.
"Unicode" is a bit misleading in this case. What you have is not at all typical of text "encoded in accordance with any of the method provided by the Unicode standard". It's a bunch of normal characters with normal code points separated with NULL characters with code point 0000 or 00. Some Java programs do output that kind of garbage.
So, if your search pattern is hello, Vim and :vim are perfectly capable of searching for and finding hello (without NULLs) but they won't ever find hello (with NULLs).
Searching for h^#e^#l^#l^#o (^# is <C-v><C-#>), on the other hand, will find hello (with NULLs) but not hello (without NULLs).
Anyway, converting that file/buffer or making sure you don't end up with such a garbage are much better long-term solutions.
If Vim can detect the encoding of the file, then yes, Vim can grep the file. :vimgrep works by first reading in the file as normal (even including autocmds) into a hidden buffer, and then searching the buffer.
It looks like your file is little-endian UTF-16, without a byte-order mark (BOM). Vim can detect this, but won't by default.
First, make sure your Vim is running with internal support for unicode. To do that, :set encoding=utf-8 at the top of your .vimrc. Next, Vim needs to be able to detect this file's encoding. The 'fileencodings' option controls this.
By default, when you set 'encoding' to utf-8, Vim's 'fileencodings' option contains "ucs-bom" which will detect UTF-16, but ONLY if a BOM is present. To also detect it when no BOM is present, you need to add your desired encoding to 'fileencodings'. It needs to come before any of the 8-bit encodings but after ucs-bom. Try doing this at the top of your .vimrc and restart Vim to use:
set encoding=utf-8
set fileencodings=ucs-bom,utf-16le,utf-8,default,latin1
Now loading files with the desired encoding should work just fine for editing, and therefore also for vimgrep.

How can I use vim in a pipeline to colorize text?

I'd like to have a command I can insert into a command pipeline that adds color escapes to its input according to vim's syntax highlighting capabilities.
For example:
cat somefile.js | vim - <???> | less
The resulting text would be that of somefile.js, but colorized according to how the current vim configuration would do it in-editor.
It occurs to me that this must be possible. I agree that the example up there isn't what a sane man might call exactly useful, but that doesn't mean the idea never is.
I think your idea has one basic flaw: that nobody ever thought about allowing such a thing.
Clearly vim is capable of doing syntax highlighting. But I'll bet you an ice cream cone that if you can manage to get vim to stream text through and process it, that you won't like the results.
Consider what happens when you pipe text through more (or less if you prefer). When it goes to the terminal, these programs display one screenful and wait for you to hit the space bar. But if you redirect stdout to some other place than the terminal, these programs notice this and simply copy their input to their output unchanged.
If vim doesn't notice that you are piping text through, it is likely to send cursor-movement commands that you probably don't want in your output. If vim does notice, it is likely to just pass the text, and not syntax-color it. Only if vim does do the syntax-coloring but does not inject cursor-movement stuff will your idea work.
You could try it. Here's an answer that discusses piping stuff through vim:
Execute a command within Vim from the command line
But I say why not pipe your text through a program that was designed and intended to have text piped through it? Pygments can colorize every major programming language and markup format.
http://pygments.org/
The major advantage I see for your idea: you can customize the way vim does syntax coloring, get it the way you want it, and then also use vim to process your text. But it's probably not that hard to customize Pygments, and it might even be satisfactory out of the box, in which case it would definitely be the easiest way to go. And Pygments not only has ANSI sequence output, it also has HTML output, RTF, LaTeX, etc. So if you get Pygments working the way you want it to, it should be able to output whatever output format you need; vim will only have the ANSI sequence one.
There's a Perl module called Text::VimColor that I've heard will do kinda what you're looking for.
http://search.cpan.org/dist/Text-VimColor/
But let me ask this: Why do want it to go through less? Why not use vim as a perfectly good file viewer? view - will read from standard input in read-only mode.
https://gist.github.com/echristopherson/4090959
Via https://superuser.com/a/554531/7198.
Tried on /etc/passwd and it works surprisingly well!
This might be what you're after
cat filename.sh | vim - -c 'syntax on; syn=bash'
This is ugly, but you could alias this:
alias vim.sh="vim -c 'syntax on; syn=bash'"
Then use like this:
cat filename.sh | vim.sh -
Use vimcat !
wget -O /usr/local/bin/vimcat "https://www.vim.org/scripts/download_script.php?src_id=23422"
chmod 755 /usr/local/bin/vimcat
vimcat /etc/passwd
See also: https://www.vim.org/scripts/script.php?script_id=4325

^# symbol in vim

The following symbol shows up when i view my file in vim.
---<snip>----
^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#fstalone
---<snip>-----
The file that I create is by redirecting stdout and stderr of my utility, like this: #./my_util > util.log 2>&1. This file tend to grow quite huge ( ~4 MB )
What is this symbol?
How to get rid of it?
That is the null character, in a format (which Vim uses a lot, as you've probably noticed) called caret notation. Basically, somehow you're getting bytes full of zeros into your file.
Since we don't know what your utility is doing, if it's the culprit, you'll need to show us some code if you want us to help. Otherwise, if you just want to remove the characters from your file, use a substitution:
%s/<Ctrl-V><Ctrl-J>//g
Ctrl-V marks the beginning of an escape sequence. After pressing Ctrl-J as well, you should see ^# appear in your command. Thus, as you guessed, Ctrl-V, Ctrl-J is one escape sequence for the null character.
None of the above worked for me. I had a file with '^#' at the end of some lines that I wanted to replace. I managed the substitute it by searching for '[\x0]' using:
%s/[\x0]//g
I hope it saves someone an hour of their life.
There's an explanation here that I will go back to read when I'm not so busy:
A discussion with a better explanation
^# shows up when you try to open a non text file in vim. For example if you open a exe file or an image file ^# is shown which is a non-readable character. Try opening the file in some other editor and see the result

Using vim+LaTeX with Scandinavian characters

I want to create a lab write-up with LaTeX in Ubuntu, however my text includes Scandinavian characters and at present I have to type them in using /"a and "/o etc. Is it possible to get the latex-compiler to read these special characters when they are typed in as is? Additionally, I would like vim to "read" Finnish: Now when I open a .tex-document containing Scandinavian characters, they are not displayed at all in vim. How can I correct this?
For latex, use the inputenc option:
\usepackage[utf8]{inputenc}
Instead of utf8, you may use whatever else fits you, like latin1, as well.
Now the trick is to make your terminal run the same character encoding. It seems that it runs a character/input encoding that doesn't fit your input right now.
For this, refer to the "Locale" settings of your distribution. You can always check the locale settings in the terminal by issueing locale. These days, UTF8 locales are preferred as they work with every character imaginable. If your terminal's environment is set up correctly, vim should happily work with all your special characters without mourning.
To find out in which encoding Vim thinks the document is, try:
:set enc
To set the encoding to UTF-8, try:
:set enc=utf8
I can't help with vim, but for LaTeX I recommend you check out XeTeX, which is an extension of TeX that is designed to support Unicode input. XeTeX is now part of Texlive, so if you have TeX installed chances are you already have it.
I use the UCS unicode support: http://iamleeg.blogspot.com/2007/10/nice-looking-latex-unicode.html

Resources