Deleting all lines that don't begin with specific text in vim
In the
vim
text editor, if I want to remove every line in a file that does not contain
the word "FALL" in capital letters, I can use
:v/FALL/d
, i.e.,
type the colon key and then
v
, which works
like the
grep command
grep -v
to select only lines in a file that don't contain a
specified text
string.
The particular text on which you wish vim to searh is preceded and followed by
the
slash
delimiter with a
d
at the end to specify you wish those lines
not containing the text, e.g., FALL in this case, to be deleted. If I want to
delete only those lines that begin with FALL, I can use
:v/^FALL/d
as the
caret specifies that the text should occur at
the beginning of lines (a dollar sign, $, would indicate I wanted to select
only lines where the text was found at the end of the line). Note:
you can also use
:g!/^FALL/d
to achieve the same end —
without the exclamation mark after the global
command
, all
lines containing FALL would be deleted, but as the exclamation mark
represents "not", all lines not containing FALL at the beginning of the line
are deleted.
[/editors/vi]
permanent link
Gvim stuck in insert mode
While editing a file with the
gVim
text editor on a Microsoft Windows 10 system, I found I was stuck in insert
mode. Hitting the
Esc key would no longer take me out of insert
mode and when I closed and reopened the editor I would be put in insert
mode as soon as the editor opened. I switched to another user account on
the system and opened gVim from that account. I could use the escape key
on that account to exit insert mode, which proved that there wasn't a problem
with the escape key on the keyboard. I was able to resolve the problem
by clicking on
Edit, selecting
Global Settings, and
then
Toogle Insert Mode.
After I selected Toogle Insert Mode, the editor was taken out
of insert mode and I was then able to enter editor commands. I could re-enter
insert mode by hitting the i key as usual. Though I could now
move in and out of insert mode with the Esc and i keys
as usual, when I closed and reopened gVim, I found it was not exiting
insert mode when I hit the Esc key, so I had to select the
Toggle Insert Mode option again from the menu. I'll have to
check the gVim Startup Settings later to see why it is not responding
to the Esc key on the one account, though there may be an issue
outside of gVim as I found that I can no longer type
URLs in
the address field of
Microsoft Edge tabs, though I could type in the search field of an
already open Wikipedia
tab in the browser.
[/editors/vi]
permanent link
Replacing a character with a newline character using vi
If you wish to replace all occurrences of a character with a newline character, which is used on Linux/Unix systems to indicate the start
of a new line, you can represent the new line with a
\r
in a vi command; think of the "r" as representing a carriage return. E.g., if
I had a long line with items on the line separated by commas, I could use the command below to remove each comma and start another line where
the comma occurred, instead.
:s/,/\r/g
[/editors/vi]
permanent link
Sorting lines in a file using the vi editor
If you need to sort all the lines in a file alphabetically with the vi editor, you can use
! sort
. E.g., you could sort
lines 10 to 20 with the following command:
:10,20 ! sort
You can sort all lines in the file with 1,$ ! sort
.
[/editors/vi]
permanent link
Vi
When I'm on a Unix or Linux system, I prefer to use the vi editor, though I also
sometimes use the pico editor as well. I also much prefer the
Vi IMproved (VIM) editor, which is a vi
clone, to Notepad on windows systems. I've lost information I was entering
in Notepad countless times on Windows systems when the system crashed or
locked up. With Vim, I have a much better chance of recovering my data.
Notepad also lacks the robust search and replace features of Vim, which
allows you to use "regular expressions" for manipulating text. However,
for someone used to only working in a Windows GUI, learning to use the
capabilities of Vim will probably take a fair amount of time and would
likely be difficult. But if you use Vi on a Unix or Linux system, it
certainly is a much more powerful editor than Notepad when you have to
use a Windows system.
I've started creating my own Vi tips
to help me remember commands that I may not use frequently, but am likely
to need again.
[/editors/vi]
permanent link