How do I edit a file currently being used by vi?

5,979

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

Share:
5,979

Related videos on Youtube

oky_sabeni
Author by

oky_sabeni

Updated on September 18, 2022

Comments

  • oky_sabeni
    oky_sabeni over 1 year

    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.

    • Tom
      Tom about 12 years
      In Ubuntu Linux, vi and vim are the same program, but one is a symbolic link to the other (and vi actually sets some old vi-style keys I think). The .swp file is a vim thing, and you can see it in /etc/ssh/ by typing ls -a, which will show hidden files. You can delete the swp file, or use an editor like pico/nano.
    • kamae
      kamae about 12 years
      In such a condition, you can remove the swp file by hitting D when you get the message.