Saturday, April 27, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 2422  / 1 Year ago, thu, january 12, 2023, 5:05:49

I've been trying to be a good little sysadmin and use sudoedit, instead of sudo vim, or sudo -i;vim, when making changes to root owned files (ex /etc/profile), but my vim colorscheme doesn't seem to show up right when using sudoedit.



The colorsceme is loading correctly, (checked via :color). I've confirmed it's not pulling up the colorscheme for root, as the problem appears when doing a sudoedit from root as well, the colors are different, even though it shows the same scheme.



Using:




  • 12.04 LTS

  • vim: 2:7.3.429-2ubuntu2.1

  • sudo: 1.8.3p1-1ubuntu3.4



Edits:



VIM is being run:



ubuntu@ip-10-0-0-104:~$ ps -f -u ubuntu
UID PID PPID C STIME TTY TIME CMD
ubuntu 4433 4345 0 19:18 ? 00:00:00 sshd: ubuntu@notty
ubuntu 6109 6021 0 20:47 ? 00:00:00 sshd: ubuntu@pts/0
ubuntu 6110 6109 0 20:47 pts/0 00:00:00 -bash
ubuntu 6233 6232 0 20:48 pts/0 00:00:00 /usr/bin/vim /var/tmp/profile.XXQiLoee
ubuntu 6322 6234 0 20:49 ? 00:00:00 sshd: ubuntu@pts/1
ubuntu 6323 6322 0 20:49 pts/1 00:00:00 -bash
ubuntu 6461 6323 0 20:50 pts/1 00:00:00 ps -f -u ubuntu


Here is an example of what I'm talking about. (other hilghlights don't work as well, but I figured one picture would be sufficient)



vim vs sudoedit


More From » 12.04

 Answers
1

The difference in your screenshots is due to Vim using a different filetype for the two files being edited. The reason for this is most likely the name of the temporary file that sudoedit creates when editing.



When comparing syntax highlighting differences between two Vim instances, the first thing to look for is the filetype variable that Vim sets automatically. You can check what the current filetype is by executing



:set filetype?


in Vim. You can also refer to the setting as ft for short. Vim should show a result on the status line, such as



  filetype=sh


Vim has a large script of heuristics for setting the filetype automatically based on the file name, or sometimes on the file contents. If the name of the file is /etc/profile, for example, Vim sets the filetype to sh automatically.



When you use sudoedit to edit /etc/profile, a temporary file is made, as shown in your ps listing. Since the name of this file doesn't match any of Vim's scripted rules, it attempts to set the filetype based on the content. Based on the screenshot, I would guess that Vim has set the filetype to conf, meaning a generic config file.



Possible Solutions



Manual



There are a couple of ways to deal with this. First, you can set filetype manually every time you edit a file. To force sh syntax to be used, execute



:set filetype=sh


The syntax highlighting rules should be reloaded and applied immediately.



Syntax Hints



Another way is to add a hint to Vim in the content of files that you frequently edit that a particular filetype should be set. For example, if the first line of a file is #!/bin/sh, Vim will use the appropriate syntax regardless of what the file's name is. Note that this makes /etc/profile look like an executable shell script even though it is not, so you may or may not want to do something like this on your system.



Vim Modelines



A third way is to force Vim to set the filetype variable by using what Vim calls "modelines". See :help modeline for more information. This involves adding a comment to your file that contains a string that Vim can parse for commands. Back to your example with /etc/profile, adding a comment like



# vim: set filetype=sh


would instruct Vim to set the filetype properly when the file is edited regardless of its name.


[#27460] Friday, January 13, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
errettas

Total Points: 160
Total Questions: 118
Total Answers: 91

Location: Laos
Member since Fri, Sep 11, 2020
4 Years ago
;