开发者

Vim functions for cleaning up hidden characters

开发者 https://www.devze.com 2023-02-12 01:03 出处:网络
Currently in my .vimrc file I have a function which clears all trailing white spaces on save, while retaining my mouse position.

Currently in my .vimrc file I have a function which clears all trailing white spaces on save, while retaining my mouse position.

fun! <SID>StripTrailingWhitespaces()
  let l = line(".")
  let c = col(".")
  %s/\s\+$//e
  call cursor(l, c)
endfun

autocmd BufWritePre *.sql,*.php :call <SID>StripTrailingWhitespaces()

This works great. But I would like to add a few more things to it like:

* Remove carriage returns

* Fix indent SP followed by a TAB开发者_如何转开发

I tried adding

%s/^M//e

to my StripTailingWhitespaces() function, but when I save now vim tells me

Press ENTER or type command to continue

So I think I did something wrong or am forgetting something. Any help figuring this out? Thanks

UPDATE: Still working on this problem. I've tried adding a <CR> at the end of my searches in the StripTrailingWhitespaces function as well as at the end of the BufWritePre command. No luck. In fact, adding it gives me lots of "Trailing Spaces" errors. Any more suggestions?

If not one for fixing the need to press enter problem, what about a search to fix indent SP followed by a TAB?


I have tested it with

fun! S()
  let l = line(".")
  let c = col(".")
  %s/\s\+$//e
  %s/^M//e   
  call cursor(l, c)
endfun

and it worked perfectly with Vim 7.3 (Note: the ^M is entered with CTRL-V CTRL-M)

So, it looks like you don't do anything wrong, and haven't forgotten anything.

Now, that doesn't help you going further, does it?

If you have this message, try :messages, maybe this will give you a hint.

Also, :help messages reads

  Press ENTER or type command to continue

This message is given when there is something on the screen for you to read,
and the screen is about to be redrawn:
- After executing an external command (e.g., ":!ls" and "=").
- Something is displayed on the status line that is longer than the width of
  the window, or runs into the 'showcmd' or 'ruler' output.

So, this section might be worth reading (it's longer than the one I pasted).

0

精彩评论

暂无评论...
验证码 换一张
取 消