vim-airline: what is "! trailing[1]" - vim

At the right of my vim-airline display, I have ! trailing[1].
I'm assuming this means trailing whitespace of some sort, but how do I read what vim-airline is telling me and what am I supposed to do?

That means you have a trailing whitespace on the first line ([1]).
You can add to your .vimrc the following:
set list " Display unprintable characters f12 - switches
set listchars=tab:•\ ,trail:•,extends:»,precedes:« " Unprintable chars mapping
That'll display whitespace chars. You can toggle it with :set invlist.

Airline is telling you that on line 1 you have trailing whitespace, which is usually something you want to get rid of.
So go to the line and delete it (1G$gelD).
It’s a good feature, but you can turn it on/off with:
:AirlineToggleWhitespace
More info on trailing whitespace here.

To turn off the trailing whitespace check at startup, add to your vimrc:
let g:airline#extensions#whitespace#enabled = 0

Related

Delete trailing whitespace, alternative to substitute command

To delete trailing whitespace on a line or across a file I can do:
:[range]s/\s\+$//
However, I was wondering for a single line in normal mode if there's an easier approach, for example if the line is:
Hi, I am a line |
And my cursor is past the e, is there a more generic command than doing dTe in normal mode? The next best I could find was dg_, but that goes one too far. And then, one more option might be gElD.
Generic way without :substitute
Use diw to delete multiple whitespace (including tabs) while the cursor is on one of the whitespaces (like in the example).
This works for any whitespace sequence: trailing, leading, etc.
Use dw to delete from the cursor to the end of the sequence. This is useful when you want to re-indent a line or fix alignment.
As this is independent of the first/last character of the previous/next word this is the most generic way.
Using a mapping with :substitute
Use nmap <leader>tr :%s/\s\+$// to remove all trailing whitespace in the whole buffer by pressing \tr (assuming the original mapleader)1.
Replace tr with what ever suits you best.
Omit the the % in the mapping to make it only work on the current line.
--
As a side note: Use set list listchars=trail:· to show trailing whitespace (replace · with any character you like).
--
1 Removing all whitespace may not be what you want, especially when using a version control system and the file contains whitespace not added by yourself.

How to set display the indentation symbol when wrapping a new line in Vim,when input it will disappear

Does anyone knows how to set this? Vim will display the indentation symbol when wrapping a new line, When input it will disappear.
These options will make sure trailing spaces are displayed as dots (or whichever other character you set instead). As you say, the dots disappear once another character is entered, as the spaces are then not trailing any more. Look through other listchars options as well, if you want to see e.g. tabs or other cool stuff.
set listchars=trail:·
set list
(See :help 'listchars', :help 'list')

vim: replace n strings with another strings

After reading Vim regex replace with n characters, I've known how to replace tabs by spaces:
:%s/^\v(\t)*/\=repeat(repeat(' ',4),strlen(submatch(0)))/g
The command above allows me to replace n tabs at the beginning of each line with n four-spaces.
Now I want to inverse it: replace n four-spaces with n tabs at the beginning of each line, I think the command should be :%s/^\v( )*/\=repeat("\t",strlen(submatch(0)))/g, but it doesn't work: if there is one four-space, it will be replaced by four tabs (but I want to make it only one tab) after executing the command.
Besides, is it possible to get the length of tab of vim so that I can make the command as below?
:%s/^\v(\t)*/\=repeat(repeat(' ',getSizeOfTab()),strlen(submatch(0)))/g
You can get the value of an option in Vimscript by prepending &. So, the size of tab is &tabstop, or &ts. There's also &softtabstop (&sts), pick which one you actually care about.
Whereas you needed to multiply the number of spaces with size of tab, you need to divide the number of tabs. Then there's the remainder to take care of. So, first set your tabstop:
:set ts=4
Then you can convert from tabs to spaces and from spaces to tabs like this:
:%s/^\v(\t)*/\=repeat(repeat(' ',&ts),strlen(submatch(0)))/g
:%s#^\v( )*#\=repeat("\t",strlen(submatch(0))/&ts).repeat(' ',strlen(submatch(0))%&ts)#g
(changed the separator from / to # because I needed / for division :P )
However... it seems you're reinventing the wheel here. :help :retab! and :help 'expandtab'. First set tabstop as above, then:
:set et | ret!
:set noet | ret!
The first one will change tabs to spaces; the second one, spaces to tabs, according to tabstop.

#vim: set only sort of working

I have .zsh-theme files (from oh-my-zsh), but they are not syntax highlighted. I was able to get this done pretty easily with
autocmd BufEnter *.zsh-theme set filetype=sh
However, before I did that I tried adding a vim meta comment for a specific file
#vim: set filetype=sh
The addition of the # apparently makes vim detect the file as a conf file, but it seems that this command is ignored (i.e. it is not detected as a sh file in spite of the comment). Is there any reason this may be happening?
Get rid of the word set (and add a space after the comment symbol)
# vim: filetype=sh
This fits with the first form of modelines
There are two forms of modelines. The first form:
[text]{white}{vi:|vim:|ex:}[white]{options}
[text] any text or empty
{white} at least one blank character (<Space> or <Tab>)
{vi:|vim:|ex:} the string "vi:", "vim:" or "ex:"
[white] optional white space
{options} a list of option settings, separated with white space
or ':', where each part between ':' is the argument
for a ":set" command (can be empty)
The syntax for this type of modeline is:
[text]{white}{vi:|vim:|ex:}[white]se[t] {options}:[text]
That is, try adding a space before vim:, and a trailing colon:
# vim: set filetype=sh:
You can find everything about modelines in
:h modeline
:h 'modeline'

Change wrap width in a text file using Vim

I want to format srt subtitle text files to avoid wrapping problems on my media player.
I need to set a line wrap width to a number of characters e.g. 43
I can do this with Editplus, its a built in function and works well. The reason I want to do it in Vim, firstly Editplus is only available on the PC and the secondly Vim is badass.
I have found the following solution on the net..
:set tw=43
gggqG
It does work, but not exactly how I want it.
E.g.
I have this text:
557
00:47:39,487 --> 00:47:42,453
I will have to complete some procedures,
and I asked you to check out what they are for me
after I format it, I get:
557 00:47:39,487 --> 00:47:42,453 I will
have to complete some procedures, and I
asked you to check out what they are for
me
It seems to ignore line breaks/CRs. As you can see the "I will" has been added to the first line.
How do I get it to not ignore line breaks?
EDIT: apoligies about the formatting, first time using stackoverflow!
You could use the whitespace option of formatoptions and make the lines you want to wrap end in whitespace.
:set tw=43
:set fo+=w
:g/^\a/s/$/ /
gggqG
The third line adds a space on the end of any line starting with a letter and fo+=w stops gq from joining lines that don't end in spaces.
See:
:help fo-table
:help 'formatoptions'
:help gq
:help :g
Edit in response to comments
:g/^\a/s/$/ /
This translates to:
:g/ " Search the file
^\a " For lines starting (^) with an alphabetic character (\a - equivalent to [A-Za-z])
/ " Then on each line that the regexp matches (i.e. each line starting with an alphabetic character)
s/ " Substitute...
$ " The end of line (zero-width match at the end of the line)
/ / " With a space (slashes are delimiters)
The global (:g) command will only operate on the current file, but the textwidth and formatoptions lines will last for the whole session. If you want those options to only be used on the current buffer, use :setlocal instead:
:setlocal tw=43
:setlocal fo+=w
:help :setlocal
If the only lines you want to wrap are the ones that start with text (ignore the ones that start with numbers) you can use the following.
:g/^[a-zA-Z]/normal gqq
This will run p00ya's command on each text line in the file.
With the following text (i.e. only 3 lines):
557
00:47:39,487 --> 00:47:42,453
I will have to complete some procedures, and I asked you to check out what they are for me
Use gqq (format current line) on the 3rd line after setting tw. Formatting by paragraph will not work since vim will treat all 3 lines as part of the same paragraph (paragraphs only end with a blank line).
While not exactly robust (or elegant), if you can assume that the subtitle lines do not begin with numbers, you can configure Vim to treat them as comments.
:set comments=:0,:1,:2,:3,:4,:5,:6,:7,:8,:9
gggqG
Lines starting with 0-9 will be treated as comments and won't be merged together with the text.
I can't see exactly what the issue is due to the formatting of your post, but you might want to checkout the 'formatoptions' setting in vim's help, specifically :he fo-table which contains a bunch of customization flags.
This link might be useful.

Resources