Display-only character substitution in VIM - vim

Is there any way to set VIM that it substitutes characters in the displayed text without changing the underlying file?
To be specific, I would like to substitute SOH characters (displayed as ^A by default) for tab to make the FIX protocol log files more readable.
I'm partially fine with a workaround
:highlight SpecialKey guifg=#ffffff
what makes the ^A invisible on white background but the substitution for tab would be much better.
Update:
I've almost found the solution:
:syntax match SOH /^A/ conceal cchar=_
:set conceallevel=2
:hi conceal guibg=NONE
Unfortunately I haven't found the way how to escape tab to the cchar parameter (instead of the underscore).

I do face the problem while opening log files.
I use autocommands to do such substitutions and leave files without saving.
:au BufEnter *.log :%s/^M/\r/g
This autocommand will replace all ^M characters with newlines while opening a log file.
So, the text like ADM-DEVICE : logged in^MADM-DEVICE : in prompt will be shown correctly as
ADM-DEVICE : logged in
ADM-DEVICE : in prompt.
Similarly, other control characters can be replaced. One thing I would do is changing '^[' to ` for better understanding.
Log files should not be edited and saved manually. So, for that I will use another autocommand.
:au BufWrite *.log :q!
This autocommand will exit the file if I try to save it. Autocommands will be the best choice to do automatically what you wanted to do.

Related

Stop Vim from auto-inserting comments on newlines permanently? [duplicate]

While inserting a new line below a comment in vim, the result tends to insert a " at the start of the new line. It's probably a simple solution or reason why this is happening, but I am unable to find an exact solution.
If you’re editing a file of the vim filetype, Vim might by default insert the comment character (in Vimscript, this would be ") at the beginning of each new line you enter after a comment. As already mentioned, this is a result of Vim’s formatoptions setting.
To turn this behavior off in the current file, run
:set formatoptions-=ro
To turn it off by default, add this to your ~/.vimrc:
set formatoptions-=ro
To turn it off for Vimscript files, add this to your ~/.vimrc:
augroup filetype_vim
autocmd!
autocmd FileType vim setlocal formatoptions-=ro
augroup END
r and o are options which can be given to formatoptions. For the full list of possible options, run :help fo-table.
This behaviour is governed by the formatoptions variable.
Use :h formatoptions to find out more.
The following article might also be helpful: Disable automatic comment insertion.
I think this should work, regardless of your formatoptions settings.
inoremap <CR> <CR><C-U>
what command are you using to insert below?
If you use the standard "o" keystroke while in Navigation mode, it should insert a new line immediately below whatever the cursor is on, and automatically place you into Insert mode, without inserting an extra "
Similarly an uppercase "O" will insert a new line above whatever line the cursor is on, and place you in insert mode.

How to make spaces wider in vim?

I recently installed the window manager awesome, and started to use xterm. While configuring the font, I came accross this webpage: https://cjshayward.com/terminal/printer.html
that makes the really good point that you don't have to make the font bigger to get more easily readable code, but only to make the spaces between words wider.
How can I achieve this in vim?
It's not possible. Vim uses a fixed size per character (as it's running in a terminal), and therefore the only way to change this is to change the font or font size. To achieve a different character/letter spacing you'll need to either edit the font or choose another.
The only hack I can think of is replacing every space character with its double-width companion, U+3000 ideographic space:
:%s/ /\=nr2char(0x3000)/g
You could use :autocmds to undo this before, and redo after saving, but I wouldn't recommend this.
Unfortunately, the conceal feature only handles single-width characters so far. It would make for a far better workaround.
#Ingo this is what I added to my .vimrc:
"doubles space in insert mode
inoremap <space> <space><space>
if !exists("autocommands_loaded")
let autocommands_loaded = 1
"doubles spaces when reading file
au bufRead *.cpp silent %s/ / /g
"for :w
au bufWritePre *.cpp silent %s/ / /g
au bufWritePost *.cpp silent %s/ / /g
"for :wq
au quitPre *.cpp silent %s/ / /g
endif

Vim Only Highlighting ~/.vimrc

I am a new vim user and trying to turn on syntax coloring for vim on Mac OS X. I followed http://vim.wikia.com/wiki/Turn_on_syntax_coloring_in_Mac_OS_X's instructions, and my ~/.vimrc file has the following content
set term=builtin_ansi
filetype plugin indent on
syntax on
And the text above in ~/.vimrc is highlighted perfectly fine. However, whenever I use vim to edit a current text file or create a new one, I only see black and white text, and manually calling :syntax on does nothing. Any help is apprecaited!
You can use the :highlight command to list all defined groups (according to your colorscheme). A plain text file probably has no filetype and therefore no syntax assigned; you want to try this with any programming language (e.g. *.py or *.c) to see syntax highlighting in action.
Your ~/.vimrc looks fine. (Do you really need the :set term, though?!)
You should remove that useless line:
set term=builtin_ansi
Do you use the default Vim? What filetypes are you editing?

Run a command each time a file opens, or general syntax

I have a syntax rule that highlights trailing whitespace:
highlight Badspace ctermfg=red ctermbg=red
match Badspace /\s\+$/
This is in my .vimrc. It works fine, but the problem is I use splits a lot, and it seems that the match is only run on the first file you open, as well it should because the .vimrc should only run once.
Anyway, how can I get the above syntax to match any file that is opened? Is there a "general" syntax file? Is there any other way to run match each time a file opens rather than just once? I'd like to know both because I could end up using either one in the future.
The :match command applies the highlighting to a window, so you can use the WinEnter event to define an :autocmd.
:autocmd WinEnter * match Badspace /\s\+$/
Note that there are already a number of plugins for this purpose, most based on this VimTip: http://vim.wikia.com/wiki/Highlight_unwanted_spaces
They handle all that for you, and turn off the highlighting in insert mode; some can also automatically delete the whitespace. In fact, I have written a set of plugins for that, too: ShowTrailingWhitespace plugin.
You could accomplish this by using an autocmd:
highlight Badspace ctermfg=red ctermbg=red
autocmd BufEnter * match Badspace /\s\+$/
However, there's another way to accomplish your specific goal of marking trailing whitespace. Vim has a built-in feature for highlighting "special" whitespace, which includes tabs (to differentiate from spaces), trailing whitespace, and non-breaking spaces (character 160, which looks like a normal space but isn't).
See :help list and :help listchars. Here's what I use:
set list listchars=tab:>·,trail:·,nbsp:·,extends:>
listchars has the benefit of working with any file type, and marking up multiple whitespace types that are of interest. It is also a lot faster (match will be noticeably slow on giant files) and built-in already.
(Note that those are funky non-ASCII dot characters, which should work fine for you if you cut-and-paste into a UTF8-capable Vim. If they don't work for you, you can use any characters you like there, such as periods or underscores).
Here's what it looks like for me:
The correct approach to this problem is actually to use :syntax to define a custom syn-match.
Try putting this in your vimrc:
augroup BadWhitespace
au!
au Syntax * syn match customBadWhitespace /\s\+$/ containedin=ALL | hi link customBadWhitespace Error
augroup END
Edit: It should also be noted that there is built-in support for highlighting trailing whitespace with the 'list' option; see :help 'listchars' and :h hl-SpecialKey (SpecialKey is the highlight group used to highlight trailing whitespace characters when 'list' is on).
This is accomplished using autocmd. The events you're looking for are BufWinEnter and VimEnter. From the Vim manual:
BufWinEnter
After a buffer is displayed in a window. This
can be when the buffer is loaded (after
processing the modelines) or when a hidden
buffer is displayed in a window (and is no
longer hidden).
Does not happen for |:split| without
arguments, since you keep editing the same
buffer, or ":split" with a file that's already
open in a window, because it re-uses an
existing buffer. But it does happen for a
":split" with the name of the current buffer,
since it reloads that buffer.
VimEnter
After doing all the startup stuff, including
loading .vimrc files, executing the "-c cmd"
arguments, creating all windows and loading
the buffers in them.
Try putting this in your vimrc:
augroup BadWhitespace
au!
au VimEnter,BufWinEnter * match Badspace /\s\+$/
augroup END
Do :help autocmd for more info.
This is completely wrong because :match is window-local, not buffer-local. Ingo Karkat has the right idea. Unfortunately, there is no good way to avoid triggering the autocmd every time you enter the window.
More to the point, though, this is a job for a custom syntax, not match.

How to get rid of empty blocks in vim

I used textmate to work with ruby code for over one year. Recently I switched to using mvim. When I open some of the files in mvim I get empty blocks. Look at this picture to get a feel for it.
Any idea on how to get rid of them?
Thanks
Others have explained that this could either be a search highlighting spaces or tabs or (more likely) it could be highlight designed to show up mixed indentation (particularly useful in python for what it's worth). I find this very useful personally.
Anyway, there are a number of options to sort out your highlighting depending on the cause:
To clean end of line spaces (as mentioned by chaos), use something like:
:%s/ \+//
Probably the most useful one: tidy up the tabbing:
If you're using spaces for indentation:
:set expandtab
:retab
If you're using tabs:
:set noexpandtab
:retab!
If you're using tabs for indentation and spaces elsewhere:
:set expandtab
:retab
:set noexpandtab
:execute '%s#^\( \{'.&ts.'}\)\+#\=repeat("\t", len(submatch(0))/'.&ts.')#'
I have the last line mapped to a command called :RetabIndents. All of those assume that your tabstop setting is correct (it should be set with set ts=2 based on your picture). Personally, I'd also recommend keeping shiftwidth equal to tabstop, so set ts=2 sw=2.
You may also be able to get away with a simple gg=G (auto-indent the whole file). However, this won't work in some languages (in particular python as there's no way for any editor to know which lines should be indented to which level).
To switch off search-based highlighting temporarily:
:noh
Or permanently (put this in .vimrc):
:set nohlsearch
Or a quick shortcut for when you've used it and don't want it anymore:
:nnoremap <ESC> :noh<CR><ESC>
To switch off indent highlighting, you'll have to identify which highlighting group is used, which is a little complicated and is probably easiest to just read your .vimrc, but if you really want to search for it, move the cursor to one of the highlighted characters and enter (taken from here):
:echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<' . synIDattr(synID(line("."),col("."),0),"name") . "> lo<" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>
You can then clear the highlighting group by searching for the name that is reported on the command line in your .vim/.vimrc/_vimrc/vimfiles configuration and commenting out anything relevant.
For more information
:help :s
:help 'expandtab'
:help :retab
:help :execute
:help 'tabstop'
:help :noh
:help 'hlsearch'
It's looks like highlighting of redundant whitespace (see line 214 for example)
Is there anything in your .vimrc along the lines of..
highlight RedundantSpaces ctermbg=grey guibg=grey
match RedundantSpaces /\s\+$\| \+\ze\t/
..try commenting it out, and seeing if this fixes the problem
I guess the reason it's highlighting the indentation is vim is configured to expect tabs, no spaces (or vice versa) - again make sure your .vimrc is setup correctly (say, using soft-tabs)
It looks like you might have a search pattern stored that highlights spaces at the beginning of the line: /^ + and your highlight color is light gray.
To get rid of it, try searching for something else: /asdf<ENTER>.
If you want to get rid of hanging spaces on the ends of lines (they always annoy the hell out of me, honestly), this command will strip them from a given file:
:%s/ *$//
As Seth mentioned, these are spaces. Essentially, mvim is showing you the spaces that are placed in your file by coloring them grey instead of black.
Personally, I think this is a feature, you can use this highlighting scheme to identify where you may have trailing spaces, and can use it to make your code look neater.
You could probably get it to go away by adjusting the highlighting options.

Resources