Text showing misaligned in Vim vs Cat - vim

I've been using vim for a few weeks, and so far I've downloaded a few plugins including airline, nerdtree, and a colorscheme. When I downloaded some files with wget, I noticed that they weren't properly aligned. Text in the code files are normally supposed to be aligned in columns, and there generally shjouldn't be issues with indentation. As an example, when I try running the command cat filename.extension, I get this:
This is how the file is supposed to look. Everything is aligned neatly into columns without any issues. However, if I try editing the file in Vim, it instead looks like this:
The text for whatever reason is not aligned properly, and so far I am not sure how to resolve it...

The file appears to be using tabulations for indentation and alignment.
The de-facto standard width of a tabulation is 8 characters.
cat respects that "standard" to the letter, without the possibility of changing the tabulation width.
Vim also respects it by default but it allows the user to change the tabulation width and you tell it to use a tabulation width of 4 characters.
The file looks different because the two programs have different tabulation settings.
Therefore, if you want the file to look the same in the two programs, use the same tabulation settings. Since they can't be changed in cat, you will have to revert Vim's to their default values.
You didn't show us your vimrc so we don't know exactly what you did and we can't really tell you how to go about it.

Related

Vimdiff - How to hide all same lines in both files and show only different ones

vimdiff file1 file2
besides differences shows also same lines from both files. Is it possible to hide them? How to do it?
As Vim is a text editor (not a specialized diff viewer), the full contents of both files are kept (so you can do edits and persist them). Vim usually just "hides" multiple identical lines by folding them away; they are still present, just not visible.
You can influence how many identical lines are kept around changes (default: 6 lines above and below) via the context value of the 'diffopt' option. So, to completely fold all identical lines:
:set diffopt+=context:0
It is still showing common lines if common line is going in sequence with different. one after another
The solution suggested by Ingo Karkat worked perfectly in Linux. Thanks for sharing it. However, it's not working on Mac.

Give vimdiff some hints

I've got two c++ files that I want to diff with vimdiff. One of them has a lot more function definitions at the start, before both have a common function that I'm actually interested in. However, vimdiff seems incapable to ignore all the function defs before the common one (perhaps because of different arguments).
Is there any way I can give a hint to vimdiff that, say, line xxx in file1.cxx is equals to line yyy in file2.cxx?
I'm open for alternative solutions without vimdiff, but they must be on linux and very preferably command line, since I'm ssh-ing and any graphical interface is a bit uncomfortable.
Vim just delegates the actual work of comparing the files to the external diff utility, cp. :help diff-diffexpr. The help page also shows how a different utility can be used. Unfortunately, I'm not aware of any more "intelligent" or configurable diff tool that would help in your situation.
A workaround might be (temporarily) removing the excess functions that you're not interested in, anyway. With the BlockDiff plugin, you don't actually need to modify the files. Just select the interesting lines in both windows and execute :[range]BlockDiff on them. Only those sections will then be diffed in a separate tab page. (The plugin mentions this requires a GUI, but Vim in a terminal supports tab pages just as well.)

Line breaks in built-in documentations?

Bulit-in documentations look like this in smaller gVim windows:
while normally it should look like (capture from larger gVim window):
Is there any way to 'fix' the line breaks when viewing documentation in smaller windows?
Use the :nowrap option, which says (in part):
This option changes how text is displayed. It doesn't change the text
in the buffer, see 'textwidth' for that.
When on, lines longer than the width of the window will wrap and
displaying continues on the next line. When off lines will not wrap
and only part of long lines will be displayed. When the cursor is
moved to a part that is not shown, the screen will scroll
horizontally.
The help files are written with hard wraps at position 78; see the modeline at the bottom; it contains tw=78.
Especially with todays gigantic monitor sizes, terminals with less that the 1970s-standard 80 character width don't make sense. If your issue is with vertical splits of the help, just don't do that :-) (use the default horizontal splits).
The long-proposed, but so far not applied breakindent patch would provide a lot of what you want, though: The indent wouldn't be cluttered by the broken lines (but you'd still have a ragged right edge).

Use Vim to "colourize" files or input streams

This may be an odd question, but still. I use cat to display a file in bash (KDE Konsole),
cat foobar.rb
Now, I would like to use Vim to colourize that foobar.rb file according to what you would get when you start foobar.rb in Vim. Edit: But only for display purpose, on the terminal.
I am not sure this is possible, but I thought it would be neat if I could use Vim for that.
I really just want colourized keywords, and Vim has the perfect colour definitions.
So I thought combining this would be great.
Is this possible in Vim out of the box though?
One approach would be to use a library such as Pygments, which is a general purpose syntax highlighter. You could write a wrapper called ccat or something that would apply syntax highlighting to an input file and write to stdout.
If you want to page up and down in a highlighted file, you can use less with the -R switch, which passes control characters through to the terminal directly, preserving colours. So:
ccat file.rb | less -R
But at that point, you're pretty much at the capabilities of view.
I'm not sure if I understand your question correctly, but if you are only looking for a command that will give you a read-only view of the input file (like cat) but with coloured keywords, use view. view is an alternative way to start vim in read-only mode, so you have all syntax highlighting possibilities. From the vim man page:
view Start in read-only mode. You will be protected from writing
the files. Can also be done with the "-R" argument.
gvim gview
The GUI version. Starts a new window. Can also be done with
the "-g" argument.
evim eview
The GUI version in easy mode. Starts a new window. Can also
be done with the "-y" argument.
rvim rview rgvim rgview
Like the above, but with restrictions. It will not be possi-
ble to start shell commands, or suspend Vim. Can also be
done with the "-Z" argument.
I have always seen view on systems that have vim installed.
Closest is the less script that comes with vim:
cat myfile | vim -u /usr/share/vim/vim72/macros/less.vim -
Note the - argument to vim. You may need to change the vim72 to your version (and the whole path if you have it installed elsewhere)
Now, this isn't exactly what you want, because its behaviour is less-like, in that you have to press keys to make it scroll down or complete. However, they are briefer than usual vim. For example, space to scroll down; and q to quit (not :q).
You want a cat-like version; me too. But there doesn't seem to be one.
EDIT uh, there's also a vimpager project, that includes vimcat - exactly what you want. But it doesn't come with vim, and I haven't tried it yet.
vim.org: http://www.vim.org/scripts/script.php?script_id=1723
github: https://github.com/rkitover/vimpager

Adding vertical space between lines in vim

Summary: I'd like to view my text in vim with a small bit of extra vertical space between some lines.
I'm writing latex in vim, and have each sentence on a new line. I'd like to add a little bit of vertical space (a few pixels) between sentences to make them clearer. I don't want to add anything to the buffer, just to how I view it.
I don't want to add a whole newline, as that has meaning in latex.
Example text:
Conventional wisdom states that a compiled program should run an
order-of-magnitude faster than an interpreted program.
In our experience, however, dynamic scripting languages do not follow this rule
of thumb.
Instead, a program written in a scripting language spends most of its run-time
handling dynamic features, such as dynamic types and \code{zval}s.
This limits the potential improvement of simply removing the interpreter loop.
This is particularly important for a compiler like \phc{} which re-uses the PHP
system, as many of the code paths executed will be the same, whether the program
is interpreted or compiled.
Any ideas?
Update:
Maybe something with breakat, linebreak, showbreak and linespace, but I dont think this is enough.
linespace will put space between every line, but I'd like space only after lines ending in .
The others aren't enough without some equivalent to linespace
If you're using gvim, can't you do this command?
:set linespace=5
If you are not using gvim, only vim, you can't change the line space or see any change when specifying "linespace=5" into vimrc. My solution was to change the line space in my terminal emulator, in my case Alacritty, using in my alacritty.yml:
font:
offset:
x: 0
y: 5
You can try to make the same in your terminal emulator, BUT this will change the line space in all the terminal.

Resources