how to write into file after doing sudo vi /dir to file
Solution 1
Vi is what is known as a modal editor - that means it has a number of modes in which it operates.
When you first start vi it is in command mode. Some common command mode key sequences:
- :q - Quit
- :q! - Force quit
- :w - Write file
- :wq! - Forced write file and exit
- :w /path/to/my/new/file - Write to a new file
- :r /path/to/file - read file and insert into buffer
To switch into one of the editing modes:
- i - Enter insert mode infront of the current character
- r - Replace single character
- R - Enter replace mode
- a - Enter insert mode after the current character
- A - Enter insert mode at the end of the current line
- o - Open a new line below cursor and enter insert mode
- O - Open a new line above cursor and enter insert mode
To return to command mode just press Esc
There are a huge number of other keypresses in command mode - this list is pretty useful.
Solution 2
type a
to append or i
to insert
Refer this document for more commands
Related videos on Youtube
Massimo Negro
Updated on September 18, 2022Comments
-
Massimo Negro almost 2 years
I entered my samplePlist.plist in the project folder and I'm trying to save the data on this .....
var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) //Get Path of Documents Directory var documentsDirectory:AnyObject = paths[0] var path = documentsDirectory.stringByAppendingPathComponent("samplePlist.plist") var fileManager = NSFileManager.defaultManager() var fileExists:Bool = fileManager.fileExistsAtPath(path) var data : NSMutableDictionary? //Check if plist file exists at path specified if fileExists == false { //File does not exists data = NSMutableDictionary () //Create data dictionary for storing in plist } else { //File exists – retrieve data from plist inside data dictionary data = NSMutableDictionary(contentsOfFile: path) } data?.setValue("\(countButton)", forKey: "NumeroButton") data?.writeToFile(path, atomically: true) //Write data to file permanently
and i Read to the plist
//Get path of Documents directory var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) var documentsDirectory:AnyObject = paths[0] var path = documentsDirectory.stringByAppendingPathComponent("samplePlist.plist") //Retrieve contents from file at specified path var data = NSMutableDictionary(contentsOfFile: path!) println(path)
my problem is that ,if I try to start it on my iphone ,I do not upload the file to the My Documents folder on startup of the Application
/var/mobile/Containers/Data/Application/9CCDBD0B-FA29-4C9E-910E-9AD5F5B11E5A/Documents/samplePlist.plist fatal error: unexpectedly found nil while unwrapping an Optional value
-
Pricey about 13 yearsMatt Jenkin's list is a good primer but if things work out too difficult, use 'nano' instead. Its probably a lot closer to the text editors your used to. Instead of writing 'vi file', write 'nano file'.
-