3 ms·
Not sure if most people don't know it, but this is useful (in Windows vim): In Unix vi(m), you use Ctrl-V to quote a special character to actually insert it in
by vram22 8y ago
Not sure if most people don't know it, but this is useful (in Windows vim):
In Unix vi(m), you use Ctrl-V to quote a special character to actually insert it into the text (or into a last line mode command; see example below).
So in Unix if you want to insert a carriage return (Ctrl-M) into a file or a command, you do Ctrl-V Ctrl-M. That does not work in Windows with gVim because Ctrl-V means paste from clipboard. So you use Ctrl-Q instead of Ctrl-V, like in this example, where you want to use vim to remove all the carriage returns (CRs) from the file (say if you were going to use the file on Unix later [1]):
:%s/Ctrl-QEnter//g
where instead of typing the letters Enter, you hit the Enter key.
The %s means do the command for all lines in the file (% for all lines, s for substitute), the command is search-and-replace (old pattern with new pattern, here Ctrl-M with nothing), and the g(lobal) at the end means do it for all occurrences in the line (otherwise it does it only for the first occurrence on the line).
This will show on your screen as:
:%s/^M//g (the Ctrl-Q does not show on screen)
where the ^M represents the CR or Enter.
The same command on Unix would be:
:%s/Ctrl-VEnter//g
This will show on your screen as:
:%s/^M//g (the Ctrl-V does not show on screen)
[1] Of course there are other ways to remove the CRs from the file, like a) using a dos2unix utility, a) many modern editors will do it for you (via a setting or a menu option), c) push it to Git from Windows and then pull it from Unix (with appropriate Git settings for line-ending handling), etc.
- vram22 8y agoI forgot to mention that on for both the Unix and Windows versions of that above command, you have to hit Enter at the end of it (after the g), because last line mode (or ex mode) commands require that, unlike regular vim commands which run immediately, without needing an Enter.
- dwyer 8y agoThere's a command for that. " Use Unix line feeds. :set fileformat=unix " Use Windows line feeds. :set fileformat=dos " Shortcut :set ff=unix " See the current format :set ff
- vram22 8y agoYes, I was aware of that (but thanks), and that's why I had written above: >a) many modern editors will do it for you although I tend not to use those particular vim options much. And for anyone else who does not know, many other vim set commands also have shortcuts, like: :se ai (for :set autoindent) and ts for tabstop, sw for shiftwidth, ic for ignorecase, wm for wrapmargin and many others, probably. I tend to do this in my .vimrc or .exrc as soon as I start using any new instance (on a new machine) of vi/vim: :se ai ts=4 sw=4 showmode showmatch ignorecase expandtab report=0 Also, like: >" See the current format > :set ff you can do this for any boolean setting, like readonly, ignorecase, etc.: :se readonly? :se ic? to query their values.