I have been using Vim for years at work (legacy stack), and I recently decided to copy the trim function we use there. It is:
fun! TrimWhiteSpace()
let l:save_cursor = getpos('.')
%s/\s\+%//e
call setpos('.', l:save_cursor)
endfun
command! Trim call TrimWhiteSpace()
So we just call :Trim
, and it does it's work. While it works fine at work, at home it is also deleting % signs along with the white space. Example: printf("Testing %2f", float);
will turn into printf("Testing 2f", float);
.
The main difference is at work I am on a Unix server, and at home I am on Windows 11. Any idea what is causing this behavior?
P.S. It is possible that this behavior would happen at work too, but in Perl there really is never a situation where there would be white space followed by a %. In C, C++ (what I use at home) obviously there is a lot.
Edit: Let me add my .vimrc for context. It is really bare right now.
" GENERAL -------------------------------------------------------
set nocompatible " We are not messing with being vi compatible
set backspace=indent,eol,start " Fix backspace issue
imap <S-BS> <BS>
"filetype on " Enable file type detection
"filetype plugin on " Enable plugins and load plugins for file type
"filetype indent on " Load an indent file for the detected file type
syntax on " Turn syntax highlighting on
set hlsearch " Set highlight search
set showmatch " Show matching words during a search
set showmode " Show mode
set ignorecase " Ignore capital letters during search
set smartcase " But allow for searching by capital specifically
set incsearch " Incrementally highlight matching searches
set tabstop=3 " Set tab width
"set expandtab " Allow spaces instead of tabs
" Enable autocompletion menu after pressing tab
"set wildmenu
"set wildmode=list:longest
"set wildignore=*.docx,*.jpg,*.png,*.gif,*.pyc,*.flv,*.img,*xlsx
fun! TrimWhiteSpace()
let l:save_cursor = getpos('.')
%s/\s\+%//e
call setpos('.', l:save_cursor)
endfun
command! Trim call TrimWhiteSpace()
The substitute
command
%s/\s\+%//e
matches whitespace \s
, repeated one or more times \+
, followed by a literal percent sign %
and replaces them with nothing, i.e. deleting them.
The following command, on the other hand
%s/\s\+$//e
matches whitespace \s
, repeated one or more times \+
, followed by a line ending $
and replaces them with nothing.
I think you have a typo in your expression and you actually use the second form at work. Please double check.
See :help :substitute
and :help pattern
.