Questions tagged [whitespace]

Question about how Vim handles non printable characters.

Filter by
Sorted by
Tagged with
162
votes
5answers
115k 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 ...
142
votes
8answers
155k views

Displaying 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?
0
votes
1answer
15 views

How to delete and replace a block of text with spaces?

What I want to do I want to select a block of text in Vim, delete it, and replace the selected block with spaces. Example Suppose I have the following text: Lorem ipsum dolor sit amet, ...
1
vote
2answers
57 views

Function to remove all trailing whitespace

I have the following function to remove every trailing white space from the document, without changing my cursor position function! StripTrailingWhitespaces() let l = line('.') let c = col('.') ...
27
votes
2answers
32k 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 ...
8
votes
3answers
766 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 "...
81
votes
5answers
17k 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 ...
19
votes
3answers
1k 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 ...
2
votes
1answer
48 views

What option controls how much space is added after a new line?

When I set tw vim will auto wrap lines at tw. When breaking that line vim usually places some spaces to keep the indentation of the previous line. Example foo bar<suppose that a wrap happens ...
2
votes
1answer
30 views

Leading blank line with execute function()

When I run :echo execute('echo "oh"') I get a leading blank line before the "oh" message. This happens with when I use no vimrc as well. Why is this?
1
vote
1answer
89 views

Disable automatic whitespace marking in red

I am experiencing a behavior as depicted in the picture below: vim marks the redundant spacing in red. I would like to completely remove this behavior. At the moment my .vimrc file reads as: set ...
2
votes
2answers
238 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 ...
2
votes
1answer
41 views

Listchar for Preceding Indentation Space

At the moment, I'm trying to highlight the spaces/tabs used for indentation and the end-of-line with special characters. Although I prefer tabs over spaces I also need the spaces highlighted in order ...
2
votes
2answers
773 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
1answer
93 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
3answers
387 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 ...
29
votes
1answer
53k 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, insert ...
7
votes
2answers
4k 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
4answers
2k 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 ...
12
votes
3answers
16k 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?
1
vote
2answers
102 views

Delete whitespace before and after word

I have some text with two dashes: This is some text -- with two dashes. I want to delete the space before and after the dashes: This is some text--with two dashes. Is there a way this can be done ...
3
votes
2answers
707 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, ...
0
votes
2answers
93 views

Replace tabs with appropriate number of spaces

I have this texts drwxr-xr-x enan enan Jan 15 16:10 Desktop/ drwxr-xr-x enan enan Jan 16 20:09 Documents/ drwxr-xr-x enan enan Jan 20 19:52 Downloads/ drwxr-xr-x enan ...
0
votes
1answer
49 views

Expandtab converts spaces to newline character?

When I enabled expandtab in vim by putting these in .vimrc: set tw=4 set expandtab Vim comverted every space typed by me to a newline character. Like with the above settings whenever I type a new ...
2
votes
1answer
24 views

On the length of tabs after an initial character

Length of tabs after initial char Start gvim with the --clean option, to get a clean slate, and then do: :set noet ts=5 sts=3 sw=2 Then enter the following 1234567890 and on the next line, press ...
3
votes
4answers
2k 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 ...
1
vote
1answer
98 views

Preventing the join() command from adding extra space

I am trying to use the join command to join multiple strings. The join command seems to add one character of space in between each of the strings. But I don't want any space added. As an example, I'm ...
1
vote
1answer
104 views

Tab multiple lines [duplicate]

I have my tab set to 4 spaces with :set tabstop=4. Now I want to highlight multiple lines and tab them, or indent all them 4 spaces. If I highlight the lines and do >, it seems to indent them more ...
4
votes
3answers
2k 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 ...
2
votes
1answer
536 views

Disable vim-better-whitespace highlighting for nvim-ipy buffer

I am using the vim-better-whitespace plugin to highlight trailing whitespaces. For some buffers, I don't want this behavior and vim-better-whitespace usually provides an option to list the filetypes ...
0
votes
1answer
240 views

Removing trailing spaces with the substitute command changes the cursor position

From http://vim.wikia.com/wiki/Remove_unwanted_spaces function TrimWhiteSpace() %s/\s\+$//e endfunction autocmd FileWritePre * call TrimWhiteSpace() autocmd FileAppendPre * call TrimWhiteSpace() ...
4
votes
1answer
2k 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 ...
2
votes
1answer
166 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 ...
2
votes
2answers
723 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
1answer
406 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 ...
4
votes
2answers
265 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 ...
6
votes
1answer
739 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 ...
2
votes
0answers
283 views

Custom highlights not working in visual splits

I'm a tabs kind of guy, so I highlight leading and trailing white spaces with the following commands in my .vimrc: highlight whiteSpace_trailing ctermbg=red guibg=red highlight whiteSpace_leading ...
13
votes
4answers
2k 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 ...
0
votes
1answer
63 views

How to operate on all of a word without its surrounding space?

In vim you can do daw to delete the entire word, but it also removes the space after the word, (caw too). Is there a way to operate on the entire word without effecting the space around it? For ...
1
vote
1answer
46 views

How to prevent indent, unindent (>>, <<) from re-aligning text?

Is it possible to make indenting/unindenting not re-align text, eg: /* * Some Text */ Indents to: /* * Some Text */ I would prefer/expect: /* * Some Text */ I saw ...
18
votes
3answers
1k 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) { &...
3
votes
1answer
126 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 ...
2
votes
1answer
310 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 ...
4
votes
1answer
9k 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 ...
6
votes
2answers
4k 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
4answers
3k 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 ...
2
votes
0answers
2k 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 ...
5
votes
3answers
1k 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(.)) ...
4
votes
2answers
5k 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....