Linux – How to edit a file currently being used by vi

linuxvivim

I am new to Linux and trying out Ubuntu 11.10. I am setting up a SSH server and one of the instructions asks me to edit sshd_config file using vi.

In vi, I can't remember what happen (it's all a blur now) but the up/down keys turn into capital alphabets. After a bit, I give up, search on SO, and the solution is to use vim, not vi. So I try to exit vi (didn't' remember what I did) but I got back to the console.

Then, when I enter sudo vim /etc/ssh/sshd_config it says a swap file exist. Using ls -A only lists the sshd_config and I don't see a ssh_confid.swp file anywhere.

ps -ef | grep vi

shows this:

root      2914  2135  0 10:45 pts/0    00:00:00 sudo vi /etc/ssh/sshd_config
root      2915  2914  0 10:45 pts/0    00:00:00 vi /etc/ssh/sshd_config
user       4066  2135  0 11:28 pts/0    00:00:00 grep --color=auto vi

Then so I try to kill the processes, by using sudo kill 2135 2914 2915 and nothing happens. HOw do I get out of this mess?

THanks.

Best Answer

First off, if

sudo kill <pid>

does not kill a process after several attempts, try

sudo kill -9 <pid>.

That will force the process to end.

Next, execute a

ls -a /etc/ssh | grep ".swp"

to look for any swap files. You'll probably find a file called ".sshd_config.swp", so you'll want to delete that before trying to edit /etc/ssh/sshd_config.

sudo rm /etc/ssh/.sshd_config.swp

If you want to learn more about Vim, here are some links to Vim tutorials. However, I would recommend nano for someone who is brand new to Linux.

The Interactive Vim Tutorial

Graphical vi-vim Cheat Sheet and Tutorial