Questions tagged [whitespace]
Question about how Vim handles non printable characters.
86 questions
219
votes
7
answers
220k
views
What's the simplest way to strip trailing whitespace from all lines in a file?
It's pretty common when programming or opening text files to encounter files with trailing whitespace at the end of a line. vim has a way to show this by setting the trail option in the listchars ...
215
votes
9
answers
277k
views
How can I display tabs as characters?
When I work on files in python, I sometimes mistake tabs for spaces and vice versa. Is there a way I can get Vim to display a character for a tab instead of a tab?
124
votes
5
answers
33k
views
How to join lines without producing a space?
The standard J command for joining lines replaces the newline character(s) with a space. It's useful when editing 'literature' but can be troublesome if I, say, edit a hex dump by hand if I forget to ...
81
votes
3
answers
103k
views
How to indent as spaces instead of tab?
I am coding some html templates in JADE files. I want to auto indent code with spaces instead of tab. I tried shiftwidth=2, tabstop=2, but no matter what, it's still indents using tabs instead of ...
70
votes
2
answers
148k
views
Setting TAB to 2 spaces
I've looked through various answers which imply that setting the following in .vimrc would be appropriate to force tab insertions to become spaces.
filetype plugin indent on
" On pressing tab, ...
21
votes
3
answers
3k
views
How can I make Vim position the cursor at the start of a tab character instead of the end?
Open an empty buffer and insert a tab character then exit to normal mode. When you place the cursor (normal mode) on the tab it will be at the right side of the whitespace. I'm looking for a way so ...
18
votes
4
answers
3k
views
What is the rationale for \r and \n meaning different things in s command?
We all know that, when searching, \n is newline and \r is carriage return (^M), but when replacing \r is newline while \n is a null byte (^@).
What is the origin of this asymmetry? Given that this ...
18
votes
3
answers
2k
views
Tabs for indentation, spaces for alignment
I am trying to make vim auto indent with tabs, and align with whitespaces, but actually, vim indent with tabs and fill the alignment with tabs + white spaces.
Vim does it :
(each block is a tab)
{
&...
16
votes
3
answers
11k
views
How to remove Neovim trailing white space?
In Vim I used autocmd BufWritePre * :%s/\s\+$//e to delete trailing white space on save. Now, Neovim 0.7 has the new vim.api.nvim_create_autocmd.
What is the correct syntax to adapt this autocmd in ...
15
votes
4
answers
35k
views
How to split a line into multiple lines efficiently?
I've been receiving logged data in a text file that is just one long line with each value separated by a space. Is there a command or set of commands that will split one line into multiple lines?
13
votes
2
answers
8k
views
How to differentiate visually the white spaces in Vim? [duplicate]
My goal were to somehow make visible, if a whitespace is space, tab, or even newline (and, ideally, in case of newline I would be happy to see \r\n and \n differently).
I am thinking to a similar ...
10
votes
3
answers
2k
views
Shift left or right (with `noet`) without converting existing spaces to tabs
Foreword
I'm using the "indent with tabs, align with spaces" ideology for indentation. This means that each line in a file can be prefixed with an arbitrary amount of tabs and spaces (as opposed to "...
9
votes
3
answers
11k
views
Vim doesn't use the correct indentation in Python files
I have encountered something that I have never seen before in Vim. While I was tentatively modifying some of the source code for software I've been planning to work on, I noticed vim was displaying ...
9
votes
4
answers
3k
views
SpecialKey foreground color w/ cursorline set
In this Q&A, it's observed that there is a highlight group for CursorLineNr, which applies to the line number for the cursor line when set cursorline is on.
I notice that using cursorline also ...
8
votes
1
answer
1k
views
How to show trailing spaces, while leaving tabs as they are?
I like the ability to show trailing whitespaces, so I have this setting.
set list listchars=tab:\ \ ,trail:·
I never use tabs (although I don't think tabs are bad, it's just a convention), but ...
8
votes
3
answers
2k
views
From a TextChanged autocommand event, how to get the range that has been changed?
Problem
I am trying to implement "trimming trailing whitespace in changed lines only". For this purpose, I intercept TextChanged and TextChangedI autocmd events and record the current line (line(.)) ...
8
votes
1
answer
1k
views
Matching any Unicode whitespace
I'd like to match in Vim any whitespace character, not only normal space (U+0020) and tab (U+0009) with \s, and the other ASCII ones like form feed (U+000C) with [[:space:]], but any Unicode ...
8
votes
1
answer
205
views
Running cleanup commands only for edited lines possible?
Referring to clean-up tasks like (but not limited to) the ones detailed in these questions:
Re-indenting badly indented code
What's the simplest way to strip trailing whitespace from all lines in ...
7
votes
1
answer
2k
views
What does vim consider a paragragh?
Suppose I have the following piece of code through which I would like to navigate
===>Cursor is here and when I press {
SomeTest
{
line1
line2
line3
line4
...
6
votes
4
answers
5k
views
How to show illegal whitespace (such as unbreakable space) in source code?
I sometimes type (by accident) shift-space, inserting unbreakable space into code. With c++, I get this:
error: stray ‘\302’ in program
error: stray ‘\240’ in program
while in other formats (like ...
6
votes
4
answers
5k
views
How to add space around a symbol using vim?
I have following code. I want to add spaces around =. With the minimal keystrokes (May be with the help of macro). Do not want search and replace approach.
<div className="row">
<div ...
6
votes
2
answers
10k
views
delete entire line after first whitespace on every line
I have a file (around 1.6 million lines) which formatted like this:
-0.00697468 0 -1.95966e-49 101325 -3.06381
-0.00697468 0 -1.95966e-49 101325 -3.06381
-0.00697468 0 -1.95966e-49 101325 -3....
6
votes
1
answer
587
views
What does breakat&vim mean?
I'm trying to understand this SO answer. The snippet reads:
:set nolist wrap linebreak breakat&vim
6
votes
2
answers
6k
views
Toggle highlighting of trailing whitespace
I put the following lines from this page into my ~/.vimrc to highlight trailing whitespace in red:
highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/
autocmd BufWinEnter * ...
6
votes
1
answer
501
views
equalprg to use for Python
Python is whitespace dependent, and it can't be totally autoindented for that reason, but there are a lot of extra stuff that should be possible to do automatically to comply with PEP8.
Things like ...
5
votes
3
answers
4k
views
How to remove trailing white-spaces automatically while typing?
I'm often editing code where others didn't care about trailing white-spaces. I'm using the TrimWhiteSpace()-function from this answer to remove white-spaces when saving the file.
However that leaves ...
5
votes
2
answers
5k
views
Expand <TAB> into 2 spaces when and only when editing HTML
My current configuration makes vim automatically expand TAB to 4 spaces for me:
" Define tab as 4 spaces
" Taken from http://stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces
set tabstop=4 ...
5
votes
2
answers
2k
views
Stop Vim from deleting trailing whitespace [closed]
Vim automatically strips trailing whitespace from my files on save, which is extremely annoying in various contexts. How do I disable this behaviour?
This question is not a duplicate of this one, ...
5
votes
1
answer
3k
views
How does vimscript know which buffer these buffer-local variables use?
Consider the following vimscript:
function! DecideWhetherToStrip()
let l:search = @/
" Check if a file being opened has trailing spaces in the first place.
let b:dostrip = !search('\s\+$',...
5
votes
2
answers
765
views
Is there a way to strip the contents of register before pasting
There are times I know I just want to paste the content of the register inline where my cursor is, so the initial and trailing whitespace (newline, tab, etc) is not wanted.
Because selecting contents ...
5
votes
1
answer
654
views
How to center a cursor on the screen, even at the start of the file?
I like always knowing where my cursor is. I do this by having it in the center line of the screen.
However if I am working at the top of the file the cursor stops centering because there is no more ...
5
votes
3
answers
1k
views
Avoid spaces getting converted to tabs automatically during shift(>>)
I use 2 spaces as indentation. So I set shiftwidth using :set sw=2.
However when I do the shift operator(>>) on a line twice, the indentation converts the 4 spaces automatically to a tab. So to ...
4
votes
2
answers
772
views
How can I make zz not center cursor when approaching end of file?
When I press zz at the end of file, the line is displayed at the center of screen by having many empty lines
having empty lines beyond end of file is great, but this is too much. Is there a way to ...
4
votes
1
answer
11k
views
ignoring spaces in search like ignoring cases
I frequently search in my files using /, and \c is really helpful in these situations. Is there any similar way to ignore white space and line breaks? It would really be nice to be able to add ...
4
votes
1
answer
86
views
Why do some Ex commands not need a white space before a path argument?
For example, why does this work:
edit/home/foo/bar.txt
(I had to enter visual to see the changes)
I was expecting a whitespace after edit, like:
edit /home/foo/bar.txt
I couldn't find a reference in ...
3
votes
3
answers
4k
views
Moving to the end of the last word on a line
There are commands to distinguish between the beginning of a line (0) and the beginning of the first word of the line (^), basically ignoring leading whitespace. But I can only find one way to move ...
3
votes
2
answers
547
views
How do I replace single whitespaces without affecting multi-whitespace such as indentation in Vim?
I have textfiles with paths that have lost underscores between words inline in an OCR process. I want to keep the indentation and put back the underscores in the paths.
I have a path like this:
/...
3
votes
1
answer
2k
views
vim search for arbitrary combination of spaces and tabs
I'd like to construct a mapping that would replace a b with c where the gap between a and b could be zero or more spaces or zero or more tabs, but not zero of both. There have been several ...
3
votes
1
answer
353
views
Moved from vim to neovim and now <BS> deletes 4 spaces at a time, instead of 1 as I'm used to. How to fix?
I've used vim for ages and am really used to inserting four spaces by hitting tab, but then being deleting them one by one. Now that I've switched to neovim this has changed to it inserting four ...
3
votes
2
answers
1k
views
Visual indicator for a file that doesn't end in newline
Inside of my editor (almost fresh install of neovim) there is no visual way to distinguish between a file that ends in a newline and a file that doesn't.
In my command line (zsh default?) there is a ...
3
votes
1
answer
629
views
How to skip over indentation whitespace with horizontal motion?
I have a file that uses 4-space indentation. By default, when I move horizontally, Vim moves over those spaces one by one.
How can I configure Vim so that it moves 4 spaces horizontally when moving ...
3
votes
1
answer
211
views
How to use different rules for highlighting trailing whitespace inside a gitcommit diff?
In my ~/.vimrc, I have:
highlight TrailingWhitespace ctermbg=blue
autocmd BufReadPost * syn match TrailingWhitespace /\s\+\%#\@<!$/
This highlights trailing spaces, except when I am still typing ...
3
votes
0
answers
4k
views
Any way to display *all* characters?
So, I got it that Vim has no option to display all characters (including whitespace and CR and LF distinctly)
I already saw the question How to Display Hidden Characters in vim? and others on ...
2
votes
3
answers
7k
views
How can I remove additional whitespace in the middle of line?
I know how to remove trailing white spaces at the end of lines.
Does anyone know how to remove white spaces in the middle of a line?
The white space I mean is before tab and after tab characters.
For ...
2
votes
2
answers
1k
views
Removing redundant whitespaces on save/write
I want all redundant whitespaces to be removed on write/save. How can I do that?
By redundant I mean ones that exists after the last character or if there's an empty line and it contains a few ...
2
votes
2
answers
2k
views
Show leading whitespaces, but not including tabs
I am a tabs kind of guy, I do not like when my code contains leading whitespaces instead of leading tabs. I often exchange codes with colleagues and I first have to convert the leading whitespaces to ...
2
votes
1
answer
522
views
Stop vim from removing whitespace on save for diff files
I am currently trying to edit a git hunk file (a diff: .git/addp-hunk-edit.diff), and to say to git that a line containing whitespace is unchanged it needs to have (a line with a single space) as its ...
2
votes
3
answers
229
views
Remove blank lines at end of file
How can I augment/modify my trailing whitespace logic to also remove any empty or all whitespace lines at the end of a file?
My current whitespace stripping logic in my .vimrc is:
"remove ...
2
votes
1
answer
137
views
Option to prevent outer yank (ya') from yanking leading whitespace?
If i have something like
foo(
'parameter'
)
and I press ya' with the cursor somewhere inside the parameter string then the indentation space gets yanked too and I end up with 'parameter' in my * ...
2
votes
1
answer
211
views
Dangerous trailing space
In remove unwanted spaces page in vim wikia, it says:
However, this is a very dangerous autocmd to have as it will always strip trailing whitespace from every file a user saves. Sometimes, trailing ...