Nano - File already being edited - How do I switch to the open file? - linux

I have a tendency to miss-type while trying to Ctrl-X to exit nano.
Every now and then I'll try to re-open the file and get the Nano "File xxx is being edited". How do I switch back to the open file? And also, what am I miss-typing to cause this to happen?

First check if there is a process editing the file.
$> jobs
You will get a list of background jobs, look for Stopped ones.
If there is and it's your editor, try re-attaching to it:
$> fg <n>
Else, if there is no running nano, make sure there is no lock-file.
ls -A *.swp
If there is, remove it.
rm nnn.swp
If you still have trouble editing the file, reboot and restart above.
Maybe there are more things to try, but that's all I can think of...

nano creates a lock file once you try to edit the file, I would say just try to move it and it might work.

Related

Vim Opening File E325 Attention Error

On Git bash windows, I was editing .bash_profile file and then I decided not to save and closed the bash console. Now when I try to open the .bash_profile using vim, I get E325: Attention error. What should I do to fix this?
By closing the console without exiting Vim first, the Vim process got killed, and Vim didn't have a chance to properly shut down. Vim uses swap files to store the last unpersisted changes to a buffer to avoid data loss in case of a crash; you can read the whole story at :help E325.
In your case, as you've consciously closed the console, there probably weren't any pending changes to your .bash_profile [worth saving]. (But there's still the swap file!) Therefore, when prompted
Swap file ".bash_profile.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort, (D)elete it:
answer with D to remove the outdated swap file. Alternatively, you can also search for the .bash_profile.swp (by default, it resides in the same directory as the edited file, likely $HOME in your case) and delete it manually. (It's hidden; use ls -a in Bash, or Windows Explorer.)
In the future, please exit Vim (:qall[!]) before closing the console it runs in, to allow for a clean shutdown.

detect when a file opens in bash

I am extremely new to bash scripting, and I need to create a script that will run a function whenever the user opens a given file (/etc/hosts) with any program.
How can I make my script detect when the file is opened?
If you have the the inotify-tools package installed (as #TobySpeight mentions in a comment above), then you have the inotifywait command available to you, so you can do something like this:
while inotifywait -e open /etc/hosts
do
echo 'hosts was opened!'
done
There are lots of options ... RTFM ... to choose files to watch, etc.
I'm guessing, but I suspect there's a race condition in my code above though. If something opens the file while the script is running the echo command, it won't notice, and could miss it while it loops back. Maybe that doesn't matter though.

Vim Ex mode loads when opening terminal

I know little about Vim in terminal(Mac) and the other day I was working copy and pasting text and i think I accidently did it when in terminal. Now whenever I open terminal it instantly loads on Vim Ex mode. I know how to quit Ex mode once in terminal but is there any way i can get rid of Vim loading when I open Terminal?
Thanks
Edit: To explain further to what i mean when I open terminal.app from Utilities I get the following
and the only way I get back to the command prompt is by typing quit every time I open terminal and i cant understand why the Vim process is running in the first place.
I was just outside the terminal in a document copy and pasting text then accidentally did a command v to paste within terminal which resulted in this happening.
It appears that you've accidentally updated one of your shell startup scripts so it launches vim.
If your default shell is csh or tcsh, take a look at .cshrc, .tcshrc, and .login in your home directory, and look for a command like vi -e or vim -e.
If your default shell is bash, check .bashrc and .bash_profile.
It may be easier to figure out which file you messed up by checking which file in your home directory was modified most recently:
% ls -altr $HOME | tail
-a lists all files, including files whose names start with ..
-l gives you a long listing, showing timestamps.
-t sorts by modification time.
-r reverses the order, so newer files are shown last

Can not edit cronjobs file in Debian with crontab -e

I have had several Debian servers and always edited cronjobs in this way:
crontab -e
and
Ctrl+x
Just got a new server and can not do it in this way anymore.
When I enter crontab -e, the file opens but I can't write anything. I can move cursor up and down but can't write. I even can not exit from this file because Ctr+x doesn't work.
When I open a file there is some information and the rest empty lines contain tildes ~ in the beginning of each line.
Any ideas how can I edit this file?
Thanks.
You need to turn on insert mode. After entering crontab -e, press i to turn on insert, enter your full line, press esc to finish entering, and then hold down shift and press z twice to save the file. This is how I managed to do it in vi/vim
As one other person has suggested, vim is obviously the default editor on your new server. You can test this by running
EDITOR=pico crontab -e
Substituting whatever is your actual preferred editor (sounds like it may be nano or pico). If that works, you should try one of the following:
edit your login script to set that environment variable on login (sets the editor just for that user)
Make sure your favourite editor is is installed and run the following (as root): update-alternatives --config sensible-editor
You can then choose the default editor for all users (they can override it individually by doing option 1).
~ would suggest that you are now editing your crontab using vi/vim instead of your usual editor
so Ctrl-X wont work, try Esc :wq
Do you have the right permissions? maybe you should open it as root user if not.
check it doing this:
ls -all $(which crontab)
if not you can change them..take a look here too..maybe you find something more!
hope it helps.

adding a shell script to a configuration file

I'm pretty new to shell scripting and linux in general. Basically, I need to change the configuration file for logging out so that when a user logs out, a certain shell script is run.
Now, I've located the logout configuration file and opened it with vi using this command
$ vi ~/.bash_logout
At this point, I'm experiencing some very weird behavior. When I try to type a character, the cursor jumps around seemingly erratically. What could this be due to? I'm running the latest version of ubuntu.
And once I get that figured out, what's the command to run a .sh file from within this configuration file?
If you're having trouble with vi, try using nano instead. nano .bash_logout
If you do need to use vi for some reason, "i" will put the editor into insert mode, and ESC will take it out of insert mode when you're done. ":wq" will write and quit the editor.
To run a command, just put it in the .bash_logout file as you would type it on the commandline.
Some other useful commands:
a insert after selected character
o insert at next line
O insert at previous line
r replace a single character
R replace mode
:q! quit without saving
:w save
:wq save and quit
To get familiar with Vi and its brother Vim ("VI improved") I recommend the book "A Byte of Vim", you can read it online or download for free at http://www.swaroopch.com/notes/Vim
You can permanently change your editor option. To find out what your current one is, type this:
export | grep -i edit
To change it on Ubuntu:
sudo update-alternatives –config editor
On any other BASH prompt, just do this:
export EDITOR="nano"
Replace 'nano' with 'vi', 'emacs', or any other preferred editor. You can also add this to your .bashrc by typing the following:
echo 'EDITOR="nano"' >> ~/.bashrc

Resources